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!

When I fetch new changes, I always prune my remote branches with git fetch --prune

And if a local branch doesn’t have a corresponding remote, I delete it


git fetch --prune is gonna be my new default! I have always typed out git remote prune origin regularly :slightly_smiling_face:

Happy Friday! :slightly_smiling_face:

I also have an alias gl='git pull --prune' to prune local branches whenever I pull.

I cleaned up my branches from the UI which makes it easier if you’re not sure what branches you worked on a while ago.

Happy Friday

1 Like

I’m also using git fetch --prune all the time so today’s challenge was an easy one. Found one old branch that could be removed on the remote though.

I cleanup branches on a regular basis and also use --prune. So not much to do today :slight_smile:

Wow, another day of learning. Good to know that it’s nice to prune when do the fetching.

I believe that its also possible to do git pull --prune

But I’d have to check

I didn’t really have local stale branches, because I clean those regularly, but remote it was a mess!
I found 6 year old branches, branches by people who’ve long since left the company.

Feels good to clean those up. I also passed along the message to the team. Thanks, see you Monday!

Just showed this challenge to some of my co-workers. I’m using git fetch --prune, so not much of a challenge today :+1:

Have a great weekend!

Good morning!

I ran through a couple of repos and indeed found a lot of branches. Many of them were from the all-contributors bot I use to add contributors to my README.

Here’s a one-liner to just delete the branches including all-contributors (or any regex, actually!)

git push origin --delete $(git ls-remote --heads origin | grep all-contributors | cut -f 2)


EDIT: for extended Regex usage in grep, use grep -E

1 Like

This is where my imposter syndrome kicks in :sweat_smile: I work with just the master branch, for years now and served me well to be honest. Keeps it all plain and simple and forces me to work on one feature at a time.

I regularly run

git fetch --prune
git cleanup

in my repos.
cleanup is an alias defined like this

cleanup = !git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

So today’s exercise was done relatively quickly. :+1:

I have this wrapped in some git aliases… anytime I want to update the default branch, I run git prunsuka instead of git pull

I have a git purge command that drops local branches that disappeared from remote, and call it from time to time.

This day I’ve opened stale branches list, and trashed everything there

git config remote.origin.prune true to do it automatically.

To clarify, this just makes it where you don’t have to add --prune to your fetch or pull.

If you’re using GitHub, the stale branches view is helpful, too.


I have a lot of local branches. Some of them are needed and cannot be merged. The others were really stale and now are gone. Yay!
I use that gives you quick insights into the branches (both local and remote) if you want to see the diff.

This won’t drop local branches I’m not going to care further :slightly_frowning_face:

I would recommend to do git fetch initially, to ensure, local and remote things are in sync (and no unexpected merges caused by pull happen)

And I have to admit - in some projects I found quite a messy bunch of branches. Thanks for heads up.

Thanks for all the great tips in here.
I am going to have to come back and steal insights from all the comments after CQC completes.

I pruned many local branches in many repos.
Now to do the hard work of merging in stale branches or deleting them.