Zain Rizvi
Zain Rizvi

Reputation: 24676

Stop tracking files in git (without deleting them)

I have a file in Git which I have made private modifications to. I want to keep the file in Git but don't want to check in my private modifications to it.

Currently I just don't include that file when I run git add but I'd rather not even see that in the list of modified files. Is there any way to do that?

P.S. .gitignore doesn't work because I have to delete the file from the repository to stop it from being tracked. I want the file to stay there but not contain my private changes.

Upvotes: 4

Views: 3049

Answers (1)

Zain Rizvi
Zain Rizvi

Reputation: 24676

Turns out the answer is pretty simple.

Just run git update-index --skip-worktree [path] to stop git from tracking changes to any given file.

If you ever do make changes to that file which you want to commit with git, run git update-index --no-skip-worktree [path] to make git start tracking that file again


FYI: The original answer suggested using git update-index --assume-unchanged but it turns out that option can break behavior when switching branches, while --skip-worktree is intended for this exact scenario. More details here: Git - Difference Between 'assume-unchanged' and 'skip-worktree'

Upvotes: 10

Related Questions