Branching and merging best practices in Git

Gaui picture Gaui · Jul 5, 2014 · Viewed 63.7k times · Source

We have a developer team of 4 and have recently moved to Git. We want to learn best practices regarding workflow with branching and merging.

We are using a lightweight version of Git Flow. We have a dev, staging and a master branch which are all linear with each other.

  • staging is branched from master
  • dev is branched from staging

On top of that we use feature and hotfix branches to work on new features and fix bugs.

I have the following questions:

  1. Should we branch feature branches from dev or from master?
  2. When a feature branch is ready, should we merge the feature branch into dev, then merge dev into staging, or merge the feature branch into staging and then the feature branch into master?

I think we should branch from master and merge the feature branch up, because there might be something in dev that we might not want to merge to staging and master.

What is your opinion? What are the best practices?

Answer

quarterdome picture quarterdome · May 14, 2015

While Git Flow is an excellent branching model, the questions you are asking are a symptom of a bigger problem: Git Flow is too heavy for a small team working on a consumer web product (I am making an assumption that you are working on consumer web product, feel free to ignore if you are coding nuclear power plant control room).

I would like to encourage you to consider Continuous Deployment (CD) with an extremely simple branching model:

Master -> Branch

It is very easy to setup CD nowadays:

  1. Use Travis, CodeShip, Jenkins or similar system to run a full build and test suite on every commit pushed on every branch of your codebase
  2. Setup Travis/Codeship/Jenkins to deploy to production every commit to master that passes the tests.
  3. Create a new branch from master for every new feature.
  4. Code a new feature and test it on a branch.
  5. Merge a feature branch into master, and watch it go live.

There are a lot of common objections to it, that all can be summarized as "but what if I introduce a bug?!". The answer is "You'll fix it!". If you write tests, if you monitor your production site, if you do code reviews, if you practice pair programming, if you use feature flags, and if you keep your features small, then the benefits you get from CD will outweigh the occasional problems any day.

I encourage you to try. It will free your mind to focus on what truly matters: building a great product! If you do not believe me, take a look at this excellent presentation from Github.