在远程仓库上删除本地 Git 分支后删除它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17983068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete local Git branches after deleting them on the remote repo
提问by sf89
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?
在 GitHub 上进行 Pull Request 后,我在那里合并并删除了我的分支(远程)。我怎样才能在我的本地存储库中获取这些信息并让 Git 也删除我的本地版本的分支?
回答by sf89
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".
注意:如果你不在master
,这有可能删除分支。继续阅读“更好的方式”。
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:
您可以确保master
,或任何其他与此相关的分支不会被grep
ing for more删除。在那种情况下,你会去:
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 -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
):
由于它有点长,您可能需要为您的.zshrc
或.bashrc
. 我的被称为gbpurge
(对于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
:
. ~/.bashrc
or
或者
. ~/.zshrc
回答by Alessio
I use the same flow with GitHub, and didn't find the previous answers satisfying me, as git branch --merged
lists branches which were merged, but not every of them was removed remotely in my case.
So, this worked for me:
我在 GitHub 上使用相同的流程,但没有找到让我满意的先前答案,因为git branch --merged
列出了合并的分支,但在我的情况下并非每个分支都被远程删除。所以,这对我有用:
git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -n 1 git branch -d
git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -n 1 git branch -d
where:
在哪里:
git fetch --all -p
: update local branches statusgit branch -vv
: list local branches statusgrep ": gone]"
: filter deleted onesawk '{ print $1 }'
: extract their namesxargs -n 1 git branch -d
: pass the name to the delete command
git fetch --all -p
: 更新本地分支状态git branch -vv
: 列出本地分支状态grep ": gone]"
: 过滤已删除的awk '{ print $1 }'
: 提取他们的名字xargs -n 1 git branch -d
: 将名称传递给删除命令
Note: if you prefer, you could use -D instead of -d, which enforces the delete.
注意:如果您愿意,可以使用 -D 而不是 -d,后者会强制删除。
For example:
例如:
someUsr@someHost:~/repo$ git branch -a
basic-testing
integration-for-tests
* master
origin
playground-for-tests
test-services
remotes/origin/HEAD -> origin/master
remotes/origin/basic-testing
remotes/origin/master
remotes/origin/test-services
someUsr@someHost:~/repo$ git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print }' | xargs -n 1 git branch -d
Fetching origin
Deleted branch integration-for-tests (was fbc609a).
Deleted branch playground-for-tests (was 584b900).
someUsr@someHost:~/repo$ git branch -a
basic-testing
* master
origin
test-services
remotes/origin/HEAD -> origin/master
remotes/origin/basic-testing
remotes/origin/master
remotes/origin/test-services
Reference:
参考:
回答by nicky_zs
try:
尝试:
git pull --prune
which deletes your local branch, if its corresponding remote branch is deleted.
如果删除了相应的远程分支,则会删除您的本地分支。
Updated:
更新:
The statement above is not that correct.
上面的说法并不正确。
In fact, running git pull --prune
will only REMOVE the remote-tracking branchessuch like
事实上,运行git pull --prune
将只删除远程跟踪分支诸如此类
remotes/origin/fff remotes/origin/dev remotes/origin/master
Then, you can run git branch -r
to check the remote-tracking branches left on your machine. Suppose the left branches are:
然后,您可以运行git branch -r
以检查留在您机器上的远程跟踪分支。假设左分支是:
origin/dev origin/master
which means the branch origin/fff
is deleted.
这意味着该分支origin/fff
被删除。
So, after running git pull --prune
, just run:
因此,运行后git pull --prune
,只需运行:
git branch --merged | grep -vFf <(git branch -r | cut -d'/' -f2-)
git branch --merged | grep -vFf <(git branch -r | cut -d'/' -f2-)
you can find out all the local branches which:
您可以找到所有当地分支机构,其中:
- have no correspoding remote branches any more;
- can be removed safely.
- 不再有对应的远程分支;
- 可以安全移除。
then, <the command above> | xargs git branch -d
can delete all of them.
然后,<the command above> | xargs git branch -d
可以删除所有这些。
回答by Intrepid
This should work to avoid deleting the masterand developmentbranches with the accepted solution:
这应该可以避免使用已接受的解决方案删除主分支和开发分支:
git branch --merged | egrep -v "^\*|master|development" | xargs -n 1 git branch -d
回答by amaechler
For people using powershell, this is the equivalent to the answer above:
对于使用 powershell 的人,这相当于上面的答案:
git branch -vv | Select-String -Pattern ': gone]' | ForEach-Object{($_ -split "\s+")[1]} | %{ git branch -D $_ }
- Filter all the branches that are marked as gone
- Call
git branch -D
on each of the found branches
- 过滤所有标记为消失的分支
- 调用
git branch -D
每个找到的分支
回答by Karl Wilbur
None of this was working for me. You can see my other answer here: https://stackoverflow.com/a/34969726/550454
这些都不适合我。你可以在这里看到我的另一个答案:https: //stackoverflow.com/a/34969726/550454
But essentially, I now have this in my ~/.gitconfig
:
但基本上,我现在在我的~/.gitconfig
:
[alias]
prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print }' | xargs -r git branch -d
回答by Mark van der Loo
Very simple solution: remove your local repo and clone the remote one anew. May not seem very elegant, but it is simple and you'll understand exactly what you're doing without reading man pages :-).
非常简单的解决方案:删除本地存储库并重新克隆远程存储库。可能看起来不是很优雅,但它很简单,您无需阅读手册页就可以准确了解自己在做什么:-)。
回答by alacambra
I just do that to remove merged local branches:
我只是这样做来删除合并的本地分支:
git branch -d $(git branch --merged)
and in case you want to remove inexistent trackings too:
如果您也想删除不存在的跟踪:
git pull --prune
回答by Joshua Schlichting
In the event that you've just pushed and merged your branch to master, then do the following in git bash:
如果您刚刚将分支推送并合并到 master,请在 git bash 中执行以下操作:
git branch -d branch_name_to_delete
If you're currently in that branch it will push you back to master. At this point do a pull with
如果您目前在该分支中,它会将您推回 master。此时做一个拉
git pull
回答by ks1322
I've written this one-liner to list all local branches which do not have corresponding remote branch:
我写了这个单行来列出所有没有相应远程分支的本地分支:
diff -u <(git branch|sed 's/..//') <(git branch -r|sed 's/..origin\///')|tail -n +4|sed -n "s/^-//p" -
After this done, deleting these local branches is easy with xargs
:
完成后,删除这些本地分支很容易xargs
:
diff -u <(git branch|sed 's/..//') <(git branch -r|sed 's/..origin\///')|tail -n +4|sed -n "s/^-//p" -|xargs -r git branch -d