Delete multiple remote branches in git
Thanks to Neevek for great and elegant solution!
But i have some troubles with slashes in branch names (i'm using Git Flow), because of awk
field separator /
(-F
option)
So my solution is based on Neevek's, but correctly parses branch names with /
. In this case i presume that your remote called origin
.
Command for deleting remote branches with names staring with PATTERN
:
git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}' | xargs -I {} git push origin :{}
And don't forget to check what you are going to delete:
git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}'
USEFUL TIP: If your branch names (without origin/
prefix) stored in a text file (one branch name per line), just run:
cat your_file.txt | xargs -I {} git push origin :{}
This may be a duplicate answer but below tested and worked for me perfectly.
- Delete local branch forcefully
git branch -D branch-name
- Delete Remote branch
git push origin --delete branch-name
- Delete more than 1 local branch
git branch -D branch-name1 branch-name2
- Delete more than 1 remote branch
git push origin --delete branch-name1 branch-name2
- Delete local branch with prefix. For example, feature/*
git branch -D $(git branch --list 'feature/*')
git branch -D backticks $(git branch --list 'feature/*' backticks)
- List remote branch with prefix.
git branch -r | grep -Eo 'feature/.*'
- Delete remote branch with prefix
git branch -r | grep -Eo 'feature/.*' | xargs -I {} git push origin :{}
Use the following command to remove all branches with PREFIX
prefix on remote server.
git branch -r | awk -F/ '/\/PREFIX/{print $2}' | xargs -I {} git push origin :{}
You may want to do a dry-run first to see if it is the branches that you want to remove:
git branch -r | awk -F/ '/\/PREFIX/{print $2}'
If you like a simpler approach, for instance delete 3 or 4 branches:
git push origin --delete <branch1> <branch2> <branch3>
Important: Only works on Git v1.7.0 and above.