您可以删除二进制膨胀并保留其余的历史记录。 Git 允许您重新排序和“压缩”先前的提交,因此您可以仅合并添加和删除大型二进制文件的提交。如果添加全部在一次提交中完成,删除则在另一次提交中完成,这将比处理每个文件容易得多。
$ git log --stat # list all commits and commit messages
在此搜索添加和删除二进制文件的提交并记下它们的 SHA1,例如2bcdef
and 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
.
.
Insert 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
现在您已经消除了臃肿,但保留了其余的历史记录。