Delete merged branches using one command

Here is a command that you can use to delete all the branches that have been merged to master branch from both local and remote using just one command. Instead of deleting branch one by one, I use below commands to delete all the branches that have bee…


This content originally appeared on DEV Community and was authored by Raj Maharjan

Here is a command that you can use to delete all the branches that have been merged to master branch from both local and remote using just one command. Instead of deleting branch one by one, I use below commands to delete all the branches that have been merged.

Delete local merged branches:
git branch --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git branch -d
Delete remote merged branches:
git branch -r --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git push --delete origin

You can change branch name from master to any other branch name that you refer to as your main branch.

Also you can use it as alias by adding it in .bashrc file.
alias gclb='git branch --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git branch -d'
alias gcrb='git branch -r --merged master | egrep -v "(^\*|master|dev)" | cut -d/ -f2- | xargs -n 1 git push --delete origin'

It has been a lot easier for me to use it this way. If you do it differently or have better way please comment below.


This content originally appeared on DEV Community and was authored by Raj Maharjan


Print Share Comment Cite Upload Translate
APA
Raj Maharjan | Sciencx (2022-10-02T13:16:59+00:00) » Delete merged branches using one command. Retrieved from https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/.
MLA
" » Delete merged branches using one command." Raj Maharjan | Sciencx - Friday April 30, 2021, https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/
HARVARD
Raj Maharjan | Sciencx Friday April 30, 2021 » Delete merged branches using one command., viewed 2022-10-02T13:16:59+00:00,<https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/>
VANCOUVER
Raj Maharjan | Sciencx - » Delete merged branches using one command. [Internet]. [Accessed 2022-10-02T13:16:59+00:00]. Available from: https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/
CHICAGO
" » Delete merged branches using one command." Raj Maharjan | Sciencx - Accessed 2022-10-02T13:16:59+00:00. https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/
IEEE
" » Delete merged branches using one command." Raj Maharjan | Sciencx [Online]. Available: https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/. [Accessed: 2022-10-02T13:16:59+00:00]
rf:citation
» Delete merged branches using one command | Raj Maharjan | Sciencx | https://www.scien.cx/2021/04/30/delete-merged-branches-using-one-command/ | 2022-10-02T13:16:59+00:00
https://github.com/addpipe/simple-recorderjs-demo