• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Git Delete Merged Branches from Remote
A weedwacker cutting grass.

Git Delete Merged Branches from Remote

Last updated on January 29, 2021 by Sal Ferrarello

When I’m working on a project using Git, I’m good about deleting local branches that I don’t need anymore. However, I often forget about my remote branches after they are merged. While it is convenient that most of the Git hosting I use makes it easy to show only unmerged branches, this also makes it easier to forget about these merged branches.

Eventually, I find I do need to clean up after myself and rather than manually going through all these branches and delete them, I’ve found the following command will delete all branches on the remote origin that have been merged into origin/master.

git branch --all --merged remotes/origin/master | grep --invert-match master | grep --invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push --delete origin

This answer is a modified version of this Stack Overflow Answer.

git delete-merged-on-origin

While this is a really helpful command, it is too long to type out each time. Fortunately, Git allows use to create an alias.

We can add the alias to our global configuration by running the following line:

git config --global alias.delete-merged-on-origin '!git branch --all --merged remotes/origin/master | grep --invert-match master | grep --invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push --delete origin'

Now, whenever we type git delete-merged-on-origin our command will be run.

How the Alias Works

The git config --global line above writes this new alias to your .gitconfig file (which is found at ~/.gitconfig on a Mac).

Another Option to Setup the Alias

The git delete-merged-on-origin command is one of the commands available with the Iron Code Studio Git Configuration I use. By setting up this project, this command and many other time saving features I use are available.

Explanation of the Command

git branch --all --merged remotes/origin/master

List both remote-tracking branches and local branches. (See git branch –all).

Show only branches that have been merged into remotes/origin/master. (See git branch –merged)

grep --invert-match master

Take the results thus far and remove any lines that match “master”. (See grep Matching Control)

grep --invert-match HEAD

Take the results thus far and remove any lines that match “HEAD”. (See grep Matching Control)

grep "remotes/origin/"

Take the results thus far and include only those lines that match “remotes/origin/”. (See grep Matching Control)

cut --d "/" -f 3-

Divide each line at the / character (-d "/")

Keep only the third section and anything after that (i.e. remove sections 1 and 2) (-f 3-)

(See cut Documentation).

xargs -n 1 git push --delete origin

Take each line we currently have and use it as the argument (i.e. put it at the end) of the command.

git push --delete origin

Each line contains the name of a branch, so each of the branches listed are deleted from origin.

Each line should be used only up to the first space (i.e. Use at most 1 argument per command line.) (-n 1)

Photo Credit

Photo by sacks08 on flickr

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips, Programming, Solution Tagged With: Git, git alias

Reader Interactions

Comments

  1. Anurag Goel says

    September 30, 2021 at 2:06 am

    I was looking for something like this to clear my remote branches after migrating my project from BitBucket to GitHub repo. Found it very useful in my case, just that double hyphen (–) in command “cut –d “/” -f 3-” is not supported on my mac, instead used single hyphen (-).

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in