在远程仓库上删除本地 Git 分支后删除它们 [英] Delete local Git branches after deleting them on the remote repo

查看:30
本文介绍了在远程仓库上删除本地 Git 分支后删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的本地和远程存储库在分支方面始终保持同步.

在 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

例如,如果我们想保留 masterdevelopstaging,我们会这样做:

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:

<预><代码>.~/.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 greping 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆