在远程回购中删除本地Git分支后删除它们 [英] Delete local Git branches after deleting them on the remote repo
问题描述
在GitHub上的Pull Request检查之后,我合并并删除了我的分支。
我想让我的本地和远程存储库在分支方面始终保持同步。 (远程)。
如何在本地存储库中获取这些信息并让Git删除我的本地版本的分支?
快捷方式
git branch --merged | grep -v\ *| xargs -n 1 git branch -d
注意:如果你不在 master
,这有可能会删除分支。请继续阅读更好的方式。
确保我们保持主人
您可以确保 master
或任何其他分支,不会被 grep
ing删除。在这种情况下,你会去:
git branch --merged | grep -v\ *| grep -vYOUR_BRANCH_TO_KEEP| xargs -n 1 git branch -d
所以如果我们想保持 master ,
develop
和 staging
例如,我们会去:
git branch --merged | grep -v\ *| grep -Ev(\ * | master | develop | staging)| xargs -n 1 git branch -d
设为别名
由于时间有点长,您可能希望为您的 .zshrc
或 .bashrc $ c $添加一个别名C>。我的名字是
gbpurge
(for git branches purge
):
alias gbpurge ='git branch --merged | grep -Ev(\ * | master | develop | staging)| xargs -n 1 git branch -d'
然后重新加载您的 .bashrc
或 .zshrc
:
。 〜/ .bashrc
或
。 〜/ .zshrc
I want to have my local and remote repositories always in sync in terms of branches.
After a Pull Request review on GitHub, I merge and remove my branch there (remote). How could I fetch this information in my local repository and get Git to remove my local version of the branch as well?
The quick way
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
NB: if you're not on master
, this has the potential to delete the branch. Keep reading for the "better way".
Make sure we keep master
You can ensure that master
, or any other branch for that matter, doesn't get removed by grep
ing for more. 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:
git branch --merged | grep -v "\*" | grep -Ev "(\*|master|develop|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
. Mine is called gbpurge
(for git branches purge
):
alias gbpurge='git branch --merged | grep -Ev "(\*|master|develop|staging)" | xargs -n 1 git branch -d'
Then reload your .bashrc
or .zshrc
:
. ~/.bashrc
or
. ~/.zshrc
这篇关于在远程回购中删除本地Git分支后删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!