Last Updated: February 25, 2016
·
687
· keyboardcowboy

Trim Git Branches Locally and Remotely in One Command

Here's a handy GIT alias to trim off old branches, keeping your repos clean and tidy.

Add this alias to your git config. It's a bit complex, so you will need to do it by hand.

--global, edit ~/.gitconfig

--system, edit /etc/gitconfig

[alias]
  trim=!sh -c '[ $# = 2 ] && git branch -d "$2" && git push "$1" :"$2" || echo "Usage: git trim <remote> <branch>" >&2 && exit 1' -

To use it, just specify the remote and the branch name:

$ git trim origin my-old-branch

This will delete my-old-branch locally and attempt to delete it from the remote as well.