Ignoring files with Git is typically straightforward. However, there are times when you may need to temporarily hide changes in a file, such as during a development session. Thanks to some inventive manual tinkering by Eelco Wiersma, there’s a simple workaround for this.

His primary issue stemmed from using git commit -a, which automatically adds modified files to the commit object. While convenient, it’s essential to grasp the staging area’s concepts if you frequently use this command.

To temporarily ignore changes in a specific file, execute:

git update-index --assume-unchanged

Then, when you’re ready to track changes again:

git update-index --no-assume-unchanged

However, there are some caveats to consider. Directly adding the file with git add will include it in the index. Additionally, merging a commit with this flag set will gracefully fail the merge, allowing you to manage it manually.

Let’s illustrate this with a quick example. Suppose changes have been made to several files in the working directory:

$ git status
On branch master
#
Changed but not updated:
(use "git add …" 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")

Running git commit -a would add all files to the new commit. However, if we want to temporarily ignore changes in one file:

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

Now, committing the work and then turning off the flag shows that Git retained the original changes to README. You can proceed to add them to a new commit or revert 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 …" to update what will be committed)
#
modified: README.textile
#
no changes added to commit (use "git add" and/or "git commit -a")

Refer to the man page for git update-index for further details. If you know of other clever index tricks, feel free to share them!