SECRET OF CSS

How to Remove Local Git Branches by Creating Git Aliases | by Paul Knulst | Jul, 2022


A handy command for your daily workflow

0*UjQ5RjB5OIbHuSHE
Photo by Ujesh Krishnan on Unsplash

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.

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.

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:

If you execute this command in your CLI, you will see that also your main and/or protected branches are included: master, main, release, etc.

You can adjust the find command to your needs (for me, it’s main/master/develop). Here’s the command:

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

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

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. Here’s an example:

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 may have already used:

  • co for checkout
  • ci for commit

To set up these aliases, you can execute:

Unfortunately, in simple Git aliases, it is impossible 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 the following:

  • 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:

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, and $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 two examples:

1. Show commits since your last Git command

Can be used by executing git new and providing the HEAD:

2. Add a remote to your local repository

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

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():

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

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 grows daily. Hopefully, you now know a technique to fix this problem and can use it in the CLI to remove every local branch that is already been merged completely.

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 leaving your valuable thoughts in the comments section. I would love to hear your feedback about my tutorial.

Want to Connect?Feel free to connect with me on my Blog, LinkedIn, and Twitter.





News Credit

%d bloggers like this: