loading...

Easily deleting local git branches

teamroggers profile image Rogier Nitschelm ・3 min read

If you are like me, you forget to delete your old branches after merging them into master. So rather than just having a handful of branches, I end up with a massive list of old git branches. For a while, I had no clue on how to clean them up quickly. I wondered if there was some hidden git-command that would dispose of my unused branches, but a colleague of mine suggested to use a few basic Unix-tools.

Let's imagine we have four branches in total. Our master-branch and four other branches:

$ git branch
  a
  b
  c
  d
* master

Grep

To delete the branches we first have to collect the ones that we want to delete. We have to search for them in our list of local branches. There is a handy-tool available for that with the name grep that allows us to search for pieces of text. The default way grep works is that it returns the matching text. But that won't do us much good, because we'd still have to specify all the branches we want to delete.

That's where flags come in. Grep allows us to pass it a flag for reversed searches: -v.

$ git branch | grep -v master
  a
  b
  c
  d

Now we grab all our branches except master.

Deleting

Now we know how to grab the branch-names we want to delete, we have to delete them. As you probably already know, you can delete a git branch using git branch -D your_branch_name.

However, we want to call this command on all our old unused branches. For that, we need to be able to somehow call git branch -D on our list of branches. We can do that by piping the result of our grep-command into it. This time we need the xargs-command to pass the result of our grep as input-strings to our delete-command. xargs git branch -D.

As a word of caution: before actually running the script, I suggest to always inspect your branches before deleting them. You don't want to delete the wrong branch accidentally:

# First see all our branches
$ git branch | grep -v "master"
  a
  b
  c
  d

# Are these the branches we expect? If yes then actually delete the branches
$ git branch | grep -v "master" | xargs git branch -D

Other flags and regexes

Despite our script working for this simple example, there are some potential problems with it in its current state.

For example the situation in which we have a branch named "master2", or "jedi-master-yoda". Those branches would not be included in the deletion. So we might have to expand our little script.

Here are a few other options to use:

# Delete everything except a full match on the word master (it would still match on jedi-master-yoda!)

$ git branch | grep -vw "master" # Check the result
$ git branch | grep -vw "master" | xargs git branch -D

# Delete everything except exactly the branch master
$ git branch | grep -vw " master$" # Check the result
$ git branch | grep -vw " master$" | xargs git branch -D

# Delete everything except our current branch
$ git branch | grep -vw "*" # Check the result
$ git branch | grep -vw "*" | xargs git branch -D

# Delete everything except our current branch or master
$ git branch | grep -vw "*\| master$" # Check the result
$ git branch | grep -vw "*\| master$" | xargs git branch -D

The "^" represents the start of a line, whereas $ represents the end of a line. The "|" is the OR-operator that we can use to look for multiple different matches.

If ever you forget the way a tool works, you can use the man command to inspect the optional flags that come with each tool: man grep and man xargs.

Discussion

pic
Editor guide