Here are some common Git branching strategies:
- Trunk-Based Development Strategy
- Feature-based strategy
- Gitflow strategy
- Trunk-Based Development Strategy
- main or trunk: This is the only active branch. Developers create very short-lived feature branches or work directly on main, making small, frequent commits.
Workflow:
- Developers work on small features and commit changes directly to main.
- Feature branches are short-lived (often only a few hours or days), reducing merge conflicts.
- Continuous integration tools automatically build and test changes.
- Feature-based strategy
Feature branching is a simple branching strategy where each new feature is developed on its own branch. This approach allows for isolated development and testing of features, making it easier to roll back changes if necessary. Feature branches are created off the master branch, and when the feature is complete, it is merged back into the master branch.
Feature branching is a great option for smaller projects or teams that want a straightforward branching strategy.
- main (or master): Contains production-ready code.
- Environment branches or Develop branch: These branches represent different stages of deployment, such as staging, production, or testing.
- Feature branches: Work is done on individual feature branches, which are merged into environment branch (or develop branch) for testing and deployment.
- Feature branches are created from the main or staging branch (or develop).
- After development, the feature branch is merged into an environment or develop branch for testing.
- Once tested, the code is merged into main (production).
- Gitflow branching strategy
Gitflow is a popular branching strategy that uses two main branches: master and develop.
The master branch contains production-ready code, while the develop branch contains the latest development code. Feature branches are created off the develop branch, and when the feature is complete, it is merged back into the develop branch. When the develop branch is ready for release, it is merged into the master branch, and a new release is created.
Gitflow is a robust branching strategy that works well for large projects with long release cycles, but it can be complicated to set up and maintain. It also requires strict discipline around feature branch creation and merging.
Workflow:
- Start with develop for ongoing development.
- Create feature branches for new functionality.
- Merge feature branches into develop when completed.
- Use release branches for preparing stable versions.
- Fix critical issues on hotfix branches and merge back into both master and develop.