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

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


当前回答

使用Git Extensions,它是一个UI工具。它有一个名为“查找大文件”的插件,可以查找存储库中的大文件,并允许永久删除它们。

在使用这个工具之前不要使用'git filter-branch',因为它不能找到被'filter-branch'删除的文件(尽管'filter-branch'不会完全从存储库包文件中删除文件)。

其他回答

新的答案在20222年有效。

请勿使用:

git filter-branch

此命令可能不会在按下后更改远程回购。如果你在使用它后进行克隆,你会看到什么都没有改变,回购仍然有一个很大的大小。这个命令现在已经过时了。例如,如果您使用https://github.com/18F/C2/issues/439中的步骤,这将不起作用。

你需要使用

git filter-repo

步骤:

(1)找到。git中最大的文件:

git rev-list --objects --all | grep -f <(git verify-pack -v  .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)

(2)开始过滤这些大文件:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force

or

 git filter-repo --path-glob '*.zip' --invert-paths --force

or

 git filter-repo --path-glob '*.a' --invert-paths --force

或 无论你在第一步中找到什么。

(3)

 git remote add origin git@github.com:.../...git

(4)

git push --all --force

git push --tags --force

完成了! !

使用Git Extensions,它是一个UI工具。它有一个名为“查找大文件”的插件,可以查找存储库中的大文件,并允许永久删除它们。

在使用这个工具之前不要使用'git filter-branch',因为它不能找到被'filter-branch'删除的文件(尽管'filter-branch'不会完全从存储库包文件中删除文件)。

我用一个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 /

这些命令在我的案例中起作用:

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

你可以使用branch filter命令:

git filter-branch -tree-filter 'rm -rf path/to/your/file' HEAD