git 删除远端已经被删除然而本地还存在的分支
1. 修剪不在远程仓库上的跟踪分支
git remote prune origin
2. 本地分支尚未删除,要实现删除本地分支
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d
详解:
git branch -vv
该命令显示本地所有分支关联的远程分支,本地存在但是远端不存在的分支为gone
状态。branch1 ec2d1b1a [origin/branch1: gone] feat: ***
branch2 cdc6092d [origin/branch2: behind 45] feat: ***
branch3 e51edba2 [origin/branch3] Merge branch 'branch3' into dev
grep 'origin/.*: gone]'
对于上述结果利用grep命令行语句查找gone
得到:branch1 ec2d1b1a [origin/branch1: gone] feat: ***
awk '{print $1}'
- 使用awk命令行语句分割上面结果
branch1
xargs git branch -d
上述过程得到远端已经删除本地还没删除的分支名称,在通过xargs将其作为参数传给下一个命令git branch -d
Deleted branch branch1 (was ec2d1b1a).
参考
git remote prune origin does not delete the local branch even if its upstream remote branch is deleted