How do I batch delete redundant remote git branches?
I have a relatively large collection of remote branches from an old remote repo:
$ git branch -r
guy/feat1
guy/feat2
guy/feat3
guy/feat4
guy/feat5
guy/feat6
guy/feat7
guy/feat8
origin/HEAD
origin/master
Is there one command that would remove all guy
branches?
The repo no longer has guy
as a remote repo.
git branch -r | grep guy/ | xargs git branch -d
(假设$
符号实际上并不是你输出的一部分......)
You can also try (from git remote
):
git remote --prune guy
With:
prune
Deletes all stale remote-tracking branches under <name>
.
These stale branches have already been removed from the remote repository referenced by <name>
, but are still locally available in "remotes/".
With --dry-run
option, report what branches will be pruned, but do not actually prune them.
See also "Difference between git remote prune
and git branch -d -r
"
if guy is no longer a valide remote repo, then:
git gc --prune=now
will clean up those branches (along with some unreferenced commits, so use it with caution)
See more at "How do you Remove an Invalid Remote Branch Reference from Git?": it is usually more safe to just go with: git branch -rd guy/badbranch
if possible, but if this doesn't work, then git gc --prune=now
can also be a solution.
To delete a remote branch you can use git push <remote-repo> :branch-to-delete
note the colon before branch-to-delete. Also see Delete multiple remote branches in git ... This shows how to construct a one-liner to delete multiple branches in one go.
上一篇: 即使它出现在git分支中,也不能删除分支
下一篇: 如何批量删除冗余远程git分支?