git ready

learn git one commit at a time
by Nick Quaranto

temporarily ignoring files

committed 18 Feb 2009

Usually ignoring files is quite simple with Git. However, you may need to quickly hide changes in a file, perhaps for an entire development session or other reasons. Luckily there’s a simple way around this, thanks to some clever manual diving from Eelco Wiersma.

His main problem was using git commit -a, which automatically adds files that are modified into the commit object. This is a neat shortcut, but make sure you understand the staging area if you find yourself running this command all the time.

So, to temporarily ignore changes in a certain file, run:

git update-index --assume-unchanged <file>

Then when you want to track changes again:

git update-index --no-assume-unchanged <file>

Obviously there’s quite a few caveats that come into play with this. If you git add the file directly, it will be added to the index. Merging a commit with this flag on will cause the merge to fail gracefully so you can handle it manually.

Let’s go over a quick example of using the command. Changes have been made to a few files in my working directory:

$ git status
# On branch master
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#	modified:   README.textile
#	modified:   Rakefile
#	modified:   TODO
#
no changes added to commit (use "git add" and/or "git commit -a")

If I ran git commit -a from here, all of the files would be added into the new commit. However, I want to temporarily ignore the changes in one of the files:

$ git update-index --assume-unchanged README.textile   
$ git status
# On branch master
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#	modified:   Rakefile
#	modified:   TODO
#
no changes added to commit (use "git add" and/or "git commit -a")

So if we commit the work now then turn the flag off, we can see that Git didn’t lose the original changes to the README. From there, you could now add them into a new commit, or revert back to the latest copy.

$ git update-index --no-assume-unchanged README.textile
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#	modified:   README.textile
#
no changes added to commit (use "git add" and/or "git commit -a")

Check out the man page for git update-index here. If you know of other clever tricks with the index, leave a comment or submit a tip!