本文翻译自:Remove tracking branches no longer on remote
Is there a simple way to delete all tracking branches whose remote equivalent no longer exists? 有一种简单的方法可以删除所有不再具有远程等效项的跟踪分支吗?
Example: 例:
Branches (local and remote) 分支机构(本地和远程)
- master 主
- origin/master 起源/主人
- origin/bug-fix-a 起源/错误修复
- origin/bug-fix-b 原点/错误修复-b
- origin/bug-fix-c 原点/错误修复-C
Locally, I only have a master branch. 在本地,我只有一个master分支。 Now I need to work on bug-fix-a , so I check it out, work on it, and push changes to the remote. 现在,我需要处理bug-fix-a ,所以我将其检出,对其进行处理,然后将更改推送到远程计算机。 Next I do the same with bug-fix-b . 接下来,我对bug-fix-b进行同样的操作 。
Branches (local and remote) 分支机构(本地和远程)
- master 主
- bug-fix-a 错误修复
- bug-fix-b 错误修复-b
- origin/master 起源/主人
- origin/bug-fix-a 起源/错误修复
- origin/bug-fix-b 原点/错误修复-b
- origin/bug-fix-c 原点/错误修复-C
Now I have local branches master , bug-fix-a , bug-fix-b . 现在我有了本地分支master , bug-fix-a , bug-fix-b 。 The Master branch maintainer will merge my changes into master and delete all branches he has already merged. Master分支维护者将我的更改合并到master中,并删除他已经合并的所有分支。
So the current state is now: 因此,当前状态为:
Branches (local and remote) 分支机构(本地和远程)
- master 主
- bug-fix-a 错误修复
- bug-fix-b 错误修复-b
- origin/master 起源/主人
- origin/bug-fix-c 原点/错误修复-C
Now I would like to call some command to delete branches (in this case bug-fix-a , bug-fix-b ), which are no longer represented in the remote repository. 现在,我想调用一些命令来删除分支(在本例中为bug-fix-a , bug-fix-b ),这些分支不再在远程存储库中表示。
It would be something like the existing command git remote prune origin
, but more like git local prune origin
. 这将类似于现有的命令git remote prune origin
,但更像git local prune origin
。
#1楼
参考:https://stackoom.com/question/WQ8D/删除不再位于远程的跟踪分支
#2楼
Seems solution is here – https://stackoverflow.com/a/1072178/133986 似乎解决方案在这里– https://stackoverflow.com/a/1072178/133986
In short, git remote prune
does the magic 简而言之, git remote prune
做魔术
#3楼
git fetch -p
这将修剪遥控器上不再存在的所有分支。
#4楼
Remove all branches that have been merged into master, but don't try to remove master itself: 删除所有已合并到master的分支,但不要尝试删除master本身:
git checkout master && git pull origin master && git fetch -p && git branch -d $(git branch --merged | grep master -v)
or add an alias: 或添加别名:
alias gitcleanlocal="git checkout master && git pull origin master && git fetch -p && git branch -d $(git branch --merged | grep master -v)"
Explanation: 说明:
git checkout master
checkout master branch git checkout master
checkout master分支
git pull origin master
ensure local branch has all remote changes merged git pull origin master
确保本地分支已合并所有远程更改
git fetch -p
remove references to remote branches that have been deleted git fetch -p
删除对已删除的远程分支的引用
git branch -d $(git branch master --merged | grep master -v)
delete all branches that have been merged into master, but don't try to remove master itself git branch -d $(git branch master --merged | grep master -v)
删除所有已合并到master的分支,但是不要尝试删除master本身
#5楼
git remote prune origin
prunes tracking branches not on the remote. git remote prune origin
修剪跟踪不在远程上的分支。
git branch --merged
lists branches that have been merged into the current branch. git branch --merged
列出已经合并到当前分支中的分支。
xargs git branch -d
deletes branches listed on standard input. xargs git branch -d
删除标准输入中列出的分支。
Be careful deleting branches listed by git branch --merged
. 小心删除git branch --merged
列出的git branch --merged
。 The list could include master
or other branches you'd prefer not to delete. 该列表可能包含您不希望删除的master
分支或其他分支。
To give yourself the opportunity to edit the list before deleting branches, you could do the following in one line: 为了使自己有机会在删除分支之前编辑列表,可以在一行中执行以下操作:
git branch --merged >/tmp/merged-branches && vi /tmp/merged-branches && xargs git branch -d </tmp/merged-branches
#6楼
I found the answer here: How can I delete all git branches which have been merged? 我在这里找到了答案: 如何删除所有已合并的git分支?
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Make sure we keep master 确保我们保持主人
You can ensure that master
, or any other branch for that matter, doesn't get removed by adding another grep
after the first one. 您可以通过在第一个分支之后添加另一个grep
来确保不会删除master
或与此相关的任何其他分支。 In that case you would go: 在这种情况下,您可以:
git branch --merged | grep -v "\*" | grep -v "YOUR_BRANCH_TO_KEEP" | xargs -n 1 git branch -d
So if we wanted to keep master
, develop
and staging
for instance, we would go: 因此,例如,如果我们想保持master
, develop
和staging
,我们将:
git branch --merged | grep -v "\*" | grep -v "master" | grep -v "develop" | grep -v "staging" | xargs -n 1 git branch -d
Make this an alias 将此设为别名
Since it's a bit long, you might want to add an alias to your .zshrc
or .bashrc
. 由于它有点长,您可能想要为.zshrc
或.bashrc
添加别名。 Mine is called gbpurge
(for git branches purge
): 我的被称为gbpurge
(对于git branches purge
):
alias gbpurge='git branch --merged | grep -v "\*" | grep -v "master" | grep -v "develop" | grep -v "staging" | xargs -n 1 git branch -d'
Then reload your .bashrc
or .zshrc
: 然后重新加载您的.bashrc
或.zshrc
:
. ~/.bashrc
or 要么
. ~/.zshrc