Git untrack file

August 30, 2016

Git untrack file

Many of us were in a situation, where we have a file that we don't want to commit. Moreover, it would be great if this file would disappear from index of changed files completely - we don't want to see it there.
The most common way to resolve such issue is by untracking file completely. This brings us the desired effect, but introduces another problem - all changes to this file are going to be ignored until you start to track it again, plus it will be ignored during pull merges. However, there is another command which might be handy in case you don't want to exclude your file from index completely, rather treat it as an unchanged one instead.


The most common way

As said, the most common way to untrack a file completely is this one:

$ git rm --cached filename

This works, yes, and we know the consequences. But what if you could assume file unchanged? Sounds tempting. Let's see how we can do it and what pitfalls it might have.

Assume unchanged

To treat a file as being unchanged you could use this command:

$ git --assume-unchanged filename

When this is used, the object names recorded for the paths are not updated, so it means that you can modify the file as you like but not a single change is going to be reflected in Git's index.

This is very good for collaboration in groups on one project. For instance, you have an IDE settings file that is being shared in repository for some common code-style settings. However, you want to tweak some parameters a bit but don't want them to be applied by everyone else. This command is perfect and, actually, I am using it sometimes myself.

To put this file back into Git's index you can use this command:

$ git --no-assume-unchanged filename
In case you mark a file with this flag, then as soon as it is being updated with the pull command - Git will alter the updated version with you local one and present changes in index, meaning that this command is being discarded. This is done intentionally due to the documentation, so you will have to mark this file with this command once again afterwards.


Depending on your project and task you might want to go with one command or another, there is no silver bullet. Though at least you are aware of the possibilities and will be able to make a more flexible choice.


comments powered by Disqus