从整个 git 存储库中完全删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16877530/
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
Completely remove a file from whole git repository
提问by Paulo Bu
Using git for a project, I accidentally added to a commit a big .zip
file. I didn't notice until I started uploading it to github. When I noticed, I hit ctrl-c
, git remove
, git commit
and uploaded it again (now with the file untracked).
在项目中使用 git 时,我不小心在提交中添加了一个大.zip
文件。直到我开始将其上传到 github 时才注意到。当我注意到时,我点击ctrl-c
, git remove
,git commit
并再次上传(现在文件未跟踪)。
I know that this wasn't the right choice to do, because once I committed the .zip
, it stays in the repo until I revert the commit, but sadly I didn't.
我知道这不是正确的选择,因为一旦我提交了.zip
,它就会留在回购中,直到我恢复提交,但遗憾的是我没有。
Now, when someone tries to download from the repo, it takes a lot of time to do it, sometimes yields git the remote end hung up unexpectedly
(which I've read can be solved by doing some git config
) and is very annoying.
现在,当有人尝试从 repo 下载时,需要花费很多时间来完成,有时会产生收益git the remote end hung up unexpectedly
(我读过可以通过做一些来解决git config
)并且非常烦人。
My point is: is there a way to tell further pull/fetch request that forget this specific file in this specific commit version?
我的观点是:有没有办法告诉在这个特定提交版本中忘记这个特定文件的进一步拉/取请求?
回答by Paul Dixon
Github provide a useful help page on removing fileslike this. There are also other questions on StackOverflow which cover this
Github 提供了有关删除此类文件的有用帮助页面。StackOverflow 上还有其他问题涵盖了这一点
- Completely remove unwanted file from Git repository history
- How can I completely remove a file from a git repository?
See also this section of the Pro Git book, an example given there:
另请参阅Pro Git book 的这一部分,那里给出了一个示例:
To remove a file named passwords.txt from your entire history, you can use the --tree-filter option to filter-branch:
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21) Ref 'refs/heads/master' was rewritten
要从整个历史记录中删除名为 passwords.txt 的文件,您可以使用 --tree-filter 选项来过滤分支:
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21) Ref 'refs/heads/master' was rewritten
After the cleanup, you could also try a git gcto further compress and clean up for repository.
清理后,您还可以尝试使用git gc进一步压缩和清理存储库。
回答by bigsolom
There's a tool now called "BFG Repo-Cleaner", it's mentioned on github.com as an alternative to filter-branch https://help.github.com/articles/remove-sensitive-data/
现在有一个名为“BFG Repo-Cleaner”的工具,在 github.com 上提到它可以替代 filter-branch https://help.github.com/articles/remove-sensitive-data/
Link to the tool's page https://rtyley.github.io/bfg-repo-cleaner/