Ignore specific changes to a file in git, but not the entire file

前端 未结 9 1807
春和景丽
春和景丽 2020-12-28 12:14

I have a file in a git repository that has a local change on it. I want to have git ignore the local change forever, but not the file. In particular,

  • If the fi
9条回答
  •  清酒与你
    2020-12-28 12:30

    You can use the skip-worktree bit. Turn it on with:

    git update-index --skip-worktree 
    

    After that, git will never stage local changes for and will fail (loudly) if git itself has to write to (say, in a merge or a checkout).

    If you ever want to stage a future change, you can turn it off, stage the new change, and then turn it back on:

    git update-index --no-skip-worktree 
    git add -p 
    git update-index --skip-worktree 
    

    While not perfect, this might be good enough. It will be up to you to notice that has unstaged changes, since git will no longer tell you that

    Note: My original suggestion was to use assume-unchanged. As explained in Git - Difference Between 'assume-unchanged' and 'skip-worktree', it is really skip-worktree that you want. In particular, assume-unchanged is a promise to Git that you won't change the file, and if you violate that promise Git is allowed to erase your changes or commit them! In contrast, Git will not erase or commit your skip-worktree changes.

提交回复
热议问题