在远程仓库上删除本地 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 分支 -d
注意:如果您不在 master
上,这可能会删除分支.继续阅读更好的方法".
确保我们保持主人
您可以确保 master
或与此相关的任何其他分支不会被 grep
删除更多.在那种情况下,你会去:
git branch --merged |grep -v "*" |grep -v "YOUR_BRANCH_TO_KEEP" |xargs -n 1 git 分支 -d
例如,如果我们想保留 master
、develop
和 staging
,我们会这样做:
git branch --merged |grep -v "*" |grep -Ev "(*|master|develop|staging)" |xargs -n 1 git 分支 -d
将其设为别名
由于它有点长,您可能需要为 .zshrc
或 .bashrc
添加别名.我的叫做 gbpurge
(对于 git branch purge
):
alias gbpurge='git branch --merged |grep -Ev "(*|master|develop|staging)" |xargs -n 1 git 分支 -d'
然后重新加载您的 .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屋!