Post

Git House Cleaning

If you’re using git, most likely a lot of branches are being created. Some of them are pushed to remote and finally merged into the main branch. Branches are very lightweight in git (they’re just pointers to commits) and branching is highly encouraging.

However there is a time to cast away stones, and a time to gather stones together. While creating branches is a joy, removing them is a routine. What is worse is that every pushed branch multiplies to 3 (three) entities that should be removed some day. Those are:

  • local branch (my-branch @ your repo)
  • remote branch (my-branch @ remote server)
  • ref to remote branch (origin/my-branch @ your repo)

Each of those beasts has unique way of passing away. Let’s see.

Local branches can be removed via git branch -d or git branch -D. The difference is that former would not remove a branch that has not been merged into your HEAD (or the branch you’re currently on, if you wish). But you can shout at git using capital D and then branch will be gone.

Next in our list is a branch on the remote server. Usually it gets removed by clever monkeys living in smart software. For instance, when you merge pull request in github/gitlab/bitbucket, latter can delete that branch for you. But you still can do it yourself. The git command is not very intuitive, but you’ll get used to it after few months or so. Here it is: git push origin :my-branch. Yes, we’re pushing the branch, but when it’s prepended with colon, it means I want to remove that branch on the server. I have no idea where it comes from and now everyone in internet says that you should use git push origin --delete my-branch, but using colons is so much more fun…

The last item (refs to remote branchs) is easy. It has been covered in post about git tricks – you can use --prune to remove remote refs during git fetch or git pull.

Okay, we covered all delete scenarios and looks like that both remote branches and refs to them usually are being removed automatically. Your only area of concern is removing local branches having been merged. You can do it manually if you’re not laze or you can use the script. My favorite is this one:

git branch --merged HEAD  | `
  ? { $_ -notmatch 'develop' -and $_ -notmatch 'master'  } |`
  %{git branch -d $_.ToString().Trim()}

It gets all merged branches that are neither master nor develop and terminates them with git -d.

I have this script in my home folder and from time to time, when I want to do some destruction, I invoke it like ~\clean-branches.ps1 and see how branches are dying.

This post is licensed under CC BY 4.0 by the author.