有很多类似的问题(例如this one https://stackoverflow.com/q/17983068/575530 or this one https://stackoverflow.com/q/18081085/575530)但接受的答案似乎没有回答我的确切问题。
Branch1 未合并到 master 中。机器 A 和机器 B 都指向同一个遥控器。 Branch1 存在于该遥控器上。
在机器 A 上,我在本地删除 Branch1 并将删除推送到远程,以便从机器 A 中删除 Branch1 和remotes/origin/Branch1,并从远程删除 Branch1。
在机器 B 上我运行
-
git checkout Branch1
进而
-
git checkout Branch2
进而
-
git fetch --prune
That git fetch --prune
从机器 B 上的本地存储库中删除remotes/origin/Branch1。如果我通过运行返回到 Branch1git checkout Branch1
我收到错误消息
切换到分支“Branch1”
你的分支基于“origin/Branch1”,但上游已经消失了。
(使用“gitbranch --unset-upstream”进行修复)
是否有 git 命令可以删除已删除上游的本地分支?
没有内置的方法可以做到这一点,但它is可以使用以下代码:
git fetch -p && \
git for-each-ref --format '%(refname:short) %(upstream:track)' | \
awk '$2 == "[gone]" {print $1}' | \
xargs -r git branch -D
该命令由几个部分组成:
-
git fetch -p
:确保远程分支是最新的
-
git for-each-ref --format '%(refname:short) %(upstream:track)'
: this returns the git branches in a format of our choosing, where we have both the local branch name as well as the upstream branch (which will be "[gone]" for branches where the remote was deleted)
-
awk '$2 == \"[gone]\" {print $1}'
:过滤远程状态为“[gone]”的分支,返回本地分支名称
-
xargs -r git branch -D
删除本地分支
您甚至可以通过运行以下命令将上述命令添加为 git 别名:
git config --global alias.gone "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"
该命令完成后,您可以执行以下操作git gone
并且它将删除远程分支已被删除的本地分支。
您可以阅读完整的解释这篇博文 https://www.erikschierboom.com/2020/02/17/cleaning-up-local-git-branches-deleted-on-a-remote/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)