删除所有已推送的 git 个分支
Delete all git branches already pushed
我的需要
你好,我正在寻找一个命令来删除所有本地分支已经推送到原点。
我特别想保留所有提交尚未推送到各自远程分支的分支。
原因
git prune
通过在删除远程时清除分支来完成部分工作,但由于我有许多功能分支,我只需要保留尚未完全推送到远程的分支,以避免在我的回购协议中列出一长串本地分支机构,将它们限制在实际工作中。
谢谢!
git remote update # `git fetch --all` but it allows configurable skipping, see the docs
git for-each-ref refs/heads --format='
[[ "%(upstream)" != "" ]] && # there is an upstream branch
git merge-base --is-ancestor %(refname) %(upstream) && # which has this tip
echo delete %(refname)
' # | sh # | git update-ref --stdin
为 shell 生成命令既有用又有趣——如果你 c&p 上面的内容,它实际上不会对你的 repo 做任何事情,它的输出是检查分支是否在的命令上游;删除一个 #
它会执行检查而不是仅仅打印它们,试试吧。
尽管就 jthill 建议的灵活而聪明的解决方案而言,它看起来像是一头瞎了眼的大象,但我在这里分享了迄今为止我倾向于使用的残酷方法,以满足您描述的相同需求:
# nuke all branches* (which have no unique commit)
git branch -d $(git for-each-ref --format="%(refname:short) refs/heads")
# for "main" branches (I mean permanent, like master), recreate them if needed from remote
git checkout master
这是 one-shot 版本,但为方便起见,可以很容易地从中创建别名。
请注意 -d
标志,与 -D
相反,它表示 "soft deletion" 并拒绝删除未合并提交的分支(安全检查被 -D
忽略)。
* 此处,git 可能(取决于当前分支的状态)抱怨无法删除您所在的分支。在直接命令模式下没什么大不了的,但在别名下有点烦人。只需事先签出一个模拟分支,然后将其删除。由于我们正在确定所有内容,因此我倾向于将分支命名为 from-orbit
.
我的需要
你好,我正在寻找一个命令来删除所有本地分支已经推送到原点。
我特别想保留所有提交尚未推送到各自远程分支的分支。
原因
git prune
通过在删除远程时清除分支来完成部分工作,但由于我有许多功能分支,我只需要保留尚未完全推送到远程的分支,以避免在我的回购协议中列出一长串本地分支机构,将它们限制在实际工作中。
谢谢!
git remote update # `git fetch --all` but it allows configurable skipping, see the docs
git for-each-ref refs/heads --format='
[[ "%(upstream)" != "" ]] && # there is an upstream branch
git merge-base --is-ancestor %(refname) %(upstream) && # which has this tip
echo delete %(refname)
' # | sh # | git update-ref --stdin
为 shell 生成命令既有用又有趣——如果你 c&p 上面的内容,它实际上不会对你的 repo 做任何事情,它的输出是检查分支是否在的命令上游;删除一个 #
它会执行检查而不是仅仅打印它们,试试吧。
尽管就 jthill 建议的灵活而聪明的解决方案而言,它看起来像是一头瞎了眼的大象,但我在这里分享了迄今为止我倾向于使用的残酷方法,以满足您描述的相同需求:
# nuke all branches* (which have no unique commit)
git branch -d $(git for-each-ref --format="%(refname:short) refs/heads")
# for "main" branches (I mean permanent, like master), recreate them if needed from remote
git checkout master
这是 one-shot 版本,但为方便起见,可以很容易地从中创建别名。
请注意 -d
标志,与 -D
相反,它表示 "soft deletion" 并拒绝删除未合并提交的分支(安全检查被 -D
忽略)。
* 此处,git 可能(取决于当前分支的状态)抱怨无法删除您所在的分支。在直接命令模式下没什么大不了的,但在别名下有点烦人。只需事先签出一个模拟分支,然后将其删除。由于我们正在确定所有内容,因此我倾向于将分支命名为 from-orbit
.