git 如何忽略存储库中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7231608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ignore files which are in repository?
提问by prcaen
I have a file (config.php), that is already commited to Git repository, but I want to ignore locally, i.e. I want that file to remain in repository, but force Git to ignore any changes to it.
我有一个文件 (config.php),它已经提交到 Git 存储库,但我想在本地忽略,即我希望该文件保留在存储库中,但强制 Git 忽略对它的任何更改。
I put the file into .gitignore, but it is still marked as changed and Git still is attempting to commit changes to it, every time I commit something. Any idea, what am I missing or doing wrong?
我将文件放入 .gitignore,但它仍被标记为已更改,并且每次我提交某些内容时,Git 仍在尝试对其进行更改。任何想法,我错过了什么或做错了什么?
回答by VonC
If the file is still displayed in the status, even though it is in the .gitignore, make sure it isn't already tracked.
如果文件仍然显示在状态中,即使它在 .gitignore 中,请确保它尚未被跟踪。
git rm --cached config.php
If you just want to ignore it locally, you could also make it ignored by the git status:
如果你只是想在本地忽略它,你也可以让它被 git status 忽略:
git update-index --assume-unchanged config.php
回答by shiva kumar
Ignore checked in file:
忽略签入文件:
git update-index --assume-unchanged file
To revert
还原
git update-index --no-assume-unchanged file
Revert All
全部还原
git update-index --really-refresh
回答by Philip Oakley
If the file is already in the repository, and hence the Index/Staging area, then an update to .gitignore won't change that situation - It would keep beinging committed.
如果文件已经在存储库中,因此是索引/暂存区,那么对 .gitignore 的更新不会改变这种情况——它会继续被提交。
To remove the file from the Index/Staging area use git rm <file>
.
要从索引/暂存区中删除文件,请使用git rm <file>
.
回答by erikvold
Once a file is listed in the .gitignore you cannot change it.
一旦文件在 .gitignore 中列出,您就无法更改它。
So you could remove the file from the list in one commit, commit changes the next, then re-add the file to the ignore list in a 3rd commit afterwards.
因此,您可以在一次提交中从列表中删除文件,在下一次提交更改,然后在第三次提交中将文件重新添加到忽略列表中。
What I usually do is split the file into two, one that is allowed and I can change, which includes the second file if it exists, then devs can make config changes in the ignored file optionally.
我通常做的是将文件分成两个,一个是允许的,我可以更改,其中包括第二个文件(如果存在),然后开发人员可以选择在忽略的文件中进行配置更改。