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 ho…


This content originally appeared on DEV Community and was authored by Paul Knulst

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

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

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 \*

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"

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"

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

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.

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

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

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"

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} "$@"'

Can be used by executing git new and providing the HEAD

git new HEAD

2. Add a remote to your local repository

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

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

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'

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'

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.knulst.de/newsletter/


This content originally appeared on DEV Community and was authored by Paul Knulst


Print Share Comment Cite Upload Translate Updates
APA

Paul Knulst | Sciencx (2022-07-14T16:25:09+00:00) How To Remove Local Git Branches By Creating Git Aliases. Retrieved from https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/

MLA
" » How To Remove Local Git Branches By Creating Git Aliases." Paul Knulst | Sciencx - Thursday July 14, 2022, https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/
HARVARD
Paul Knulst | Sciencx Thursday July 14, 2022 » How To Remove Local Git Branches By Creating Git Aliases., viewed ,<https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/>
VANCOUVER
Paul Knulst | Sciencx - » How To Remove Local Git Branches By Creating Git Aliases. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/
CHICAGO
" » How To Remove Local Git Branches By Creating Git Aliases." Paul Knulst | Sciencx - Accessed . https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/
IEEE
" » How To Remove Local Git Branches By Creating Git Aliases." Paul Knulst | Sciencx [Online]. Available: https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/. [Accessed: ]
rf:citation
» How To Remove Local Git Branches By Creating Git Aliases | Paul Knulst | Sciencx | https://www.scien.cx/2022/07/14/how-to-remove-local-git-branches-by-creating-git-aliases/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.