git 如何停止跟踪文件而不将其从 repo 中删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7072213/
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 stop tracking a file without removing it from repo
提问by bgcode
I have a config file which I want to keep on the remote repository, but I don't want to track its changes on my computer. Adding it to .gitignore doesn't do the trick.
我有一个配置文件,我想将其保留在远程存储库中,但我不想在我的计算机上跟踪它的更改。将它添加到 .gitignore 并不能解决问题。
The reason I don't want to track changes is because it's supposed to differ between computers depending on their environment.
我不想跟踪更改的原因是因为它应该因计算机的环境而异。
采纳答案by cdhowie
If that's the case then you shouldn't have the file versioned at all; you should version a template of the file. For example, if the configuration file is foo/config.txt
then you should have a versioned foo/config.txt.template
in the repository with example (or blank) configuration settings. foo/config.txt
should not be in the repository at all, and should be ignored with .gitignore
.
如果是这种情况,那么您根本不应该对文件进行版本控制;你应该版本文件的模板。例如,如果配置文件是,foo/config.txt
那么您应该foo/config.txt.template
在存储库中具有示例(或空白)配置设置的版本。 foo/config.txt
根本不应该在存储库中,并且应该被忽略.gitignore
。
Then, in a new clone, you just copy foo/config.txt.template
to foo/config.txt
and alter the settings as appropriate.
然后,在新的克隆中,您只需复制foo/config.txt.template
到foo/config.txt
并根据需要更改设置。
回答by gracchus
If you want to temporarily stop tracking a file, you can still use
如果您想暂时停止跟踪文件,您仍然可以使用
git update-index --assume-unchanged <file>
// track changes again :
git update-index --no-assume-unchanged <file>
回答by Dhaval Chheda
You will need to do the combination of .gitignore file and git rm --cached. Use the following command :
您需要组合 .gitignore 文件和 git rm --cached。使用以下命令:
git rm --cached filename.txt
git rm --cached 文件名.txt
This will remove the file from indexing but it will keep it in your repository and working area. After applying rm --cached you will need to commit the changes for it to take effect. It will show as Deleted in your tracked changes, but that's only because it has been deleted from indexing. Note: If you make changes to it again in future commits you will have to run: git rm --cached to untrack the changes to the file from the particular commit.
这将从索引中删除文件,但会将其保留在您的存储库和工作区中。应用 rm --cached 后,您需要提交更改才能生效。它会在您的跟踪更改中显示为已删除,但这只是因为它已从索引中删除。注意:如果您在以后的提交中再次对其进行更改,则必须运行: git rm --cached 以从特定提交中取消跟踪对文件的更改。