Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all Git branches which have been merged?

I have many Git branches. How do I delete branches which have already been merged? Is there an easy way to delete them all instead of deleting them one by one?

like image 554
Nyambaa Avatar asked May 25 '11 15:05

Nyambaa


People also ask

Can you delete branches after merge?

There's no problem in deleting branches that have been merged in. All the commits are still available in the history, and even in the GitHub interface, they will still show up (see, e.g., this PR which refers to a fork that I've deleted after the PR got accepted).

How do I delete a branch after merge in GitHub?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Settings. Under "Pull Requests", select or unselect Automatically delete head branches.

Should you delete remote branches after merge?

So basically the only reason to keep hotfix branch after a merge is if you plan to make any more changes to the same hotfix, which doesn't make much sense once you release the hotfix. So you should feel perfectly safe deleting the branch after the merge.

How do I delete old branches in git?

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch.


1 Answers

UPDATE:

You can add other branches to exclude like master and dev if your workflow has those as a possible ancestor. Usually I branch off of a "sprint-start" tag and master, dev and qa are not ancestors.

First, list locally-tracking branches that were merged in remote (you may consider to use -r flag to list all remote-tracking branches as suggested in other answers).

git branch --merged 

You might see few branches you don't want to remove. we can add few arguments to skip important branches that we don't want to delete like master or a develop. The following command will skip master branch and anything that has dev in it.

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

If you want to skip, you can add it to the egrep command like the following. The branch skip_branch_name will not be deleted.

git branch --merged| egrep -v "(^\*|master|main|dev|skip_branch_name)" 

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

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

You can see that 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 use:

git push --delete origin branchname  git push origin :branchname    # for really old git 

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 
like image 195
Adam Dymitruk Avatar answered Sep 29 '22 12:09

Adam Dymitruk