Locally ignoring changes with Git

It’s one of those little problems that I’ve been having over and over during my whole career: You have some local changes in your working tree, but you don’t want to commit them in the end. The files should be in version control, so .gitignore is not the solution. You promise yourself to be careful when adding files to the index, but sooner or later muscle memory is coming into play and an innocent git add . happens without you noticing. You work along and in the end you are not attentive when you commit your changes. It happened to me more often than I can count, but only now I wondered if there’s way to locally ignore files. Turns out there is – obviously.

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

To revert this, you can also do the opposite:

git update-index --no-assume-unchanged

You can make it a bit less verbose by defining some aliases in your .gitconfig as well:

ignore = update-index --assume-unchanged
unignore = update-index --no-assume-unchanged
ignored = !git ls-files -v | grep "^[[:lower:]]"

That way you can use

git ignore <somefile>

to ignore a file and

git unignore <somefile>

to unignore it again. To show currently ignored files, you can use

git ignored

It’s not rocket science, if you know about it. So now I now – and you do to.

Links

Leave a Reply

Your email address will not be published. Required fields are marked *