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!

Run this on my day work iOS app.

git branch -r | wc -l
  2467

:scream:

I’ve added a weekly reminder to my calendar to go through this exercise every Friday morning :slight_smile:.

Any ideas on how to automate this further? :thinking: Maybe a Git hook that warns you after a certain threshold is reached?

Are you using GitHub?

I have cleaning up extra branches as part of the process of merging feature branches, still, there was one dangling branch and BOOM! it’s gone. I have to admit I didn’t know about git remote prune origin. I would always do it manually. TIL! :grin:

My main project only has 4 branches for working with feature-branches is very tricky with database development and therefore we commit to mainline at least once a day.
I went through all the other projects and did a bit of cleanup - the most things to clean were on my open source contribution project :smiley:
Great task though, learned something new

We don’t use Git, so done! Sadly, we don’t use any source control at the moment at our shop. We’re working on bringing in SVN.

You can add the following to your gitconfig and it will prune remote branches every time you run git fetch

[fetch]
  prune = true
4 Likes

The gitconfig tip is a good one. I had an alias set up in mine:

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

With this a git fp2 would fetch & prune for all remotes in the repo, but I kinda like the idea of always pruning on fetch.

One other tip: I have an alias for removing all branches that have been merged into the current branch.

cleanbranches = !sh -c 'git remote | xargs -n 1 git fetch -v --prune $1 && git branch --merged | egrep -v master | egrep -v develop | xargs git branch -d'

With this I’ll often go into a checked out copy of a repo, check out the master branch, and do a git cleanbranches which will remove local branches that have been merged into master.

In any case, for this challenge I deleted a handful of remote branches on one of our repos, and then turned to another of our repos which had ~45 branches when I started. It’s now down to 35 (not counting our special branches like master & develop), and I pinged my teammates to clean up some of the ones that they’d orphaned (most of which had long ago been merged into master).

1 Like

Jokes on y’all, I have often been committing code to master since I’m the only one on my team for many projects. So I have nothing to trim, though this is probably a symptom of a different problem.

I’ve been recently added to the largest collaborative project I’ve been a part of, and I’ve been forced to adopt a feature branch workflow which has been very good and I’ve been learning a lot. I want to get better at this style.

Yes :slightly_smiling_face:.

Thanks for the tip @brendan. Worked like a charm.

This is what I love about the CQC, there’s always something new to learn :sparkles:.

If you merge your branches on GitHub, there’s a button to delete the branch after merging. I almost wish it was a single “merge and delete” button, but still, it’s only one more click.

:ok_hand: Right.

That combined with @brendan’s tip for the .gitconfig and the amount left over branches should stay under control.

This is an easy one for me, as i’ve always been pretty adamant in trimming the branches… I have some bash scripts which I shared on previous run of cqc: Day 5 - Trim your branches

I cut a few branches earlier this month when this came up, but a coworker over the weekend found we had upwards of 700 old branches (some experiments, most were past merges over the life of the project).

We’ve cut that to ~500 just this morning and we’re adopting a “trim as you go” approach for the future to keep things from getting unwieldy again.

1 Like

“Trim as you go” is the best approach :+1:.

If you use GitHub there’s now a button that appears in the PR page after a merge to delete the branch straightaway.

If you use Bitbucket there’s also a “Delete source branch after merging” checkbox which can help as well. Unfortunately there’s no way (or at least none that I can see) to force that box to be checked by default.