清洁旧的git分支/提交

发布于 2025-01-21 15:17:15 字数 1051 浏览 1 评论 0原文

我有一个大的存储库(〜100 GB),其中有许多旧的分支,需要清理以免费存储空间。

要做的是

  1. 删除所有合并的
  2. 分支

基本上

  1. 我 :
git branch -r --merged | egrep -v "(^\*|master|develop)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
  1. 要删除在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
  1. 重新克隆以匹配遥控器
  2. 以删除所有提交,除了最新的剩余分支,
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

  1. Remove all merged branches
  2. Remove all branches that were created before 2022
  3. On the remaining branches, remove all commits except the last (depth of 1)

So, my current plan is this:

  1. To remove merged branches from remote:
git branch -r --merged | egrep -v "(^\*|master|develop)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
  1. 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
  1. Re-clone to match remote
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2025-01-28 15:17:15

总的来说,Mkreiger1的答案是您应该尝试的。
但是,我可以为您提供有关如何在将来管理此存储库的一般提示,以及将来的存储库,它将为您节省很多头痛。

  1. 在GitHub和Bitbucket中,您可以在拉动请求期间单击一个框,该请求说“合并后删除源分支”,也可以单击“删除源分支”,或者为此效果。那应该是一项标准行动。
  2. 您不能通过某种界面手动执行此操作吗?如果您不确定该命令的实际功能,则应执行此操作。或在其他回购上测试命令,我承认我没有看到这样的东西。
  3. 重新订阅是您的朋友: https://www.atlassian.com/ git/tutorials/重写历史/git-rebase 。通常,您会选择按时间顺序排列的,然后将其他人挤入其中。如果您在使用特定分支“完成”时执行此操作,则每个分支只能在所有更改中都有一个提交。我不明白为什么您仍然不能这样做,应该可以随时重新分支。

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.

  1. In GitHub and Bitbucket you can click a box during a pull request that says "delete source branch after merge", or something to that effect. That should be a standard action to take.
  2. Can't you just do this manually through some sort of interface? You should do this if you are unsure what that command actually does. Or test the command on a different repo, I'll admit I had not seen something like that.
  3. Rebasing is your friend here: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase. You would usually pick the chronologically first commit, and squash the others into it. If you do this whenever you are "done" with a specific branch, each branch will only have one commit with all the changes. I don't see why you can't still do this, one should be able to rebase a branch at any time.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文