A Few Tips To Help Manage Git Branches

Employees working at desks


Managing git branches when branching off multiple feature branches can be a pain. What branches have been merged and can any be deleted?

Within my first few months at Made By Munsters I came to realize that I needed a better workflow for managing my branches.

Let’s break down the current workflow. First, at the beginning of a sprint we create a sprint_deploy branch with the date that it was created. This branch is based off the master, because master has the latest code base. From this point, we branch off of sprint_deploy into several feature branches.

Each feature branch is named according to the user story it is addressing, as well as, time stamped with a creation date. For instance feature_addAdminLogin_7_21_16 will contain code that is primarily used to add the admin login functionality.

From here a PR will be made for this branch. If fixes are needed, then the PR reviewer will branch off the feature branch to create the fixes, otherwise, the feature branch developer will fix any changes that need to be made within this branch.

The reason for the seperation is that another developer modifying code needs to be reviewed just as if it was the original PR. At Made By Munsters we pride ourselves on making sure the work is done correctly.

As stated above, we can see that at any point we may have 5 to 10 branches open on our local environments. Manually managing these branches and checking if the code has been merged can be a pain.

For this I have found a simple command that has saved me a ton of time.

# ~./bashrc or ~./zshrc# removes all branches that have been merged into currently checked out branch# excluding master branchalias remove_merged='git branch --merged | grep -v "\*" | grep -v master | xargs -n 1 git branch -d'

The above code allows me to easily checkout the sprint_deploy branch and run remove_merged within my terminal. Without having to think about it, this will remove all branches that have been merged into the sprint branch except for the master branch.

Inspiration for this came from Git Clean.