我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
当前回答
比git的filter-branch快100倍,更简单
在这个帖子里有很多很好的答案,但同时很多都过时了。不再推荐使用git-filter-branch,因为它很难使用,而且在大型存储库上非常慢。
Git-filter-repo使用起来更快更简单。
git-filter-repo是一个Python脚本,可以在github: https://github.com/newren/git-filter-repo上获得。安装时,它看起来像一个普通的git命令,可以由git filter-repo调用。
您只需要一个文件:Python3脚本git-filter-repo。将其复制到path变量中包含的路径。在Windows上,您可能需要更改脚本的第一行(请参阅INSTALL.md)。您需要在系统上安装Python3,但这不是什么大问题。
首先你可以跑
git filter-repo --analyze
这可以帮助你决定下一步要做什么。
你可以在任何地方删除你的DVD-rip文件:
git filter-repo --invert-paths --path-match DVD-rip
Filter-repo非常快。一个在我的电脑上用filter-branch花了9个小时的任务,用filter-repo只用了4分钟就完成了。你可以用filter-repo做更多的事情。请参阅相关文档。
警告:在存储库的副本上执行此操作。filter-repo的许多操作不能撤消。Filter-repo将更改所有修改过的提交(当然)及其所有后代直到最后一次提交的提交哈希值!
其他回答
Git filter-branch是一个功能强大的命令,你可以使用它从提交历史中删除一个巨大的文件。该文件将保留一段时间,Git将在下一次垃圾收集中删除它。 下面是从提交历史中删除文件的完整过程。为了安全起见,下面的进程首先在一个新分支上运行命令。如果结果是您所需要的,那么将其重置回您实际想要更改的分支。
# Do it in a new testing branch
$ git checkout -b test
# Remove file-name from every commit on the new branch
# --index-filter, rewrite index without checking out
# --cached, remove it from index but not include working tree
# --ignore-unmatch, ignore if files to be removed are absent in a commit
# HEAD, execute the specified command for each commit reached from HEAD by parent link
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch file-name' HEAD
# The output is OK, reset it to the prior branch master
$ git checkout master
$ git reset --soft test
# Remove test branch
$ git branch -d test
# Push it with force
$ git push --force origin master
当您遇到这个问题时,git rm是不够的,因为git会记住这个文件在我们的历史中曾经存在过一次,因此会保留对它的引用。
更糟糕的是,重基也不容易,因为任何对blob的引用都会阻止git垃圾收集器清理空间。这包括远程引用和reflog引用。
我把git forget-blob放在一起,一个尝试删除所有这些引用的小脚本,然后使用git filter-branch重写分支中的每个提交。
一旦你的blob完全没有被引用,git gc就会删除它
它的用法很简单,git forget-blob file-to-forget。你可以在这里获得更多信息
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/
多亏了Stack Overflow和一些博客的回答,我把这些放在了一起。感谢他们!
这些命令在我的案例中起作用:
git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch oops.iso' --prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
它与上面的版本没有什么不同。
对于那些需要把这个推到github/bitbucket的人(我只用bitbucket测试了这个):
# WARNING!!!
# this will rewrite completely your bitbucket refs
# will delete all branches that you didn't have in your local
git push --all --prune --force
# Once you pushed, all your teammates need to clone repository again
# git pull will not work
比git的filter-branch快100倍,更简单
在这个帖子里有很多很好的答案,但同时很多都过时了。不再推荐使用git-filter-branch,因为它很难使用,而且在大型存储库上非常慢。
Git-filter-repo使用起来更快更简单。
git-filter-repo是一个Python脚本,可以在github: https://github.com/newren/git-filter-repo上获得。安装时,它看起来像一个普通的git命令,可以由git filter-repo调用。
您只需要一个文件:Python3脚本git-filter-repo。将其复制到path变量中包含的路径。在Windows上,您可能需要更改脚本的第一行(请参阅INSTALL.md)。您需要在系统上安装Python3,但这不是什么大问题。
首先你可以跑
git filter-repo --analyze
这可以帮助你决定下一步要做什么。
你可以在任何地方删除你的DVD-rip文件:
git filter-repo --invert-paths --path-match DVD-rip
Filter-repo非常快。一个在我的电脑上用filter-branch花了9个小时的任务,用filter-repo只用了4分钟就完成了。你可以用filter-repo做更多的事情。请参阅相关文档。
警告:在存储库的副本上执行此操作。filter-repo的许多操作不能撤消。Filter-repo将更改所有修改过的提交(当然)及其所有后代直到最后一次提交的提交哈希值!
使用Git Extensions,它是一个UI工具。它有一个名为“查找大文件”的插件,可以查找存储库中的大文件,并允许永久删除它们。
在使用这个工具之前不要使用'git filter-branch',因为它不能找到被'filter-branch'删除的文件(尽管'filter-branch'不会完全从存储库包文件中删除文件)。