Delete all git remote merged branches

November 24, 2017

A shell command to delete orphan (merged) branches on a remote git repo.

If you use Github the default workflow requires that you manually delete a branch after you’ve merged it.

While easy, it’s also easy to forget to do.

Forgetting to delete a branch

Forgetting to delete a branch

After a while you can collect a lot of orphan branches, so I wrote it as a reference for myself for how I did this.

git branch --list --color=never --remotes --merged origin/master | \
  grep -v master | \
  grep -v stable  | \
  cut -d/ -f2- | \
  xargs -n1  git push origin --delete

This shell command will;

  • Lists all remote merged branches
  • Exclude the master branch
  • Exclude the stable branch
  • Remove the origin/ prefix
  • Push the deletion (one by one)

Originally published as a gist.