Day 5 - Trim your branches

Nice to clean up the tracking branches! Also took the opportunity to review some old branches from a colleague who has since left and dropped a couple of them.

me too. Had about 45! didn’t know about --prune

This felt more like a win than yesterday’s challenge. I didn’t know about --prune either. Lost track counting, more than 10 branches for sure. For cleaning remote branches, I went to github.com. You can see there if there was a PR attached to it and whether it was already merged.

Yay! I’m used to deleting remote branches right after merging/closing pull requests, but my local branches were a mess. Ah, Github stale branches view is also great: https://github.com/YOURCOMPANY/YOURPROJECT/branches/stale

git branch -r | wc -l
   704

Ha!

Our workflow has typically been “develop on a branch & PR, close via squash with issue number in title.” That let’s us go dig into the full commit history of a change and I like that, but I’m starting to think we don’t do that enough to justify the cruft of all those leftover branches.

This is great! Just removed a bunch of dead branches…

Also, while I am it, I find it that often times that are several files that are “temp files”, etc that are not committed and are laying around inside our local code base.

This is great! Just deleted about 70 branches on origin. Until recently, we were not deleting feature branches after merging to develop. We do it now but all the old branches remained on Github. Some of the nifty scripts in the comments here were a great way to quickly review and clean it up. Thank you!

It’s nice to have an easy CQC for the end of the week. We have several repos with hundreds of branches. Many branches were merged over a year ago. For today I just focused on cleaning up my branches in 2 repos both locally and in GitHub.

Perhaps next month, with permission of course, I can start deleting branches that are over a year old. I did a Google search for “delete branch over one year old” and found a few different posts with different levels of safety and autonomy. I would like to revisit this.

Yea, this is a great starting place! I found two old branches of mine this way and deleted them.

A git command that I often run to find my recent branches is:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'

Which will output something like:

2021-05-06 master
2021-05-06 remove-unused-script
2021-05-04 ie11-todo-task
2021-05-03 readme-updates-min-ver
2021-04-23 select-kit-is-disabled

If you use Ruby, you can install a gem I made for this a long time ago with gem install branches and then you can just run the branches command instead of trying to remember the above git command.

We had about 130 stale branches in my job project, Today we deleted a lot of these branches now was have 80 stale branches and we will continue with this cleanup.

3 Likes

Fortunately we already have a bit of tooling to delete stale branches (or ones that match previously merged/closed PRs), but I’m still glad to have learned new git commands, especially git remote prune origin which did a bit of cleaning in my local tracking branches.

It was also worth giving a look at remote branches as I ended up deleting 8 of them :slightly_smiling_face:

1 Like

I meant to post this Friday, but my wife got her 2nd Covid shot and was down for the count!

Anyway, this was a good one… I’m the sole developer, so keep a pretty tight lid on this, BUT I have a desktop and 2 laptops. So it felt good to clean up all the machines I had!

1 Like

Phew … just did a 5 x 20(+) minutes binge fest of last weeks tasks as I was on vacation and just didn’t want to get sucked into work by doing this (too tempting to totally toss the 20 minute rule too, when you’re off!)

Great exercises, thank you! And I’m embarrassed to say I had no idea about the whole purging of tracking branches thing … I deleted about a million! lol

1 Like

I’ve used git prune --dry-run --verbose --expire=now so that I displayed all objects taken by the prune and forcing expiration of those that are past. I had 4 blobs in total.
Also because it’s a personal project I intentionally didn’t delete any of the branches I no longer use. It’s for keeping a personal progress. :nerd_face:

There were branches that required pruning locally.
It also reminded me to clear up origin, which had gotten cluttered with branches that were undeleted once a PR was merged in.

I went through this process with some of my most common repos and found not many branches since I have GitHub set to delete branches once they’re merged into the main branch. However, I did clean up a few I had abandoned a while ago, so progress was still made!