Day 7 - 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

Easy win, I clean up all my branches after every remote merge of a PR :slight_smile:

From my bash profile (should still work, I’m not maintaining this repo anymore since I moved it to a private one because I added shortcuts for non-public projects):

Warning: I’m far away from being a bash or git expert! There is a better way for sure :slight_smile:

1 Like

I have a git cleanup command that does this.

git gc is also an useful thing todo when pruning.

1 Like

Easy since we use Gerrit and hence no common Git patterns with branches, but did manage to delete one remote. (And not a fan of Gerrit)

Done. I generally delete branches after they’ve been merged into main, but there are still a lot of extra ones hanging out there. I got rid of a couple dozen or so.

That was an easy win since we usually clean them up as we go. If you are using GitHub, I recommend you turn this option in settings:

You can also find out stale branches on https://github.com/<owner>/<repo>/branches/stale. It’s a nice way to pop discussions around old branches that may be POC that never got shipped.

Got to this one :+1: I had a bunch of stale branches pushed up that had some experimental changes that I wanted CI to chew through :broom: