Source control is a must-have for any team working on a project. It provides so many things, including accountability, code reviews, blah blah, and so much more! IMO, the best part is working in branches that can be merged to other branches. In a typical project you have 2 main branches: master and develop. Master should reflect what is in production and develop is the current updates slated to go live at some point in the not too distant future. Beyond those main branches, you may have a hotfix branch with a bug fix that needs to be pushed ASAP, or a feature branch with some new feature. All of those branches can get tangled and complicated. Enter git flow.
Git flow is a command line tool to help with branches and branching for git repos. It’s really simple to setup gives you a few tools that make merging branches to master easier! To get started, install git flow and then run this in your repo root:
git flow init
This init process will ask some simple questions, most can be left as the default unless you have specific naming conventions for branches. Once that is done, you can do any of the following.
Create version 1.0.0
Let’s say you’d like to merge develop into master and tag it as version 1.0.0. Perform the following commands:
#Start Release 1.0.0, merging develop into master git flow release start 1.0.0 #Update your version file vim version #Commit the version file update as the 'Version Bump' git add version git commit -m 'Version Bump' #Finish Release 1.0.0 git flow release finish 1.0.0 #Push it, including tags git push --follow-tags
When creating a new feature that may not go to production for a while, you should do the work in a feature branch.
#Create a new feature branch from develop git flow feature start report-xyz #Make changes and commit vim report-xyz git add report-xyz git commit -m 'Adding report-xyz' git push #Finish the feature, merging it into develop #This can also be done via a PR for code reviews git flow feature finish report-xyz
There’s a bug on production! Going to have to fix it, based on the code in master. Once it’s fixed both master and develop need to have that update.
#Create a new hotfix branch from master git flow hotfix start bad-profile-image #Make your changes vim profile git add profile git commit -m 'Fix for profile image' git push #TODO Verify that all is well #Once all is good, do a 'Version Bump' vim version git add version git commit -m 'Version Bump' #Finish the hotfix, merging it into develop and master #This can also be done via PRs for code reviews git flow hotfix finish bad-profile-image
That’s all for today. Happy Coding!!