How do I delete branches which have already been merged? Can I delete them all at once, instead of deleting each branch one-by-one?
52 Answers
NOTE: 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 (consider using -r flag to list all remote-tracking branches).
git branch --merged You might see a few branches you don't want to remove. We can add arguments to skip important branches that we don't want to delete like master or a develop. The following command will skip the master/main branch and anything that has 'dev' in it.
git branch --merged | grep -Ev "(^\*|^\+|master|main|dev)" The first part (^\*|^+) excludes the current branch and any branch checked out in another worktree.
If you want to skip a branch, you can add it to the grep command as below. The branch skip_branch_name will not be deleted.
git branch --merged | grep -Ev "(^\*|^\+|master|main|dev|skip_branch_name)" To delete all local branches that are already merged into the currently checked out branch:
git branch --merged | grep -Ev "(^\*|^\+|master|main|dev)" | xargs --no-run-if-empty 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 To force deletion of an unmerged branch, 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 35 Comments
git branch --merged | %{$_.trim()} | ?{$_ -notmatch 'develop' -and $_ -notmatch 'master'} | %{git branch -d $_}fatal: branch name required if you have no branches that should be deleted. To avoid that you can pass -r to xargs so it won't run git branch -d if the stdin is empty. (This a GNU xargs extension, according to the man page).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 UPDATE (by @oliver; since does not fit in comment, but enough answers already): if you are on branch ABC then ABC will appear in the results of git branch -r --merged because the branch is not specified, so branch defaults to current branch, and a branch always qualifies as merged to itself (because there are no differences between a branch and itself!).
So either specify the branch:
git branch -r --merged master | grep -v master ... OR first checkout master:
git checkout master | git branch -r --merged | grep -v ... 21 Comments
dev so I had to change that| grep origin after grep -v master to prevent pushing branches of other remotes to origin. Highly recommending testing the output beforehand, using git branch -r --merged | grep -v master | grep origin | sed 's/origin\//:/' | xargs -n 1 echodevelop branch as well. git branch -r --merged | grep -v master | grep -v develop | sed 's/origin\///' | xargs -n 1 git push --delete origin. Now this turned out to be my alias.-r argument, which I've not seen mentioned anywhere else. It's taken for granted that only local branches are worth doing some housekeeping on. But remotes are full of garbage too.myFeatureBranch it will wipe origin/myFeatureBranch. Probably it's best to git checkout master first.Just extending Adam's answer a little bit:
Add this to your Git configuration by running git config -e --global
[alias] cleanup = "!git branch --merged | grep -v '\\*\\|master\\|develop' | xargs -n 1 -r git branch -d" And then you can delete all the local merged branches doing a simple git cleanup.
14 Comments
git branch --merged master since you want to look at what has been merged into master, not currently checked out branch?develop or dev and in that case the command will fail with fatal: malformed object name it's better to have a generic command and you have the responsibility to run it-r to xargs will prevent unnecessary errors (branch name required) when running this alias multiple times or when there is no branch left to be deleted. My alias looks like this: cleanup = "!git branch --merged | grep -v -P '^\\*|master|develop' | xargs -n1 -r git branch -d"You'll want to exclude the master, main & develop branches from those commands.
Local git clear:
git branch --merged | grep -v '\*\|master\|main\|develop' | xargs -n 1 git branch -d Remote git clear:
git branch -r --merged | grep -v '\*\|master\|main\|develop' | sed 's/origin\///' | xargs -n 1 git push --delete origin Sync local registry of remote branches:
git fetch -p 8 Comments
git config --global --add fetch.prune true to prune automatically on fetch or pull.git branch -r --merged | grep -v '\*\|master\|develop' | grep '^\s*origin/' | sed 's/origin\///' | tr "\n" " " | xargs git push --delete originThis also works to delete all merged branches except master.
git branch --merged | grep -v '^* master$' | grep -v '^ master$' | grep -v '^* main$' | grep -v '^ main$' | xargs git branch -d Edit: Also ignoring main branch as it's being used these days
5 Comments
master in it. Try grep -v ^master$ for the middle.| grep -v '^\*' to avoid deletting current branch if you are not on mastergrep -v '^ master$'. If you type it in yourself and miss one, you'll delete master if you're not on it.git branch will list each branch name on a new line with two spaces to the left if it is not the currently checked out branch. You have essentially guaranteed that anyone who runs this command will delete their master branch unless it is the currently checked out branch.For those of you that are on Windows and prefer PowerShell scripts, here is one that deletes local merged branches:
function Remove-MergedBranches { git branch --merged | ForEach-Object { $_.Trim() } | Where-Object { $_ -NotMatch "^\*" } | Where-Object { -not ( $_ -Like "*master" -or $_ -Like "*main" ) } | ForEach-Object { git branch -d $_ } } Or the short version:
git branch --merged | %{$_.trim()} | ?{$_ -notmatch 'dev' -and $_ -notmatch 'master' -and $_ -notmatch 'main'} | %{git branch -d $_.trim()} 4 Comments
git branch --merged | ?{-not ($_ -like "*master")} | %{git branch -d $_.trim()}for /f "usebackq" %B in (``git branch --merged^|findstr /v /c:"* " /c:"master"``) do @git branch -d %B (sigh, replace double-backquotes with single, I'm not sure how to format a literal that contains backquotes)I've used Adam's answer for years now. That said, that there are some cases where it wasn't behaving as I expected:
- branches that contained the word "master" were ignored, e.g. "notmaster" or "masterful", rather than only the master branch
- branches that contained the word "dev" were ignored, e.g. "dev-test", rather than only the dev branch
- deleting branches that are reachable from the HEAD of the current branch (that is, not necessarily master)
- in detached HEAD state, deleting every branch reachable from the current commit
1 & 2 were straightforward to address, with just a change to the regex. 3 depends on the context of what you want (i.e. only delete branches that haven't been merged into master or against your current branch). 4 has the potential to be disastrous (although recoverable with git reflog), if you unintentionally ran this in detached HEAD state.
Finally, I wanted this to all be in a one-liner that didn't require a separate (Bash|Ruby|Python) script.
TL;DR
Create a git alias "sweep" that accepts an optional -f flag:
git config --global alias.sweep '!git branch --merged $([[ $1 != "-f" ]] \ && git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" \ | xargs git branch -d' and invoke it with:
git sweep or:
git sweep -f The long, detailed answer
It was easiest for me to create an example git repo with some branches and commits to test the correct behavior:
Create a new git repo with a single commit
mkdir sweep-test && cd sweep-test && git init echo "hello" > hello git add . && git commit -am "initial commit" Create some new branches
git branch foo && git branch bar && git branch develop && git branch notmaster && git branch masterful git branch --list bar develop foo * master masterful notmaster
Desired behavior: select all merged branches except: master, develop or current
The original regex misses the branches "masterful" and "notmaster" :
git checkout foo git branch --merged | egrep -v "(^\*|master|dev)" bar
With the updated regex (which now excludes "develop" rather than "dev"):
git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)" bar masterful notmaster
Switch to branch foo, make a new commit, then checkout a new branch, foobar, based on foo:
echo "foo" > foo git add . && git commit -am "foo" git checkout -b foobar echo "foobar" > foobar git add . && git commit -am "foobar" My current branch is foobar, and if I re-run the above command to list the branches I want to delete, the branch "foo" is included even though it hasn't been merged into master:
git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)" bar foo masterful notmaster
However, if I run the same command on master, the branch "foo" is not included:
git checkout master && git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)" bar masterful notmaster
And this is simply because git branch --merged defaults to the HEAD of the current branch if not otherwise specified. At least for my workflow, I don't want to delete local branches unless they've been merged to master, so I prefer the following variant using git rev-parse:
git checkout foobar git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" bar masterful notmaster
Detached HEAD state
Relying on the default behavior of git branch --merged has even more significant consequences in detached HEAD state:
git checkout foobar git checkout HEAD~0 git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)" bar foo foobar masterful notmaster
This would have deleted the branch I was just on, "foobar" along with "foo", which is almost certainly not the desired outcome. With our revised command, however:
git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" bar masterful notmaster
One line, including the actual delete
git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" | xargs git branch -d All wrapped up into a git alias "sweep":
git config --global alias.sweep '!git branch --merged $([[ $1 != "-f" ]] \ && git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" \ | xargs git branch -d' The alias accepts an optional -f flag. The default behavior is to only delete branches that have been merged into master, but the -f flag will delete branches that have been merged into the current branch.
git sweep Deleted branch bar (was 9a56952). Deleted branch masterful (was 9a56952). Deleted branch notmaster (was 9a56952).
git sweep -f Deleted branch foo (was 2cea1ab).
6 Comments
git config atomic?!f(){ git branch .... It's a function declaration, right? Why not start directly with git branch ... ?git checkout master && git branch -d `git branch --merged` && git checkout - Except that it would delete develop, but might be a simpler approach.So many bad answers here. This is what you probably want:
git branch --delete $(git branch --merged master --no-contains master --format='%(refname:short)') This selects all local branches that have been merged into master (including master), and that aren't descendent of master (which excludes master itself). The --format is necessary because by default Git prints asterisks for the currently checked out branches. You might be able to do this with git for-each-ref too but it seems more complicated (it lists remote branches too).
You don't want:
git branch --merged(withoutmaster): This will list branches that have been "merged" into your currently checked out commit (i.e.HEAD), which is probably unexpected and not what you want.| grep -v master: There's no need for this; you can just use--no-contains.| xargs: Again, no need.git branch --deletecan delete more than one branch.
Update: I've been using this for a while and it works pretty well. There are two minor flaws:
If there are no branches to delete it will give you an error. That's fine for interactive use IMO. You shouldn't be using this in a script anyway since it's Bash and Bash should not be used for scripting.
It won't delete branches that point to the same commit as
master. I have a tool that automatically rebases all my branches, and for ones that have been merged it can leave them in this state. This may not matter to you.
7 Comments
Using Git version 2.5.0:
git branch -d `git branch --merged` 5 Comments
master branch btw!master.git branch -d $(git branch --merged | grep -v master)If you're on Windows you can use Windows Powershell or Powershell 7 with Out-GridView to have a nice list of branches and select with mouse which one you want to delete:
git branch --format "%(refname:short)" --merged | Out-GridView -PassThru | % { git branch -d $_ }
after clicking OK Powershell will pass this branches names to git branch -d command and delete them 
Comments
My favorite and simple script:
git branch --merged | grep -E -v "(master|main|develop|other)" | xargs git branch -d 4 Comments
git branch | grep -E -v "(master|develop|other)" | xargs git branch -Dgit branch --merged | grep -E -v "(master|main|develop|other)" | xargs git branch -dYou can add the commit to the --merged option. This way you can make sure only to remove branches which are merged into i.e. the origin/master
Following command will remove merged branches from your origin.
git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: |xargs -n 1 git push origin --delete You can test which branches will be removed replacing the git push origin --delete with echo
git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: |xargs -n 1 echo 1 Comment
I use the following Ruby script to delete my already merged local and remote branches. If I'm doing it for a repository with multiple remotes and only want to delete from one, I just add a select statement to the remotes list to only get the remotes I want.
#!/usr/bin/env ruby current_branch = `git symbolic-ref --short HEAD`.chomp if current_branch != "master" if $?.exitstatus == 0 puts "WARNING: You are on branch #{current_branch}, NOT master." else puts "WARNING: You are not on a branch" end puts end puts "Fetching merged branches..." remote_branches= `git branch -r --merged`. split("\n"). map(&:strip). reject {|b| b =~ /\/(#{current_branch}|master)/} local_branches= `git branch --merged`. gsub(/^\* /, ''). split("\n"). map(&:strip). reject {|b| b =~ /(#{current_branch}|master)/} if remote_branches.empty? && local_branches.empty? puts "No existing branches have been merged into #{current_branch}." else puts "This will remove the following branches:" puts remote_branches.join("\n") puts local_branches.join("\n") puts "Proceed?" if gets =~ /^y/i remote_branches.each do |b| remote, branch = b.split(/\//) `git push #{remote} :#{branch}` end # Remove local branches `git branch -d #{local_branches.join(' ')}` else puts "No branches removed." end end 3 Comments
\/ at the beginning of the reject statement for the remote_branches line. Is that a typo or does it serve a purpose?b.split(/\//) line nowHow to delete merged branches in PowerShell console
git branch --merged | %{git branch -d $_.Trim()} If you want to exclude master or any other branch names, you can pipe with PowerShell Select-String like this and pass the result to git branch -d:
git branch -d $(git branch --merged | Select-String -NotMatch "master" | %{$_.ToString().Trim()}) 3 Comments
* master :)Note: I am not happy with previous answers, (not working on all systems, not working on remote, not specifying the --merged branch, not filtering exactly). So, I add my own answer.
There are two main cases:
Local
You want to delete local branches that are already merged to another local branch. During the deletion, you want to keep some important branches, like master, develop, etc.
git branch --format "%(refname:short)" --merged master | grep -E -v '^master$|^feature/develop$' | xargs -n 1 git branch -d Notes:
git branch output --format".." is to strip whitespaces and allow exact grep matchinggrep -Eis used instead ofegrep, so it works also in systems without egrep (i.e.: git for windows).grep -E -v '^master$|^feature/develop$'is to specify local branches that I don't want to deletexargs -n 1 git branch -d: perform the deletion of local branches (it won't work for remote ones)- of course you get an error if you try deleting the branch currently checked-out. So, I suggest to switch to master beforehand.
Remote
You want to delete remote branches that are already merged to another remote branch. During the deletion, you want to keep some important branches, like HEAD, master, releases, etc.
git branch -r --format "%(refname:short)" --merged origin/master | grep -E -v '^*HEAD$|^*/master$|^*release' | cut -d/ -f2- | xargs -n 1 git push --delete origin Notes:
- for remote, we use the
-roption and provide the full branch name:origin/master grep -E -v '^*HEAD$|^*/master$|^*release'is to match the remote branches that we don't want to delete.cut -d/ -f2-: remove the unneeded 'origin/' prefix that otherwise is printed out by thegit branchcommand.xargs -n 1 git push --delete origin: perform the deletion of remote branches.
Comments
kuboon's answer missed deleting branches which have the word master in the branch name. The following improves on his answer:
git branch -r --merged | grep -v "origin/master$" | sed 's/\s*origin\///' | xargs -n 1 git push --delete origin Of course, it does not delete the "master" branch itself :)
Comments
There is no command in Git that will do this for you automatically. But you can write a script that uses Git commands to give you what you need. This could be done in many ways depending on what branching model you are using.
If you need to know if a branch has been merged into master the following command will yield no output if myTopicBranch has been merged (i.e. you can delete it)
$ git rev-list master | grep $(git rev-parse myTopicBranch) You could use the Git branch command and parse out all branches in Bash and do a for loop over all branches. In this loop you check with above command if you can delete the branch or not.
Comments
git branch --merged | grep -Ev '^(. master|\*)' | xargs -n 1 git branch -d will delete all local branches except the current checked out branch and/or master.
Here's a helpful article for those looking to understand these commands: Git Clean: Delete Already Merged Branches, by Steven Harman.
Comments
You can use git-del-br tool.
git-del-br -a You can install it via pip using
pip install git-del-br P.S: I am the author of the tool. Any suggestions/feedback are welcome.
2 Comments
The accepted solution is pretty good, but has the one issue that it also deletes local branches that were not yet merged into a remote.
If you look at the output of you will see something like
$ git branch --merged master -v api_doc 3a05427 [gone] Start of describing the Java API bla 52e080a Update wording. branch-1.0 32f1a72 [maven-release-plugin] prepare release 1.0.1 initial_proposal 6e59fb0 [gone] Original proposal, converted to AsciiDoc. issue_248 be2ba3c Skip unit-for-type checking. This needs more work. (#254) master be2ba3c Skip unit-for-type checking. This needs more work. (#254) Branches bla and issue_248 are local branches that would be deleted silently.
But you can also see the word [gone], which indicate branches that had been pushed to a remote (which is now gone) and thus denote branches can be deleted.
The original answer can thus be changed to (split into multiline for shorter line length)
git branch --merged master -v | \ grep "\\[gone\\]" | \ sed -e 's/^..//' -e 's/\S* .*//' | \ xargs git branch -d to protect the not yet merged branches. Also the grepping for master to protect it, is not needed, as this has a remote at origin and does not show up as gone.
Comments
On Windows with git bash installed egrep -v will not work
git branch --merged | grep -E -v "(master|test|dev)" | xargs git branch -d where grep -E -v is equivalent of egrep -v
Use -d to remove already merged branches or -D to remove unmerged branches
1 Comment
If you'd like to delete all local branches that are already merged in to the branch that you are currently on, then I've come up with a safe command to do so, based on earlier answers:
git branch --merged | grep -v \* | grep -v '^\s*master$' | xargs -t -n 1 git branch -d This command will not affect your current branch or your master branch. It will also tell you what it's doing before it does it, using the -t flag of xargs.
Comments
Alias version of Adam's updated answer:
[alias] branch-cleanup = "!git branch --merged | egrep -v \"(^\\*|master|dev)\" | xargs git branch -d #" Also, see this answer for handy tips on escaping complex aliases.
Comments
Below query works for me
for branch in `git branch -r --merged | grep -v '\*\|master\|develop'|awk 'NR > 0 {print$1}'|awk '{gsub(/origin\//, "")}1'`;do git push origin --delete $branch; done and this will filter any given branch in the grep pipe.
Works well over http clone, but not so well for the ssh connection.
Comments
Based on some of these answers I made my own Bash script to do it too!
It uses git branch --merged and git branch -d to delete the branches that have been merged and prompts you for each of the branches before deleting.
merged_branches () { local current_branch=$(git rev-parse --abbrev-ref HEAD) for branch in $(git branch --merged | cut -c3-) do echo "Branch $branch is already merged into $current_branch." echo "Would you like to delete it? [Y]es/[N]o " read REPLY if [[ $REPLY =~ ^[Yy] ]]; then git branch -d $branch fi done } 1 Comment
Try the following command:
git branch -d $(git branch --merged | grep -vw $(git rev-parse --abbrev-ref HEAD)) By using git rev-parse will get the current branch name in order to exclude it. If you got the error, that means there are no local branches to remove.
To do the same with remote branches (change origin with your remote name), try:
git push origin -vd $(git branch -r --merged | grep -vw $(git rev-parse --abbrev-ref HEAD) | cut -d/ -f2) In case you've multiple remotes, add grep origin | before cut to filter only the origin.
If above command fails, try to delete the merged remote-tracking branches first:
git branch -rd $(git branch -r --merged | grep -vw $(git rev-parse --abbrev-ref HEAD)) Then git fetch the remote again and use the previous git push -vdcommand again.
If you're using it often, consider adding as aliases into your ~/.gitconfig file.
In case you've removed some branches by mistake, use git reflog to find the lost commits.
Comments
As of 2018.07
Add this to [alias] section of your ~/.gitconfig:
sweep = !"f() { git branch --merged | egrep -v \"(^\\*|master|dev)\" || true | xargs git branch -d; }; f" Now you can just call git sweep to perform that needed cleanup.
git branch -Ddeletes any branch whether it as been merged or not.mastereach branch is. However, your local client will still list the old branches if you rungit branch -a; usegit fetch --pruneto remove them (as per this answer ).git delete-merged --doit originorgit delete-merged --doit --localrm -fr work && git clone http://example.com/work.gitover the years has become the easiest way to get out of a pickle with git.