在git中停止跟踪文件(不删除它们) [英] Stop tracking files in git (without deleting them)
问题描述
我在Git中有一个文件,我已经对其进行了专门的修改。我想保留在Git中的文件,但不想检查我的私人修改。
目前我只是不包含该文件,当我运行 git add
但我宁愿在修改过的文件列表中看不到它。有没有办法做到这一点?
.gitignore不起作用,因为我必须从存储库中删除文件以阻止它被跟踪。我希望文件留在那里,但不包含我的私人更改。
原来,答案非常简单。
只需运行 git update-index --skip-worktree [path]
即可停止git跟踪任何给定文件的更改。
如果您曾经对git提交的文件进行更改,请运行 git update-index --no-skip-worktree [path]
让git开始跟踪该文件
通知:原始答案建议使用 git update-index --assume-unchanged
,但事实证明,选项可以在切换分支时中断行为,而--skip-worktree用于此确切场景。更多细节在这里: Git - '假定 - 不变'和'跳过之间的区别-worktree'
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.
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'
这篇关于在git中停止跟踪文件(不删除它们)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!