Git - 如何从提交历史记录中删除大文件,以便我可以推送代码库?

4

我几个月前不小心将一个约100MB的目录提交到了我的代码库中。

现在,我想将该代码库迁移到Github,但由于存在大小限制,无法完成迁移。

通过以下帖子删除了该目录:彻底从Git版本控制中删除文件

看起来已经生效,但整个项目仍然有100M的大小。运行du .命令后输出如下:98M ./.git/objects

我尝试过一些解决方案,但似乎都不可行,不确定该怎么办。

编辑:我通过上面链接中的Darren的回答成功解决了问题。


1
尝试运行 git gc --prune=now - Gergo Erdosi
没有帮助。它已经被缩小到95MB(从99MB下降)。 - matanc1
类似的问题:https://dev59.com/ZYLba4cB1Zd3GeqPhKA9 - Alexan
尝试在Gergo的命令中添加“--aggressive”。 - wolfovercats
1
我没有除了 master 以外的任何分支。无论如何,我已经修复了它并将解决方案添加到我的问题中。 - matanc1
显示剩余2条评论
2个回答

2

我通过上面链接的帖子解决了这个问题。具体来说,我做了类似于Darren答案中的操作。以下是为您提供方便所做的操作:

This is the best way:
http://github.com/guides/completely-remove-a-file-from-all-revisions

Just be sure to backup the copies of the files first.

EDIT

The edit by [Neon][1] got unfortunately rejected during review. See Neons post below, it might contain useful information!

[1]: https://stackoverflow.com/users/309261/neon


E.g. to remove all *.gz files accidentally committed into git repository:

$ du -sh .git ==> e.g. 100M
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.gz' HEAD
$ git push origin master --force
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now

That still didn't work for me? (I am currently at git version 1.7.6.1)

$ du -sh .git ==> e.g. 100M

Not sure why, since I only had ONE master branch. Anyways, I finally got my git repo truely cleaned up by pushing into a new empty and bare git repository, e.g.

$ git init --bare /path/to/newcleanrepo.git
$ git push /path/to/newcleanrepo.git master
$ du -sh /path/to/newcleanrepo.git ==> e.g. 5M 

(yes!)

Then I clone that to a new directory and moved over it's .git folder into this one. e.g.

$ mv .git ../large_dot_git
$ git clone /path/to/newcleanrepo.git ../tmpdir
$ mv ../tmpdir/.git .
$ du -sh .git ==> e.g. 5M 

(yeah! finally cleaned up!)

After verifying that all is well, then you can delete the ../large_dot_git and ../tmpdir directories (maybe in a couple weeks or month from now, just in case...)

简而言之:我筛选了分支,创建了一个新的裸仓库,将主分支推送到其中,克隆到一个新的目录中,并用克隆的git目录替换了我的项目的git目录。

1
你需要在reflog中“过期”条目,否则它将保留对那些旧blob的引用,然后进行“垃圾回收”。
git reflog expire --expire=now --all
git gc --prune=now --aggressive

如果那不起作用,请尝试使用BFG工具来清理您的存储库。按照他们的建议,首先复制存储库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接