可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'd like to clean up my local repository, which has a ton of old branches: for example 3.2
, 3.2.1
, 3.2.2
, etc.
I was hoping for a sneaky way to remove a lot of them at once. Since they mostly follow a dot release convention, I thought maybe there was a shortcut to say:
git branch -D 3.2.*
and kill all 3.2.x branches.
I tried that command and it, of course, didn't work.
回答1:
Not with that syntax. But you can do it like this:
git branch -D 3.2 3.2.1 3.2.2
Basically, git branch will delete multiple branch for you with a single invocation. Unfortunately it doesn't do branch name completion. Although, in bash, you can do:
git branch -D `git branch | grep -E '^3\.2\..*'`
回答2:
git branch | cut -c3- | egrep "^3.2" | xargs git branch -D ^ ^ ^ ^ | | | |--- create arguments | | | from standard input | | | | | |---your regexp | | | |--- skip asterisk |--- list all local branches
EDIT:
git for-each-ref --format="%(refname:short)" refs/heads/3.2\* | xargs git branch -D
... or the xargs-free:
git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/3.2\*`
回答3:
Well, in the worst case, you could use:
git branch | grep '3\.2' | xargs git branch -d
回答4:
Use
git for-each-ref --format='%(refname:short)' 'refs/heads/3.2.*' | xargs git branch -D
回答5:
Recently, I was looking for solution of same problem, finally i found one answer and it is working very well:
- Open the terminal, or equivalent.
- Type in git branch | grep " pattern " for a preview of the branches that will be deleted.
- Type in git branch | grep " pattern " | xargs git branch -D
This solution is awesome and if you want full explanation of each command and how it is working, its given here.
回答6:
You can use git branch --list to list the eligible branches
git branch --list '3.2.*'
Using git branch -D/-d to remove the eligible branches. One liner example:
git branch -d `git branch --list '3.2.*'`
回答7:
I made a small function that might be useful based off of the answer provided by @gawi (above).
removeBranchesWithPrefix() { git for-each-ref --format="%(refname:short)" refs/heads/$1\* | xargs git branch -d }
Add that to your .bash_profile
and restart your terminal. Then you can call from command-line like this:
removeBranchesWithPrefix somePrefix
Note
I have it currently setup for a soft delete, which means it won't delete the branches unless they've already been merged. If you like to live on the edge, change -d
to -D
and it will delete everything with the prefix regardless!
回答8:
If you're not limited to using the git command prompt, then you can always run git gui
and use the Branch->Delete menu item. Select all the branches you want to delete and let it rip.
回答9:
You might like this little item... It pulls the list and asks for confirmation of each item before finally deleting all selections...
git branch -d `git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "remove branch: $line (y/N)?" answer /tty; case "$answer" in y|Y) echo "$line";; esac; done`
Use -D to force deletions (like usual).
For readability, here is that broken up line by line...
git branch -d `git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "remove branch: $line (y/N)?" answer /tty; case "$answer" in y|Y) echo "$line";; esac; done`
here is the xargs approach...
git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "remove branch: $line (y/N)?" answer /tty; case "$answer" in y|Y) echo "$line";; esac; done | xargs git branch -D
finally, I like to have this in my .bashrc
alias gitselect='git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "select branch: $line (y/N)?" answer /tty; case "$answer" in y|Y) echo "$line";; esac; done'
That way I can just say
gitSelect | xargs git branch -D.
回答10:
As in Git all the branches are nothing by references to the git repo, why don't you just delete the branches going to .git/ref
and then if anything is left out which is not interesting in the repository will automatically be garbage collected so you don't need to bother.
回答11:
I you're on windows, you can use powershell to do this:
git branch | grep 'feature/*' |% { git branch -D $_.trim() }
replace feature/*
with any pattern you like.
回答12:
You can remove all the branches removing all the unnecessary refs:
rm .git/refs/heads/3.2.*