Clean your local git branches

stephdotnet

Stephdotnet

Posted on February 27, 2024

Clean your local git branches

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

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

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 goneappears after the origin reference. This is our key to our next steps: isolating and deleting those branches.

Result of the branch list command

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

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

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

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

💖 💪 🙅 🚩
stephdotnet
Stephdotnet

Posted on February 27, 2024

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

Sign up to receive the latest update from our blog.

Related

Clean your local git branches
git Clean your local git branches

February 27, 2024