从 Git 存储库中删除现有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4124792/
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
Remove an Existing File from a Git Repo
提问by Splashlin
I want git to stop tracking my local development log (log/development.log) in our repositories. Is this possible and how can I do it?
我希望 git 停止跟踪我们存储库中的本地开发日志 (log/development.log)。这是可能的,我该怎么做?
回答by Tim Visher
Just to give the full answer all at once:
只是一次给出完整的答案:
from klemens: You need to add the file to your
.gitignore
file somewhere above the undesired file in the repo. i.e.$ cd $ cat >> .gitignore development.log C-d
from m. narebski: You then need to remove the file from the repo by executing "
git rm --cached <file>
and then committing this removal"
来自 klemens:您需要将文件添加到存储库中
.gitignore
不需要的文件上方的文件中。IE$ cd $ cat >> .gitignore development.log cd
从米。narebski:然后您需要通过执行“
git rm --cached <file>
然后提交此删除”从存储库中删除文件
If you were also hoping to make the repo look as if it had never tracked that file, that is much more complicated and highly discouraged as it not only creates brand new commits for every single commit in your history, thus destroying interoperability in a nasty way between other people who have cloned your repo, but it also leaves every one of those commits untested (assuming you test your commits before making them).
如果您还希望让 repo 看起来好像它从未跟踪过该文件,那将更加复杂且非常不鼓励,因为它不仅为您历史上的每一次提交都创建了全新的提交,从而以一种令人讨厌的方式破坏了互操作性在克隆了你的 repo 的其他人之间,但它也使每一个提交都未经测试(假设你在提交之前测试了你的提交)。
With that caveat in mind, the tool you're looking for if that's your goal is filter-branch
. The first example does exactly what I'm describing.
考虑到这一点,如果这是您的目标,您正在寻找的工具是filter-branch
. 第一个例子完全符合我的描述。
回答by kusma
To stop tracking the file, simply use git rm --cached <filename>
.
要停止跟踪文件,只需使用git rm --cached <filename>
.
But as pointed out by the others here, adding the file to .gitignore might prevent you from accidentally adding it again later.
但正如这里的其他人所指出的,将文件添加到 .gitignore 可能会防止您以后意外地再次添加它。