Git删除误提交的大文件历史记录

it2023-01-07  67

Git删除误提交的大文件历史记录


废话不多说,直接上方法

1.列出所有仓库中的对象(包括SHA值、大小、路径等),并按照大小降序排列,列出TOP 20:

$ git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4 | head -20

执行结果:

100644 blob 266fa1f45993b2ff569d237c15603b78bb5b4516 97895533 "release/2.2.9/\345\212\240\345\233\272/\345\212\240\345\233\272.rar" 100644 blob b3ebda491802e3feadf005bdff51edabf4d544b0 43542403 release/2.3.0/app-vm_2.3.0.zip 100644 blob 7c76c446f6e5dde511ec259735783beca93cd11d 50990378 "release/2.2.9/\345\212\240\347\255\276/app-Vmeng-release_2.2.9_35_signed.apk" 100644 blob d4d53797f55a41fc5ed74e69dfb891bde101d6fe 50848202 "release/2.2.9/\345\212\240\345\233\272/app-Vmeng-release_2.2.9_35.apk" 100644 blob 194651cbfdb0367b4674203af622d022fca56210 49795229 "release/2.2.9/\345\212\240\347\255\276/app-Vmeng-release_2.2.9_38_signed.apk" 100644 blob 8c7b9925a6d34934f47c4faccb83c3f1e0f6d5ec 49690079 "release/2.3.0/\345\212\240\347\255\276/app-vm_2.3.0_35_signed.apk" 100644 blob a7adb94ddccffbc022e7337e10d57ff58d09255e 49660092 "release/2.2.8/\347\255\276\345\220\215/app-Vmeng-release_2.2.8_35_signed.apk" 100644 blob 49a9edcc19f942a7329acf5fcaba09c18a7483e3 49598051 "release/2.2.9/\345\212\240\345\233\272/app-Vmeng-release_2.2.9_38.apk" 100644 blob 442b417d4cc5de86e8003d037d14356ec572630a 49547911 "release/2.3.0/\345\212\240\345\233\272/app-vm_2.3.0_35.apk" 100644 blob ff99cad129bd0de5751f575567550522afeb8f58 49518473 "release/2.2.8/\345\212\240\345\233\272/app-Vmeng-release_2.2.8_20200914105410_35.apk" 100644 blob ce83787c84b2254803a1ae3683d3305c906d5171 48656635 "release/2.3.0/\345\212\240\347\255\276/app-vm_2.3.0_38_signed.apk" 100644 blob bdd0f56750ea896fc41d7e479a217adb3adcbfd7 48617097 "release/2.2.8/\347\255\276\345\220\215/app-Vmeng-release_2.2.8_38_signed.apk" 100644 blob 25d2ac6ccdaf2aa22f9dba1f1cf9b1cd5c3ab4c1 48455273 "release/2.3.0/\345\212\240\345\233\272/app-vm_2.3.0_38.apk" 100644 blob 82af183b0c8ac5179746c7bba97316d017297318 48416545 "release/2.2.8/\345\212\240\345\233\272/app-Vmeng-release_2.2.8_20200914105503_38.apk" 100644 blob 1ebe17f16596223a88e28cb1c4a531a48b1f94ba 45720176 release/2.2.9/app-Vmeng-release_2.2.9.apk 100644 blob 09ec712df308727d08ba55489cb678ab97628fcc 44637304 release/2.3.0/app-vm_2.3.0.apk 100644 blob 79aaf28894e878007e814a5c38309cef64f37904 44614399 release/2.2.8/app-Vmeng-release_2.2.8.apk

2.找到需要删除的提交记录,7c76c446f6e5dde511ec259735783beca93cd11d是它的 id。找出该记录对应的文件,执行以下命令:

$ git rev-list --objects --all | grep 7c76c446f6e5dde511ec259735783beca93cd11d

执行结果:

7c76c446f6e5dde511ec259735783beca93cd11d release/2.2.9/加签/app-Vmeng-release_2.2.9_35_signed.apk

3.既然文件找到了,那么得将该文件从历史记录中删除,执行以下命令:

$ git log --pretty=oneline --branches -- release/2.2.9/加签/app-Vmeng-release_2.2.9_35_signed.apk

执行结果:

450fb547f9e498eb61f045d9162272647bcc364d (HEAD -> master, origin/master, origin/HEAD) v2.3.3提交 53eb6f0f0c974bf66a3fa03883ad49f1e12fb501 提交版本包

4.上面的命令执行后只是从历史记录中移除,还没有完全删除它,我们需要重写所有 commit,将该文件从 Git 历史中完全删除:

$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch release/2.2.9/加签/app-Vmeng-release_2.2.9_35_signed.apk' -- --all

执行结果:

WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites. Hit Ctrl-C before proceeding to abort, then use an alternative filtering tool such as 'git filter-repo' (https://github.com/newren/git-filter-repo/) instead. See the filter-branch manual page for more details; to squelch this warning, set FILTER_BRANCH_SQUELCH_WARNING=1. Proceeding with filter-branch... Rewrite cbf35dd01c05ea17acc8ecb387224a2c59b73820 (2/3) (1 seconds passed, remaining 0 predicted) rm 'release/2.3.0/鍔犵/app-vm_2.3.0_38_signed.apk' Rewrite 69c9f3735218e92425dfd7f7df790c34ca72a048 (2/3) (1 seconds passed, remaining 0 predicted) Ref 'refs/heads/master' was rewritten Ref 'refs/remotes/origin/master' was rewritten WARNING: Ref 'refs/remotes/origin/master' is unchanged

5.上面的命令执行后,此时历史记录中已经没有该文件了,此时是真正删除了它。 不过我们运行 filter-branch 产生的日志还是会对该文件有引用,所以我们还需要运行以下几条命令,把该文件的引用完全删除:

$ rm -Rf .git/refs/original $ rm -Rf .git/logs/ $ git gc $ git prune

6.现在我们再看 .git 文件的大小明显变小了,少了那个大文件,说明我们之前误提交的大文件已经删除了。 最后一步就是 push 代码了,不过就是需要强制 push:

$ git push --force

到这一步就大功告成了,去git上查看历史提交日志会发现被删除得文件已经没有了。

最新回复(0)