We make heavy use of puppet environments in our workflow. Using r10k, git branches are magically mapped to environments. This allows a process where anyone one the team can individually work on a new feature or change, and then we can collaborate and review/revise/test in a controlled manner. We can rebase to the production branch, and use the diff output as part of our change-management documentation. Once the change is merged, however, sometimes the original branch is not deleted.

In doing a puppet-server migration, I decided to clean up some of these “forgotten” branches. Not all branches end up being merged, though; sometimes we will prove out a change, decide not to implement it in production, but want to keep it for future reference. Or, maybe the change is still a work in progress.

To list the remote branches that have already been merged (into the production branch):

$ git branch -r --merged production
  remotes/origin/HEAD -> origin/production

These can be deleted. I can never remember the syntax for deleting a remote branch, so I use a nuke alias that deletes both the local and remote branches:

$ git config --get alias.nuke
!sh -c 'git branch -D $1 && git push origin :$1' -
$ git nuke new_feature_foo
Deleted branch new_feature_foo (was 0b43193).
remote: Deploying to puppetmaster.
remote: r10k new_feature_foo environment removed, will be purged on next run.
remote: Sending notifications.
remote: Mirroring to GitHub.
remote: To git@github.com:/bowdoincollege/noc-puppet
remote:  - [deleted]         new_feature_foo
To git.bowdoin.edu:/noc/puppet.git
 - [deleted]         new_feature_foo

Alternatively, you may want to see all branches that have not been merged (yet) into the production branch:

$ git branch -a --no-merged production
  remotes/origin/HEAD -> origin/production

I took a look at these and cleaned up what I could, but there were a few from other team members that I didn’t know about. I asked everyone to take a look and clean up any unneeded branches/environments. After they had deleted the remote branches, I was able to clean up my local repository:

$ git remote prune origin
Pruning origin
URL: git@git.bowdoin.edu:/noc/puppet.git
 * [pruned] origin/arubaguestportal
 * [pruned] origin/freeradius_arubalab
 * [pruned] origin/freeradius_nopersontype
 * [pruned] origin/opsview_mitsubishiups
 * [pruned] origin/wlcforrancid

This deletes (prunes) any local tracking branches that no longer exist on the remote.