您可以删除二进制膨胀,并保留其余的历史记录。Git允许您重新排序和“压缩”先前的提交,因此您可以仅合并添加和删除大二进制文件的提交。如果所有添加都在一个提交中完成,而删除都在另一个提交中完成,则比处理每个文件要容易得多。
$ git log --stat # list all commits and commit messages
在此搜索以查找添加和删除二进制文件的提交,并记下它们的SHA1,例如2bcdef
和3cdef3
。
然后,要编辑存储库的历史记录,请使用rebase -i
命令及其交互选项,从添加二进制文件的提交的父项开始。它将启动您的$ EDITOR,您将看到以开头的提交列表2bcdef
:
$ git rebase -i 2bcdef^ # generate a pick list of all commits starting with 2bcdef
# Rebasing zzzzzz onto yyyyyyy
#
# Commands:
# pick = use commit
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
pick 2bcdef Add binary files and other edits
pick xxxxxx Another change
.
.
pick 3cdef3 Remove binary files; link to them as external resources
.
.
插入 squash 3cdef3
作为第二行,然后pick 3cdef3
从列表中删除该行。现在,您有一个用于交互式的动作列表rebase
,它将将添加和删除二进制文件的提交合并为一个提交,其区别只是这些提交中的任何其他更改。然后,当您告诉它完成时,它将按顺序重新应用所有后续提交:
$ git rebase --continue
这将需要一两分钟。
现在,您有了一个不再有二进制文件进出的仓库。但是它们仍然会占用空间,因为默认情况下,Git会将更改保留30天左右,然后再进行垃圾收集,以便您可以改变主意。如果要立即删除它们:
$ git reflog expire --expire=1.minute refs/heads/master
#all deletions up to 1 minute ago available to be garbage-collected
$ git fsck --unreachable # lists all the blobs(files) that will be garbage-collected
$ git prune
$ git gc
现在,您已消除了膨胀,但保留了其余的历史记录。