Member-only story
How to Remove Local Git Branches by Creating Git Aliases
A handy command for your daily workflow
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:
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.
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: