How To Remove Local Git Branches By Creating Git Aliases

paulknulst

Paul Knulst

Posted on July 14, 2022

How To Remove Local Git Branches By Creating Git Aliases

After optimizing your Git workflow and starting working with Pull Requests you will probably have a ton of local branches that all have been merged already and could be deleted.

Within this short tutorial, I will show how you can easily do this and how you can create a Git alias that you can simply reuse everywhere.

Remove A Single Local Branch

To delete a single local branch you can open your favorite Git GUI interface and just press delete on the selected branch.
However, you cannot call yourself a developer if you are not able to use the terminal correctly!
Switch to your favorite terminal and use the following command to delete a branch:

git branch -d YOUR_BRANCH
Enter fullscreen mode Exit fullscreen mode

Unfortunately (or luckily), this command will only work if the selected branch that you want to delete is already merged. However, if you want to delete a branch that is not merged you can use the capital D:

git branch -D YOUR_BRANCH
Enter fullscreen mode Exit fullscreen mode

Remove All Local Branches

When You have multiple local branches, you probably want to delete them all with one command instead of executing the delete command for every single branch.
To implement a function that does this it is important to know that git branch -D can handle several files at once.
With this in mind, we first write a command that finds all branches in your repository:

git branch | grep -v \*
Enter fullscreen mode Exit fullscreen mode

If you execute this command in your CLI you will see that also your main and/or protected branches are included: master, main, release, ...
You can adjust the find command to your needs (for me it's main/master/develop):

git branch | grep -v "main\|master\|develop"
Enter fullscreen mode Exit fullscreen mode

Furthermore, you can add the --merged flag to indicate that you only want to find branches that are already merged

git branch --merged | grep -v "main\|master\|develop"
Enter fullscreen mode Exit fullscreen mode

Now, you can pipe the result to the delete command introduced in the previous chapter:

git branch --merged | grep -v "main\|master\|develop" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Executing this command in any Git repository (where main/master is the protected branch) will delete every branch that is already merged and have no local changes. You can also use -d to abort if you forgot --merged.
IMPORTANT:
If you execute this script while you are on a branch that has changed it does not work correctly because there will be the following error:

error: branch '*' not found.
Enter fullscreen mode Exit fullscreen mode

To avoid this issue you can adjust the command by adding a \|* in the end:

git branch --merged | grep -v "main\|master\|develop\*" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

Create A Git Alias

Simple Git Aliases

Git Aliases are a feature that improves your Git experience by making your workflow simple and easy. It packs commands into an alias so you can use it wherever you want.
Important aliases that you maybe already used:

  • co for checkout
  • ci for commit To set up these aliases you can execute:
git config --global alias.co checkout
git config --global alias.ci commit
Enter fullscreen mode Exit fullscreen mode

Unfortunately, in simple Git aliases, it is not possible to use the pipe operator correctly and forward the results of a command to another one.
Luckily, Git lets you shell out in aliases!

Complex Git Aliases

By using the ! (bang) operator your alias can escape to a shell and you are welcome to a new world of possibilities for all your aliases. With this technique you can use:

  • Shell parameters and expansions
  • Multiple git commands
  • Greps, Pipes, and all Unix command-line tools that you have installed

To create a new Git alias you can use this template and add your own command:

fancy_alias = "!f() { (your complex commands here) }; f"
Enter fullscreen mode Exit fullscreen mode

Within this alias, you use a simple trick by wrapping the actual git command in an anonymous bash-function (or better a function named f()). When you follow this approach you can access the command line variables and shell expansions as the following:

  • $1, $2, $3 for the first, second, and third parameters passed to the Git alias
  • Chain git commands with &&
  • Pipe results with |
  • use the entire Unix toolkit To demonstrate the functionality of Complex Git Alias You can have a look at the following 2 examples

1. Show commits since your last Git command

git config --global alias.new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
Enter fullscreen mode Exit fullscreen mode

Can be used by executing git new and providing the HEAD

git new HEAD
Enter fullscreen mode Exit fullscreen mode

2. Add a remote to your local repository

git config --global alias.ra = "!f() { git remote add $1 $2; }; f"
Enter fullscreen mode Exit fullscreen mode

Can be used by executing git ra and adding the remote name and remote URL:

git ra cool_remote git@github.com:paulknulst/nestjs-starter.git
Enter fullscreen mode Exit fullscreen mode

Create An Alias To Delete Branches

Now, that you know how to create an extended Git alias we can use the previous information and wrap the command with an anonymous bash function called f():

'!f() { git branch --merged | grep -v "main\|master\|develop\|*" | xargs git branch -D; }; f'
Enter fullscreen mode Exit fullscreen mode

Afterward, you can use the git config command to create a global Git alias for your current logged-in user:

git config --global alias.clean-branches '!f() { git branch --merged | grep -v "main\|master\|develop\|*" | xargs git branch -D; }; f'
Enter fullscreen mode Exit fullscreen mode

Conclusion

You can have multiple local branches that you forget to delete instantly after closing a pull request. Maybe the remote branch is already deleted but your local folder keeps growing every day. Hopefully, you now know a technique to fix this problem and can use it in the CLI to completely remove every local branch that is already been merged.

Furthermore, I hope you learned an excellent way to use Git Aliases and Complex Git Alias with the ! (bang) operator to optimize your Git workflow.

If you enjoyed reading this article consider commenting your valuable thoughts in the comments section. I would love to hear your feedback about my tutorial. Furthermore, share this article with fellow developers to also optimize their Git workflow.

Feel free to connect with me on my Personal Blog, Medium, LinkedIn, and Twitter

Also, if you want to receive more stories like this one consider subscribing my Newsletter: https://www.paulsblog.dev/newsletter/


Photo by Ujesh Krishnan / Unsplash

💖 💪 🙅 🚩
paulknulst
Paul Knulst

Posted on July 14, 2022

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

Sign up to receive the latest update from our blog.

Related