DEV Community

Cover image for Stop tracking changes to certain files in Git
Ben Selby
Ben Selby

Posted on • Originally published at benmatselby.dev

Stop tracking changes to certain files in Git

If you've used git before, you're probably aware of the .gitignore file. This is what the manual has to say:

A gitignore file specifies intentionally untracked files that Git should ignore.

This file is usually tracked in your Git repo, so every contributor ignores the same files. Interestingly, you can also have a global ignore file in your home directory too ~/.gitignore. I have a global ignore file for ignoring basic IDE files.

Recently, I had a situation whereby the team was split on whether a file should, or should not, be ignored. Tricky, however, there is a way around it.

Assume unchanged

Let's say a file is not going to be ignored in the repo, but you specifically don't want to track changes to this file locally, you can run the following command.

git update-index --assume-unchanged [file]
Enter fullscreen mode Exit fullscreen mode

This is what the manual has to say:

When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.

OK, so you're promising that you won't change the file. I think this works for our situation, as the promise is really "I don't mind if Git doesn't track this file locally". This does not impact anyone else on your project either, so it's win win.

OK, let's say you change your mind and you do want to commit changes to the file. Here's what you do to track the file locally again.

git update-index --no-assume-unchanged [file]
Enter fullscreen mode Exit fullscreen mode

What if you've completely forgotten what files you are, or are not, assuming unchanged?

git ls-files -v | grep "^[[:lower:]]"
Enter fullscreen mode Exit fullscreen mode

The -v flag will alter the ls-files output to put a lowercase letter before the file if the assume-unchanged bit is set. e.g.

h untracked-file.ts
H tracked-file.ts
Enter fullscreen mode Exit fullscreen mode

This means we can then grep on lowercase letters to see which files are assume-unchanged.

Summary

Hopefully, this solution helps you ignore files that you don't want to commit by accident, without them being in the .gitignore file for your project.


See also

Top comments (8)

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

Good tips, I barely used to be honest. I don't remember how it copes with pulling changes. I mean how conflict will be handled. Would it refuses to apply them or would it conflict?

Collapse
 
benmatselby profile image
Ben Selby

Ooo good question. I’ll have to check actually as I’ve not had that issue. The file I needed to sort very rarely changes. I’d be happy with a conflict ideally.

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

My super power is to think about edge cases, haha

I have faced years of debug and support…

Thread Thread
 
benmatselby profile image
Ben Selby

Hey Christophe - I finally got back to a machine, it gives you a conflict, which I think is what I would expect.

error: Your local changes to the following files would be overwritten by merge:
        untracked-file.ts
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
ccoveille profile image
Christophe Colombier

So it's not a conflict. It's almost the same message you would have with a rebase without autostash

So it's great, it warns you have local changes and prevents the rebase/merge.

Collapse
 
jeffzone profile image
jeff-zone

This should show up on git status.

Collapse
 
benmatselby profile image
Ben Selby

Hey jeff-zone.

Do you mean the assume-unchanged files? I cannot see this in my git status output. I'm on 2.45.0 currently.

Collapse
 
jeffzone profile image
jeff-zone

I meant this would be a good feature. Seems like relevant info to note in git status output.