如何从 Git 历史记录中删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43762338/
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 remove file from Git history?
提问by Marcos R. Guevara
Some time ago I added info(files) that must be private. Removing from the project is not problem, but I also need to remove it from git
history.
前段时间我添加了必须是私有的信息(文件)。从项目中删除不是问题,但我还需要将其从git
历史记录中删除。
I use Git and Github (private account).
我使用 Git 和 Github(私人帐户)。
On this threadShows something similar but here is an old file that was added to a feature branch, that branch merged to a development branch and finally merged to master, since this, a lot of changes was done. So it's not the same and what is needed is to change the history, and hide that files for privacy.
在这个线程上显示了类似的东西,但这里有一个旧文件,它被添加到一个特性分支,该分支合并到一个开发分支,最后合并到主分支,因为这个,做了很多更改。所以它不一样,需要的是更改历史记录,并隐藏该文件以保护隐私。
采纳答案by Petro Franko
I have found this answer and it helped:
我找到了这个答案,它有帮助:
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
Found it here https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35
在这里找到它https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35
回答by hspandher
If you have recently committed that file, or if that file has changed in one or two commits, then I'd suggest you use rebase
and cherrypick
to remove that particular commit.
如果您最近提交了该文件,或者该文件在一两次提交中发生了更改,那么我建议您使用rebase
和cherrypick
删除该特定提交。
Otherwise, you'd have to rewrite the entire history.
否则,您将不得不重写整个历史。
git filter-branch --tree-filter 'rm -f <path_to_file>' HEAD
When you are satisfied with the changes and have duly ensured that everything seems fine, you need to update all remote branches -
当您对更改感到满意并确保一切正常时,您需要更新所有远程分支 -
git push origin --force --all
Note:- It's a complex operation, and you must be aware of what you are doing. First try doing it on a demo repository to see how it works. You also need to let other developers know about it, such that they don't make any change in the mean time.
注意:- 这是一项复杂的操作,您必须清楚自己在做什么。首先尝试在演示存储库上执行此操作以查看它是如何工作的。您还需要让其他开发人员知道这一点,以便他们在此期间不会进行任何更改。
回答by suhailvs
remove the file and rewrite history from the commit you done with the removed file(this will create new commit hash from the file you commited):
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all
now force push the repo:
git push origin --force --all
now tell your collaborators to
rebase
.
删除文件并从您使用已删除文件完成的提交中重写历史记录(这将从您提交的文件中创建新的提交哈希):
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all
现在强制推送回购:
git push origin --force --all
现在告诉你的合作者
rebase
。
回答by vancy-pants
I read this GitHub article, which led me to the following command (similar to the accepted answer, but a bit more robust):
我阅读了这篇 GitHub 文章,这使我使用了以下命令(类似于已接受的答案,但更强大):
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" --prune-empty --tag-name-filter cat -- --all
回答by c1au61o_HH
Using the bfg repo-cleanerpackage is another viable alternative to git-filter-branch
. Apparently, it is also faster...
使用bfg repo-cleaner包是另一种可行的替代git-filter-branch
. 显然,它也更快......
回答by CodeWizard
- First of all, add it to your
.gitignore
file and don't forget to commit the file :-) You can use this site: http://gtiignore.ioto generate the
.gitignore
for you and add the required path to your binary files/folder(s)Once you added the file to
.gitignore
you can remove the "old" binary file with BFG.
- 首先,将它添加到您的
.gitignore
文件中,不要忘记提交文件:-) 您可以使用此站点:http: //gtiignore.io
.gitignore
为您生成并添加所需的二进制文件/文件夹路径添加文件后,
.gitignore
您可以使用 BFG 删除“旧”二进制文件。
How to remove big files from the repository
How to remove big files from the repository
You can use git filter-branch
or BFG.
https://rtyley.github.io/bfg-repo-cleaner/
您可以使用git filter-branch
或BFG。
https://rtyley.github.io/bfg-repo-cleaner/
BFG Repo-Cleaner
an alternative to git-filter-branch.
The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad dataout of your Git repository history:
* Removing Crazy Big Files*
* Removing Passwords, Credentials & other Private data
BFG Repo-Cleaner
git-filter-branch 的替代品。
BFG 是 git-filter-branch 的更简单、更快的替代方案,用于清除 Git 存储库历史记录中的不良数据:
* 删除疯狂的大文件*
* 删除密码、凭据和其他私人数据
Examples (from the official site)
示例(来自官方网站)
In all these examples bfg is an alias for java -jar bfg.jar.
在所有这些示例中,bfg 是 java -jar bfg.jar 的别名。
# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa} my-repo.git