Day 5 - Trim your branches

Nice way to close out the week!

I recently set up a new user on my laptop and still had a handful of old branches around. I’m sure my old user would have been way higher.

I went through the GitHub interface to prune old branches, but definitely need to look through some of the scripts and one-liner suggestions in this thread.

Killed about 25 local branches. Pruned 71 down to 14. Easy and quick win.

I am actually curious, I’ve always been warned to keep branches around a while after merging in case … something happens? In the 6 years I’ve been working professionally, I’ve see some pretty teeth clenching reverts and rollbacks, but never anything that required the old, already merged branch to be around. So I deleted my coworkers merged branches too. Is that taking it too far?

I normally just check with git branch -vv to directly see which branches are gone upstream, as the screenshot shows:

But when I’m confident about the output I run both following commands (with their respective aliases):

git remote prune origin
git branch --merged | xargs git branch -d

At the consul repo I’ve 4 fork’s remotes so I run them on a daily basis as we normally checkout branches locally before merging PR’s

1 Like

I try to keep branches at minimum as soon I merge it. So no changes in my project (yes it’s a little project with a few commits yet but… :wink:)

Removed 7 out of 11 local branches and 10 out of 16 remote branches. The rest are used for special versions.

Whenever I work on windows, I prefer using TortoiseGit to have a look at the branches (last commit date and messages) before deciding to delete branches.

Week 1 summary:

  • Great experience! Thanks @ben!
  • I already see the benefits of the challenges both from the code-base perspective and from the commitment on my part to dedicate a bit of time, every day, 1st thing in the morning, to all those quality tasks that always seems to be pushed back…
  • I also find that 20 minutes/day are enough to have a taste of each challenge but I need around an hour to fully participate and expend a bit on each topic.
  • I find that doing the CQC as the first thing every day is key for me. Looking at emails before starting the challenge tend to drag me into answering emails that makes starting the daily challenge much more difficult…
2 Likes

I am so behind! Going 2 a days until I catch up :smiley:. I didn’t have too many local branches needing trimming. My biggest problem is we have LOTS of remote branches that are merged but undeleted, or stale. I wasn’t ever able to come up with a way to get them all in one fell swoop.

I used

git branch -r --merged | 
grep origin | 
grep -v '>' | 
grep -v master | 
xargs -L1 | 
cut -d"/" -f2- | 
xargs git push origin --delete

which helped, but didn’t get them all. but not all. got unable to delete 'branchtype/branchname': remote ref does not exist for quite a few.

The Github stale tab on the branches page was a huge help as well. https://github.com/org/repo/branches/stale

Any suggestions on a full way to eliminate all remote merged branches?

Every time we merge a branch on github we use the “delete branch” button. Thanks github! As a result, I end up with a bunch of branches that I have locally but do not exist on remote.

Like many of the others posting here, I use this bash script regularly to clean up my local branches:

#!/bin/bash
echo "Checkout master"
git checkout master
echo "Deleting local copies of merged branches"
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Anyone know how to programatically remove stale branches?