Creating an alias for deleting useless git branches

juanbelieni

Juan Belieni

Posted on August 30, 2021

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
💖 💪 🙅 🚩
juanbelieni
Juan Belieni

Posted on August 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Linux and Git-GitHub cheat sheet!
linux Linux and Git-GitHub cheat sheet!

October 15, 2024

Kafka Quickstart
kafka Kafka Quickstart

March 13, 2024

Github git cheat sheet
github Github git cheat sheet

December 4, 2020