如何清除分布在GitHub仓库中的许多分支和提交的Java工件?

时间:2012-10-13 12:23:31

标签: git github

回到过去,我不小心将大量的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 repo无法删除,因为它有很多附加信息(问题,wiki,手表等)。针对空的临时存储库执行此脚本工作正常 - 克隆的存储库为2Mb。

问题仍然存在,为什么它不适用于主回购。

1 个答案:

答案 0 :(得分:4)

这完全是因为叉子

事实证明,如果有人在GitHub上分发你的回购,那么他们会保留链接并引用其中的条目。因此,除非每个持叉子的人都在他们的仓库上运行脚本,否则你的清除将无法工作。