回顾过去,我不小心将大量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
Run Code Online (Sandbox Code Playgroud)
这在本地工作得非常好,我的100Mb回购降至约2Mb。然后我用了
git push --all origin --force
Run Code Online (Sandbox Code Playgroud)
命令用我的本地更改覆盖GitHub存储库中的所有分支。一切顺利。为了检查所有内容,我删除了本地存储库并从GitHub克隆。本来应该是2Mb,但还是100Mb。
那么,经过漫长的漫漫跋涉,我哪里出错了?如何强制GitHub使用其清除的历史记录的本地存储库?
编辑以获取更多信息
无法删除GitHub存储库,因为它周围有很多其他信息(问题,Wiki,手表等)。针对空的暂存仓库执行此脚本可以正常工作-克隆的仓库为2Mb。
问题仍然在于为什么它不能与主仓库一起使用。
都是因为叉子
事实证明,如果有人在GitHub上分叉您的仓库,那么他们将保留链接和对其中条目的引用。因此,除非所有人都拿着叉子,否则清除操作将无法进行,除非所有人都在仓库中运行脚本。
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |