如何从 git 存储库中完全删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3458685/
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 can I completely remove a file from a git repository?
提问by David X
I noticed recently that the project files my text editors use (along with some other junk) got added the git repository for the project. Since they aren't actually part of the project, I'd like to remove them, but git rm
doesnt remove the old versions from the repository, and I couldnt find anything else that looks promising.
我最近注意到我的文本编辑器使用的项目文件(以及其他一些垃圾文件)为项目添加了 git 存储库。由于它们实际上不是项目的一部分,我想删除它们,但git rm
不会从存储库中删除旧版本,而且我找不到其他看起来有希望的东西。
回答by mipadi
The tool you want is git filter-branch
. Its usage is described here, but basically:
你想要的工具是git filter-branch
. 它的用法在这里描述,但基本上:
$ git filter-branch --tree-filter 'rm -f my_file' HEAD
will remove "my_file" from everycommit.
将从每次提交中删除“my_file” 。
Notice that this rewrites every commit, so if you push into a remote repository, you have to (a) force the update, and (b) everyone else who pulled from you will now have duplicate commits (since you rewrote the history), as described on the git rebase
man page.
请注意,这会重写每个提交,因此如果您推送到远程存储库,则必须 (a) 强制更新,并且 (b) 从您那里提取的所有其他人现在都会有重复的提交(因为您重写了历史记录),如git rebase
手册页中描述。
回答by nachoparker
This is what git filter-branch
is for, but beware that your repo history will change, and the commit hashes will be different after history rewrite.
这是git filter-branch
为了什么,但要注意你的 repo 历史会改变,并且在历史重写后提交哈希会有所不同。
If you also want to free the space, I recommend that you use git forget-blob
, because git filter-branch
alone will not make git forget your file, as it may still be referenced by remotes, reflog, tags and such.
如果您还想释放空间,我建议您使用git forget-blob
,因为git filter-branch
单独使用git 不会让 git 忘记您的文件,因为它仍可能被遥控器、reflog、标签等引用。
git forget-blob main.c.swp
git forget-blob main.c.swp
You can get more information here
您可以在此处获取更多信息