At work we are using topic branches which are integrated into a few (3) master branches at some point. Now I'd like to delete all topic branches from my remote repository which have been fully integrated into a master branch. If that's not possible, retrieving a list of local branches which have been integrated would be fine, too.
-
**See Also**: [Delete all local git branches](https://stackoverflow.com/q/10610327/1366033) – KyleMit Jul 02 '21 at 18:53
6 Answers
Another answer edited in by someone who thought it's the best (and it looks good):
git branch -r --merged origin/master | grep -v master | grep "origin/" | cut -d "/" -f 2- | xargs -n 20 git push --delete origin
Explanation:
git branch -r --merged origin/master
-r
/--remotes
list the remote-tracking branches.--merged origin/master
only list branches whose tips are reachable fromorigin/master
.
grep -v master
remove any branch name containingmaster
from list.1-v
means negative match.grep "origin/"
select only branches onorigin
remote.cut -d "/" -f 2-
drop theorigin/
prefixxargs -n 20 git push --delete origin
do something similar togit push --delete origin branch-a branch-b branch-c …
-n 20
/--max-args=20
use at most 20 arguments per command line.
As for -n
, I choose 20 just as an example. Fewer arguments will make it slower, for example -n 1
makes it delete one at a time; you have more progress hints, because it will report each time it deletes a branch. More arguments like -n 200
will make it faster (less total time), but it only reports once every 200 branches, making you think that it is frozen at first (while it is not). Tune the number to your need. If you omit this option, the default number is really large (2048 in my machine).
1. Note that this also removes origin/HEAD -> origin/master
, but you won't want to mess with origin/HEAD
anyway.
Original answer:
git push --delete remote topicbranch
or
git push remote :topicbranch
Giving a list of branches, would be something with git branch --merged master

- 138,757
- 24
- 193
- 173
-
1Thanks, `git branch --merged` looks nice. Unfortunately it won't work properly with branches that have been rebased during integration.. but well, at least *some* of my shitload (160) of branches are listed as super-safe for deletion. – ThiefMaster Nov 23 '11 at 08:39
-
Rebased branches are trickier... That might be possible, but it would require at the very least the shellscript that will pull the list of revisions after the branch point and then looking for patch signatures along the master branch... I'm not aware of any pre-cooked solution for this. – Michael Krelin - hacker Nov 23 '11 at 08:52
-
This is fantastic, though I did hit on small snag. I name my branches with a `/`, e.g. one my of branch names might be `sf/correct-typo-7`. Because of the `-f 3` parameter on **cut**, this branch name would be truncated to `sf`. Changing the parameter to `-f 3-`, corrects this issue and handles branches with any number of slashes. – Sal Ferrarello Jun 26 '17 at 14:54
-
Why delete one at a time? What if we batch delete (by removing `-n 1`)? – Franklin Yu Aug 29 '18 at 03:51
-
@FranklinYu, can't tell, someone helpfully vandalized my answer by adding a "correct" answer to it. Maybe the reason is you need to add `--delete` before each branch? Not that I do bulk deletes daily… – Michael Krelin - hacker Aug 29 '18 at 09:52
-
@MichaelKrelin-hacker Didn't notice that until I checked changelog myself. The user is abusing the "edit without review" privilege. – Franklin Yu Aug 29 '18 at 19:39
-
Anyway since this is the accepted answer, I would suggest to edit that and explain each part of the pipeline, because typically other users would change it to fit their need. – Franklin Yu Aug 29 '18 at 19:45
-
@FranklinYu, this is how this "community" works. I can't say I'm excited about it, but since that's how it works, your edit suggestions are probably welcome. My own opinion is that it's okay to make people do research to adapt to their needs. :) There's way too much copypaste going on in the observable universe nowadays. – Michael Krelin - hacker Aug 30 '18 at 07:32
-
Thanks for the great command! It looks like you might have a small typo though. In your explanation you refer to part of the command as `cut -d "/" -f 2-`, but in the actual command you have `cut -d "/" -f 3-` – lolsky Dec 19 '19 at 18:53
-
@lolsky, thanks, fixed that one, that wasn't even my original answer, but now I need to maintain it :) (no, I don't complain, really). – Michael Krelin - hacker Dec 20 '19 at 07:57
You can do this in one go with
git branch --merged master | grep -v master | xargs -n 1 git push --delete origin
Dump that in a script called 'clean' if you find you're doing this often.

- 14,461
- 4
- 37
- 66

- 124,556
- 26
- 146
- 141
-
12If you want to remove all remote branches, not only those which have their local counterparts: `git branch -a --merged remotes/origin/master | grep -v master | grep "remotes/origin/" | cut -d "/" -f 3 | xargs -n 1 git push --delete origin`. Also, I recommend using `remotes/origin/master` in place of plain `master` to exclude stuff you merged locally but not pushed yet, in case if you eventually forget or decide not to push. – skalee Dec 06 '12 at 16:56
-
2@skalee thanks! this worked for me except when branches are in folders - use `cut -d "/" -f 3-` instead. I also had to add a `grep -v develop` to stop it trying to remove my dev branch. – antonyh Nov 20 '14 at 12:29
-
1`xargs -n 1` is too slow, if you have many branches use `xargs -n 20` instead. – mozillazg Jan 27 '16 at 03:25
-
If you want to delete remote branches from origin repository:
git branch -r --merged develop | egrep -iv '(master|develop)' | sed 's/origin\///g' | xargs -n 1 git push --delete origin

- 181
- 2
- 3
-
This is also helpful for removing all the upstream branches after a fork – ianstarz Jul 03 '15 at 23:54
-
this is the real right answer :), since the others can't seem to delete branches including slashes, which we use (mixed though) feature/my-feature, bugfix/my-feature – Tobias Hagenbeek Sep 17 '15 at 18:13
-
This looks pretty good, except it's quite important to use origin/master or origin/develop as the standard by which something may be removed. It's not OK to delete something from the remote just because you merged it into your local version of the master branch, there's a chance you forgot to push that branch to the remote, which may mean deleting unmerged branches from origin. – Theodore Murdock Jul 18 '16 at 23:06
Just for Powershell and windows users.
git branch -r --merged | findstr /v "origin/master" | %{git push origin --delete $_.Trim().Substring(7)}

- 1,461
- 19
- 17
These are the commands I'm using to remove everything merge into origin/master
. Basically, I remove all the branches merged into master
from GitHub.
git remote update -p &&
git branch -r --merged origin/master |
grep origin |
grep -v master |
cut -d"/" -f2- |
xargs git push origin --delete

- 12,772
- 11
- 42
- 73
For a Windows box I use this PowerShell oneliner to clean all merged remote git branches on a weekly basis using Windows Scheduled Tasks on our build system:
git branch --all --merged remotes/origin/master | Select-String -NotMatch "master" | Select-String -NotMatch "HEAD" | Select-String "remotes/origin/" | Foreach-Object { $_.ToString().Replace("remotes/origin/", "").Trim() } | Foreach-Object { git.exe push origin --delete $_ }
Remark: it combines most of the answers already given but without capping the number of branch cleans.

- 355
- 4
- 7