Run git branch -r. Marvel at all the old tracking branches that have been left in your local repo.
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.
Re-run git branch -r. Better, right?
Now that your local repo is clean, take a look at the branches on origin by running git ls-remote --heads origin.
Delete any of your branches that are no longer needed with git push origin --delete old_branch.
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!
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
Great task though, learned something new
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).
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.
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.
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.
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.