DEV Community

Cover image for Creating an alias for deleting useless git branches
Juan Belieni
Juan Belieni

Posted on

Creating an alias for deleting useless git branches

Sometimes, deleting branches that we are no longer working on can be tedious. For this, you can create an alias to do the hard work for you.

All git aliases are located at the ~/.gitconfig file:

...
[alias]
  cm = commit # Example alias
Enter fullscreen mode Exit fullscreen mode

The custom alias we will create will delete all branches except master and the current one:

git branch | grep -v "master" | grep -v "^*" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

At ~/.gitconfig:

...
[alias]
  del-branches = !git branch | grep -v "master" | grep -v "^*" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

Finally, you just have to call the alias at your repository directory as a normal git command:

git del-branches
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

Nice tip.

I have similar commands/aliases for deleting merged branches.

Checkout git-extras for more, fun git commands/aliases.