4

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.

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133

1 Answers1

10

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'

Community
  • 1
  • 1
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
  • 1
    Beware of checkout or pull though. read the second part of http://stackoverflow.com/a/653495/6309 – VonC Aug 13 '14 at 06:03
  • More details on difference between --skip-worktree and --assume-unchanged: http://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree – Zain Rizvi Aug 13 '14 at 18:31
  • @ZainRizvi This will only work for my local clone of the repo. Is there a way to make this behavior happen automatically for all devs? – MauriR Jan 29 '20 at 17:30
  • 1
    @MauriR not that I know of. You'd need to setup some git config file and share that with all your devs to make this somewhat automatic – Zain Rizvi Feb 13 '20 at 21:36