我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。

我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?


当前回答

git reset --soft HEAD~1

它将保留更改,但删除提交,然后您可以重新提交这些更改。

其他回答

为什么不使用这个简单而强大的命令呢?

git filter-branch --tree-filter 'rm -f DVD-rip' HEAD

——tree-filter选项在项目每次签出后运行指定的命令,然后重新提交结果。在这种情况下,您从每个快照中删除一个名为DVD-rip的文件,无论它是否存在。

如果你知道是哪个提交引入了这个巨大的文件(比如35dsa2),你可以用35dsa2替换HEAD。HEAD以避免重写太多的历史,从而避免在还没有推送的情况下出现不同的提交。@alpha_989提供的这个评论似乎太重要了,不能在这里省略。

请看这个链接。

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 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

使用BFG Repo-Cleaner,这是一个更简单、更快的Git -filter-branch的替代方案,专门用于从Git历史记录中删除不需要的文件。

仔细按照使用说明,核心部分就是这样:

$ java -jar bfg.jar --strip-blobs-bigger-than 100M my-repo.git

任何超过100MB大小的文件(不是最近提交的文件)都将从Git存储库的历史记录中删除。然后你可以使用git gc清除死数据:

$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

修剪后,我们可以强制推送到远程repo*

$ git push --force

*注意:不能在GitHub上强制推送一个保护分支

BFG通常比运行git-filter-branch快10-50倍,而且通常更容易使用。

完全披露:我是好心眼巨人回收清理器的作者。

我用一个bitbucket帐户遇到了这个问题,我不小心在那里存储了我网站的巨大*.jpa备份。

git filter-branch——prune-empty——index-filter 'git rm -rf——cached——ignore-unmatch MY-BIG-DIRECTORY-OR-FILE'——tag-name-filter cat -- --all

用所讨论的文件夹重新安装MY-BIG-DIRECTORY,以完全重写历史记录(包括标记)。

来源:https://web.archive.org/web/20170727144429/http: / / naleid.com: 80 /博客/ 2012/01/17 / finding-and-purging-big-files-from-git-history /