Stop misusing –assume-unchanged flag!

I frequently see topics on StackOverflow asking how to locally ignore changes to a tracked file, and almost as often I see people recommending git update-index with --assume-unchanged flag. However, there is a more appropriate way to do this, and it is:

git update-index --skip-worktree <filename>

The problem with --assume-unchanged is that it is mean only for performance improvement. Quoting the official documentation (emphasis mine):

Many operations in Git depend on your filesystem to have an efficient lstat(2) implementation, so that st_mtime information for working tree files can be cheaply checked to see if the file contents have changed from the version recorded in the index file. Unfortunately, some filesystems have inefficient lstat(2). If your filesystem is one of them, you can set “assume unchanged” bit to paths you have not changed to cause Git not to do this check. Note that setting this bit on a path does not mean Git will check the contents of the file to see if it has changed — it makes Git to omit any checking and assume it has not changed. When you make changes to working tree files, you have to explicitly tell Git about it by dropping “assume unchanged” bit, either before or after you modify them.

The workings of --skip-worktree flag is similar, but it is meant precisely for scenarios where the developer does need to change the file, but just wants Git to ignore it:

When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

This QA from StackOverflow has two nice answers, the second one explaining thoroughly how these two flags behave in different screnarios. So, the next time you need to locally ignore changes to a tracked file, use --skip-worktree.

Leave a Reply

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