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
• 18,520 points
2,894 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
• 52,350 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
• 7,060 points

Related Questions In Git & GitHub

0 votes
1 answer

error: The branch 'dev1' is not fully merged.If you are sure you want to delete it, run 'git branch -D dev1'.

Hi@akhtar, When you create one new branch in ...READ MORE

answered May 10, 2020 in Git & GitHub by MD
• 95,460 points
3,085 views
0 votes
1 answer

How to clone all remote branches in Git?

Hi@akhtar, You can use the git clone command ...READ MORE

answered Sep 4, 2020 in Git & GitHub by MD
• 95,460 points
1,389 views
0 votes
1 answer

How to delete all the Git stashes in the stack?

Hi@akhtar, If you want to delete all the ...READ MORE

answered Nov 20, 2020 in Git & GitHub by MD
• 95,460 points
1,136 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,019 views
+2 votes
1 answer
+1 vote
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
• 7,060 points
3,525 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
• 52,350 points
1,033 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP