Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Last active March 2, 2024 01:32
Show Gist options
  • Save sloanlance/0f0cb5e9819e11d698a26a623bc649f4 to your computer and use it in GitHub Desktop.
Save sloanlance/0f0cb5e9819e11d698a26a623bc649f4 to your computer and use it in GitHub Desktop.
git: A couple ways to temporarily ignore changed or new files without altering .gitignore.

There are times notifications aren't wanted about either a changed repo file or a new file that needs to be added to the repo. However, adding the name of the file to .gitignore might not be a good option, either. For example, locally-generated files that other users aren't likely to generate (e.g., files created by an editor) or files of experimental test code might not be appropriate to appear in a .gitignore file.

In those cases, use one of these solutions:

  1. If the file is a changed repo file

    Use the command:

    git update-index --assume-unchanged "$FILE"

    To undo this, use the command:

    git update-index --no-assume-unchanged "$FILE"

    The update-index command doesn't work with new files that haven't been added to the repo yet, though.

  2. If the file is new and not added to the repo

    Add its filename to the repo's exclude file:

    echo "$FILE" >> .git/info/exclude

    This also works for changed repo files, but there isn't a specific undo command for it. The exclude file would need to be edited and the filename deleted from it. Or other commands could approximate it:

    ex -s -c"g/^${FILE}\$/d" -cwq .git/info/exclude

    Note that this overwrites the existing exclude file and if the filename specified contains special characters that could affect the regular expression, the results are unpredictable.

    This usage of the exclude file is recommended by the page "Ignoring files" on GitHub Help.

@karlhorky
Copy link

Thanks, this is super useful! 🙌 💯

Just used it in Preflight, a program that we use to check students' work, to ignore the changes to the .replit file if they are running on repl.it:

upleveled/preflight@ab2a879

@FabienDehopre
Copy link

FabienDehopre commented Feb 10, 2021

Sometimes, the file that has been modified and mark as unchanged with the command git update-index --assume-unchanged "$FILE" is reset to its original content after a commit.

@sloanlance
Copy link
Author

@karlhorky: I'm glad this helped you. I'm happy that this is still relevant after more than four years. Your project sounds interesting, I'll check it out.

@FabienDehopre: Thanks for the information. I'll try to replicate that and update this document, if necessary.

Thanks to both of you for starring this gist!

@marcelomrwin
Copy link

@sloanlance Thanks, very helpful.

@alexamy
Copy link

alexamy commented Aug 16, 2022

You can get the list of ignored files (by --assume-unchanged) with this command:

git ls-files -v | grep '^[[:lower:]]'

@PostCyberPunk
Copy link

based on this:
you may want to try

 git update-index --skip-worktree [<file> ...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment