Day 5 - Trim your branches

Congrats on reaching the end of week one!

It’s Friday, so let’s end with a quick win:

  1. Run git branch -r. Marvel at all the old tracking branches that have been left in your local repo.

  2. Run git remote prune origin to delete the local tracking branches that don’t exist on origin anymore. You might want to throw a --dry-run on there to confirm that git is going to do the right thing.

  3. Re-run git branch -r. Better, right?

  4. Now that your local repo is clean, take a look at the branches on origin by running git ls-remote --heads origin.

  5. Delete any of your branches that are no longer needed with git push origin --delete old_branch.

  6. Maybe bug your coworkers to do 4 and 5, too.

  7. Enjoy the weekend!

1 Like

Wow, I went from 204 to 45 just from step 2.


I have some bash scripts that I’ve been using for a while for this


branches=$(git branch --merged | grep -v '*' | grep -v master)

if [[ $branches != '' && $length > 0 ]]; then
  echo $branches | xargs git branch -d
  echo Merged branches not found

branches=$(git fetch -p && git branch -vv | grep -v '^*' | awk '/: gone]/{print $1}')

if [[ $branches != '' && $length > 0 ]]; then
  echo $branches | xargs git branch -D
  echo No branches with missing remotes

which would automatically remove merged branches and branches with missing remotes (usually merged but with rebased commit)… saved as git-localprune so that I can run it as git localprune

and then another bash script to also take care of pruning remote which looks like


git pull
git remote prune origin
git localprune

saved as git-prunsuka… so everytime I need to pull master, I do git prunsuka instead of git pull:smiley:

NB: prunsuka is a combination of prune and punsuka (プンスカ)which is japanese onomatopoeia for / (╯°□°)╯︵ ┻━┻


Great exercise. Quickest so far for me.
I only had one old branch forgotten and removed across 9 repos.

But I’m still used from my old company to always delete branches once merged into master, and with the rule master must always be deployable.

Week 1 in review: great second first morning exercise after checking my Trufflepiggy stats. Really enjoyed the experience so far. Thank you @ben! For week 2 I try to be quicker and more strict with the 20min limit because I just looked at my time tracking and on average I spent 1 hour per day on CQC (including reading other results, and posting results). Way more than I should :smile:.

Last but not least 5 of 5 exercises :white_check_mark: yeah :tada:

1 Like

I’m pretty discipline about this one.
No unused branch for me.

Thanks for the first week, great exercises.
Looking forward the next ones.


1 Like

I thought, that there will be a lot of unsued branches, but it seems I’ve deleted them at some point.
Deleted only one branch, that was no longer used.

Once again I’m a little lucky on this one, things got a bit out of hand at the end of last year while a feature grew a few arms and legs on one repo, but I’d gone through and sorted them in December.

If anyone is looking to keep a better visualisation, I find Sourcetree a good way to visualise stashes and branches (remote and local). I use it alongside the command line, for some reason my brain likes being able to jump between the two :slight_smile:


I use this script to clean the old and merged branches:

I don’t like doing things like this via script because I like looking at old branches and making sure they are really merged/obsolete before actually deleting them.

For me, git branch -r didn’t help that much because we always set the [ ] remove source branch checkbox in Gitlab for every merge request. That means that origin branches are usually cleaned up pretty well.

git branch --merged helped me more to see the branches that I could really clean up.

I still did git logf (with my alias
logf = log --graph --pretty=format:'%Cred%h %C(yellow)%d%Creset %s %Cgreen(%cr) %C(cyan)<%an>%Creset' --all to show the tree structure of the git repo) and then searched through it with /[branch-name] to make sure the branch had really been merged.

Maybe I am just paranoid about deleting stuff from git. But in my opinion, the whole version control thing is there to protect me from myself and protect me from mindlessly doing stupid things.


GitHub UI is where I usually check / remove stale branches. It shows if branches have unmerged commits so you don’t accidentally remove something that’s not merged yet.

My remote is in pretty good shape, as we have a habit of removing branch after merging a PR. But local repo needs more cleanup as there seems to be over 200 old branches :flushed:


My local machine had about 100 old branches (bye, Felicia!) but even better were all of the old ones that needed to be deleted on Github. We try to delete branches as we merge but some of them continue to lurk. I do like the commands we were supplied today.

Thanks for the great first week, Ben! 5/5 completed! :tada:

1 Like

I like ending on an easy win!

I have a script that I run regularly called git super-prune that does two things:

  1. Delete all my locally merged branches
  2. Delete all the remotes branches that no longer exist… remotely

I’ve found this piece of tidiness most helpful over the last few years.


Some great snippets and scripts here - thanks!

I have an alias for a snippet that removes all merged local branches, which has been helping me out. It doesn’t, however, solve the issue of local branches that have diverged from what was merged because of being rebased - I’ll look into automate that lookup process. Simply looking for the last commit message on the branch in the main branch should find a most of them.

I didn’t prune the origin remotes either before, so that removed 352 remote references :blush:

I took some time to go over all the local branches that were left (this includes the rebase-diverge branches) and ended up reducing them from 58 to 12, most of them being currently active and the others being experiments that I don’t want to get rid of just yet. The oldest was from the beginning of the project, almost 3 years ago.

Awesome first week! :+1:

This is a great exercise that my team actually does at the end of every quarter, so there wasn’t much for me to do this time. But the scripts shared in here will definitely make it go faster next time we do it!

My coworkers have a bad habit of not killing PR branches after merging the PR, so we had over 160 branches on one of our microservice repos. I turfed many of them down to about 47.

As an aside, I have a git alias that does a fetch --prune which I regularly do on all repos I work with, so step 2 did nothing for me. :slight_smile: This is in my .gitconfig under [alias]:

fp2 = !sh -c 'git remote | xargs -n 1 git fetch -v --prune $1'

Note the git remote as I often work with multiple git remotes for a single checked out repo, so this alias iterates over them all.

1 Like

I use this git alias to delete all branches that are merged into master or next. This might help automate your workflow. However, it doesn’t help with pruning remote tracking branches.

# .gitconfig
  # Delete Merged: Remove branches that have already been merged with master or next
  dm = "!git branch --merged | grep -v '\\*\\|next\\|master' | xargs --no-run-if-empty --max-args=1 git branch --delete"
1 Like

I tend to be good about this, only 2 branched removed for me.

Deleted a handful and back down to one page of branches in the yours tab in github!

1 Like

I did this for the first time back in September, and pruned 449 branches. This time, 81 branches that have existed in the meantime.

1 Like

Pruned 19 branches down to 3. Definitely a big win!

1 Like