Delete all git branches that have been merged

0 votes
I have a lot of branches and I want to delete all of them. Is there a way of deleting them all at a time?
Aug 17, 2018 in Git & GitHub by Hannah
• 14,080 points
46 views

2 answers to this question.

0 votes

To delete all branches on remote that are already merged:

git branch -r --merged | grep -v master | sed 's/origin\//:/' | xargs -n 1 git push origin

In more recent versions of Git

git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push –delete origin
answered Aug 17, 2018 by Kalgi
• 37,430 points
0 votes

To delete all local branches that are already merged into the currently checked out branch:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

master and dev are excluded in case they are an ancestor.

You can delete a merged local branch with:

git branch -d branchname

If it's not merged, use:

git branch -D branchname

To delete it from the remote in old versions of Git use:

git push origin :branchname

In more recent versions of Git use:

git push --delete origin branchname

Once you delete the branch from the remote, you can prune to get rid of remote tracking branches with:

git remote prune origin

or prune individual remote tracking branches, as the other answer suggests, with:

git branch -dr branchname
answered Aug 21, 2018 by Nilesh
• 6,900 points

Related Questions In Git & GitHub

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+13 votes
2 answers
0 votes
1 answer
0 votes
2 answers

Delete a git branch locally and remotely

Deleting a remote branch: git push origin --delete ...READ MORE

answered Aug 20, 2018 in Git & GitHub by Nilesh
• 6,900 points
72 views
+1 vote
2 answers

Git Commands

This command basically adds all your changes ...READ MORE

answered Aug 14, 2018 in Git & GitHub by Kalgi
• 37,430 points
46 views