Clean your local git branches
Tags: git, github, shell, webdev
Published: 2024-02-27 07:27:44

## Context

When working with Git and a remote repository, you might have faced the scenario where, once in a while, you need to clean your local branches. 
This might be time-consuming, and you probably don't really care about it. However, I'll show you my workflow and the simple commands you can alias to clean your local branches in a wink.

## Prerequisite

Since I'm working on GitHub, I've configured my repositories to remove head branches when a pull request is merged. 

With this option enabled, your head branch will be deleted after your pull request is merged.

> This feature can be useful for identifying stale branches

![Deleting branch on merge option on github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu5vk10hh5ui0jdvco3w.png)

## Step by step

Let's build the command together as we follow the process.

> If you're not interested in the step-by-step explanations, you can jump to the end of the post, as this command is a one-liner.

### Pruning remote

The following command, `git remote prune origin`, will allow us to delete remote branch references when the remote branch does not exist. 

Since the remote branch is deleted when the pull request is merged, we need to remove the reference to this branch. 

This is a necessary step, and next, we'll find a way to identify local branches with a "gone" remote.

![Result of the remote prune origin command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqg5pyl935m50oost50l.png)

### Listing orphan branches

Now that we've removed the remote reference, we'll be able to identify those branches with `git branch -vv`. 

As you may notice, the flag `gone `appears after the origin reference. This is our key to our next steps: isolating and deleting those branches.

![Result of the branch list command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbeq603xt4ljryrd9zg1.png)

By using `grep` and `awk` we'll be able to grab the branch names with a missing remote. This is the command i'm using : `git branch -vv | grep "gone" | awk '{print $1}'`, and as you can see it works pretty well.

![Result of grep and awk on branch list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cz1rkw5iq459mxt5c67.png)

### Deleting the branch

To use the result of the previous command, we'll need xargs since it will allow us to combine the result of a command with another one. So this is the _**almost**_ final result

`git branch -vv | grep "gone" | awk '{print $1}' | xargs git branch --delete`

As you can see below, this action throws an error. This can be explained by the fact that the branch does not exist on the remote. It's the same error you'll get after a fetch with changes on the remote not present on the local branch.

![Result of the delete branch action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h96wqed1s9a0dcyc2o3c.png)

Just after, you'll find the final result (which only consists of using the force flag), and now you're able to keep your branch list as clean as your desk!

## The final result

As we saw together, this is the command you need to alias if you've implemented branch deletion on merge on GitHub.

`git branch -vv | grep "gone" | awk '{print $1}' | xargs git branch --delete --force`

![Final result of the branch cleaning command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgbq5tkr08ytke5cypkn.png)

I hope you found it useful, and see you next time! 🫡