GitFlow is a branching model for Git that helps manage the development, release, and maintenance of your codebase. It introduces two main branches develop and master along with additional branches for features, releases, and hotfixes. This model allows you to separate the code for development, staging, and production, making it easier to manage releases and bug fixes.
Branching Strategy
Master/Main Branch: This branch represents the production-ready state of your code. All commits to this branch are considered releases.
Develop Branch: This branch serves as the integration branch for features. It contains the latest development changes and is used to prepare for the next release.
Feature Branches: Branches created from develop for developing new features. These are merged back into develop once complete.
Release Branches: Branches created from develop when preparing for a release. These branches allow for final testing and bug fixes before merging into master.
Hotfix Branches: Branches created from master for urgent fixes that need to go into production immediately. Once fixed, these are merged back into both master and develop.
Branching and Merging Workflow
1. Starting Development
Create a Feature Branch:
Branch off from develop.
Naming convention: feature/feature-name
Example command:
git checkout -b feature/add-login-page develop
Work on the Feature: Commit changes to your feature branch.
Merge Feature Branch into Develop:
When the feature is complete and tested, merge it back into develop.
Example command:
git checkout develop && git merge feature/add-login-pag
Delete the feature branch if no longer needed:
git branch -d feature/add-login-page
2. Preparing for a Release
Create a Release Branch:
Branch off from develop
.
Naming convention: release/version-number
Example command:
git checkout -b release/1.0.0 develop
Finalize the Release:
Perform final testing and make necessary adjustments.
Commit changes to the release branch.
Merge Release Branch into Master and Develop:
Merge the release branch into master to finalize the release.
Example command:
git checkout master && git merge release/1.0.0
Tag the release:
git tag -a v1.0.0 -m "Release version 1.0.0"
Merge the release branch back into develop
to incorporate any fixes.
Example command:
git checkout develop && git merge release/1.0.0
Delete the release branch if no longer needed:
git branch -d release/1.0.0
3. Hotfixes
Create a Hotfix Branch:
Branch off from master
.
Naming convention: hotfix/issue-description
Example command:
git checkout -b hotfix/critical-bug master
Apply the Hotfix:
Make the necessary fixes and commit changes to the hotfix branch.
Merge Hotfix Branch into Master and Develop:
Merge the hotfix branch into master to deploy the fix immediately.
Example command:
git checkout master && git merge hotfix/critical-bug
Tag the hotfix:
git tag -a v1.0.1 -m "Hotfix version 1.0.1"
Merge the hotfix branch back into develop to ensure that the fix is included in future releases.
Example command:
git checkout develop && git merge hotfix/critical-bug
Delete the hotfix branch if no longer needed:
git branch -d hotfix/critical-bug
Example Workflow
Creating and Merging a Feature
## Start a new feature
git checkout develop
git checkout -b feature/new-dashboard
## Work on the feature, then commit
git add .
git commit -m "Add new dashboard feature"
## Merge feature into develop
git checkout develop
git merge feature/new-dashboard
## Delete the feature branch
git branch -d feature/new-dashboard
Preparing and Releasing
## Start a release
git checkout develop
git checkout -b release/1.0.0
## Finalize the release, then commit
git add .
git commit -m "Prepare release 1.0.0"
## Merge release into master
git checkout master
git merge release/1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
## Merge release into develop
git checkout develop
git merge release/1.0.0
## Delete the release branch
`git branch -d release/1.0.0`
Handling a Hotfix
## Start a hotfix
git checkout master
git checkout -b hotfix/critical-bug
## Apply the hotfix, then commit
git add .
git commit -m "Fix critical bug"
## Merge hotfix into master
git checkout master
git merge hotfix/critical-bug
git tag -a v1.0.1 -m "Hotfix version 1.0.1"
## Merge hotfix into develop
git checkout develop
git merge hotfix/critical-bug
## Delete the hotfix branch
`git branch -d hotfix/critical-bug`
Best Practices
Regularly Pull Changes: Regularly pull from develop and master to keep your branches up-to-date.
Use Pull Requests: Use pull requests for merging branches to ensure code reviews and discussions.
Keep Commits Small: Make small, incremental commits with descriptive messages.
Tag Releases: Tag releases in master to keep track of versions and facilitate rollbacks if necessary.
Summary
Transitioning to a GitFlow approach with develop and master branches will improve your development workflow by clearly separating the stages of development, release, and hotfix management. By following this branching strategy, you can manage features, releases, and urgent fixes in a structured and efficient manner
Top comments (0)