清洁旧的git分支/提交
我有一个大的存储库(〜100 GB),其中有许多旧的分支,需要清理以免费存储空间。
要做的是
- 删除所有合并的
- 分支
- ,
基本上
- 我 :
git branch -r --merged | egrep -v "(^\*|master|develop)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
- 要删除在2022年之前创建的分支:
for k in $(git branch -r | sed /\*/d); do
if [ -n "$(git log -1 --before='Jan 1, 2022' -s $k)" ]; then
git push origin --delete "${k/origin\//}"
fi
done
- 重新克隆以匹配遥控器
- 以删除所有提交,除了最新的剩余分支,
for ref in $(git branch -r | sed /\*/d)
do
git checkout $ref
echo "Current branch: $ref"
c="$(git rev-parse HEAD)"
echo "Recreating $ref branch with initial commit $c ..."
git checkout --orphan new-start $c
git commit -C $c
git rebase --onto new-start $c $ref
git branch -d new-start
git reflog expire --expire=now --all; git gc --prune=now
done
所以我的问题是针对具有很多GIT专业知识的人,此计划看起来如何?试图在我弄乱一些东西之前,试图让另一组眼睛
I have a large repo (~100 GB) with lots of old branches that needs to be cleaned up to free repository space.
Basically, what I want to do is
- Remove all merged branches
- Remove all branches that were created before 2022
- On the remaining branches, remove all commits except the last (depth of 1)
So, my current plan is this:
- To remove merged branches from remote:
git branch -r --merged | egrep -v "(^\*|master|develop)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
- To remove branches created before 2022:
for k in $(git branch -r | sed /\*/d); do
if [ -n "$(git log -1 --before='Jan 1, 2022' -s $k)" ]; then
git push origin --delete "${k/origin\//}"
fi
done
- Re-clone to match remote
- To remove all commits except latest on remaining branches
for ref in $(git branch -r | sed /\*/d)
do
git checkout $ref
echo "Current branch: $ref"
c="$(git rev-parse HEAD)"
echo "Recreating $ref branch with initial commit $c ..."
git checkout --orphan new-start $c
git commit -C $c
git rebase --onto new-start $c $ref
git branch -d new-start
git reflog expire --expire=now --all; git gc --prune=now
done
So my question is for those with lots of git expertise, how does this plan look? Trying to get another set of eyes on it before I mess something up haha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总的来说,Mkreiger1的答案是您应该尝试的。
但是,我可以为您提供有关如何在将来管理此存储库的一般提示,以及将来的存储库,它将为您节省很多头痛。
In general, mkreiger1's answer is something you should try.
But, I can give you general tips on how to manage this repo in the future, as well as future repos, it will save you a lot of headaches.