4

回到过去,我不小心将大量 Java 工件(.war、.jar 和 .class)提交到我的 GitHub 存储库中。这导致大小膨胀到大约 100Mb。直到后来许多提交和分支合并我才注意到。

幸运的是,有很多关于这方面的信息,所以在无休止地浏览 StackOverflow、GitHub 和 Git 文档(谢谢大家!)之后,我终于设法将以下脚本放在一起:

#!/bin/bash          
echo "Removing history for *.war, *.jar, *.class files"

echo "Starting size"
git count-objects -v

echo "Removing history for *.war, *.jar, *.class files"
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.war' --prune-empty --tag-name-filter cat -- --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.jar' --prune-empty --tag-name-filter cat -- --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.class' --prune-empty --tag-name-filter cat -- --all

echo "Purging refs and garbage collection"
# Purge the backups 
rm -Rf .git/refs/original

# Force reflog to expire now (not in the default 30 days)
git reflog expire --expire=now --all

# Prune
git gc --prune=now

# Aggressive garbage collection
git gc --aggressive --prune=now

echo 
echo "Ending size (size-pack shows new size in Kb)"
git count-objects -v

# Can't do this in the script - it needs a human to be sure
echo
echo "Now use this command to force the changes into your remote repo (origin)"
echo 
echo git push --all origin --force

这在本地运行得很好,我的 100Mb 存储库下降到大约 2Mb。然后我用

git push --all origin --force

命令用我的本地更改覆盖 GitHub 存储库中的所有分支。一切顺利。为了检查我删除了本地仓库并从 GitHub 克隆的所有内容。这应该是 2Mb,但又是 100Mb。

那么,在所有这些漫无边际的事情之后,我哪里做错了?如何强制 GitHub 使用我的本地存储库及其已清除的历史记录?

编辑以获取更多信息

无法删除 GitHub 存储库,因为它周围有很多附加信息(问题、wiki、手表等)。对空的临时存储库执行此脚本可以正常工作 - 克隆的存储库为 2Mb。

问题仍然是为什么它不适用于主仓库。

4

1 回答 1

5

都是因为叉子

事实证明,如果有人在 GitHub 上 fork 你的 repo,那么他们会保留对其中条目的链接和引用。因此,除非每个持有分叉的人也在他们的 repo 上运行脚本,否则你的清除将不起作用。

于 2012-10-16T19:37:12.047 回答