Git Flow | Beginner's Tutorial
Git Flow is a branch model based on Git, designed to help teams better manage and release software.
Git Flow was proposed by Vincent Driessen in 2010, and through a set of standard branch naming conventions and workflows, makes the development, testing, and release processes more orderly and efficient.
Git Flow mainly consists of the following types of branches: master, develop, feature, release, hotfix.
Git Flow Installation
Linux
Debian/Ubuntu:
sudo apt-get install git-flow
Fedora:
sudo dnf install gitflow sudo apt-get install git-flow
macOS
On macOS, you can use Homebrew to install Git Flow:
brew install git-flow
Source Installation
If your distribution's package manager does not have Git Flow, you can also install from source:
git clone https://github.com/nvie/gitflow.git cd gitflow sudo make install
After installation, you can verify whether Git Flow was successfully installed with the following command:
git flow version
Windows
On Windows, you can install Git Flow in the following ways:
- Using Git for Windows: Git for Windows includes Git Flow. You can install Git from Git for Windows, then use Git Bash to use Git Flow.
- Using Scoop: If you use the Scoop package management tool, you can install Git Flow with the following command:
scoop install git-flow - Using Chocolatey: If you use the Chocolatey package management tool, you can install Git Flow with the following command:
choco install gitflow
Git Flow Branch Model
master branch:
- Always maintains a stable and release-ready state.
- Each time a new version is released, it is merged from the
developbranch to themasterbranch.
develop branch:
- Used to integrate all development branches.
- Represents the latest development progress.
- Feature branches, release branches, and fix branches all branch from here and are eventually merged back here.
feature branch:
- Used to develop new features.
- Created from the
developbranch, and merged back to thedevelopbranch after development is complete. - Naming convention:
feature/feature-name.
release branch:
- Used to prepare for the release of a new version.
- Created from the
developbranch, final testing and fixes are performed, then merged back to thedevelopandmasterbranches, and a version tag is added. - Naming convention:
release/release-name.
hotfix branch:
- Used to fix urgent issues.
- Created from the
masterbranch, and merged back to themasteranddevelopbranches after the fix is complete, and a version tag is added. - Naming convention:
hotfix/hotfix-name.
Branch Operation Principles
- Each Commit on the Master branch should be tagged with a Tag, and the Develop branch is created based on Master.
- After the Feature branch is completed, it is merged back to the Develop branch, and the branch is usually deleted.
- The Release branch is created based on Develop, used for testing and fixing Bugs, and after release, it is merged back to Master and Develop, and a Tag is added to mark the version number.
- The Hotfix branch is created based on Master, and after completion, it is merged back to Master and Develop, and a Tag is added. 1
Git Flow Command Examples
- Start Feature branch:
git flow feature start MYFEATURE - Finish Feature branch:
git flow feature finish MYFEATURE - Start Release branch:
git flow release start RELEASE - Finish Release branch: merge to Master and Develop, add Tag, delete Release branch.
- Start Hotfix branch:
git flow hotfix start HOTFIX - Finish Hotfix branch: merge to Master and Develop, add Tag, delete Hotfix branch.
Git Flow Workflow
1. Initialize Git Flow
First, initialize Git Flow in the project. You can use the Git Flow plugin (e.g., git-flow) to simplify operations.
git flow init
During initialization, you need to set branch naming conventions and default branches.
2. Create Feature Branch
When starting to develop a new feature, create a feature branch from the develop branch.
git flow feature start feature-name
After completing development, merge the feature branch back to the develop branch, and delete the feature branch.
git flow feature finish feature-name
3. Create Release Branch
When preparing to release a new version, create a release branch from the develop branch.
git flow release start release-name
Perform final testing and fixes on the release branch, and when ready to release, merge the release branch back to the develop and master branches, and add a version tag.
git flow release finish release-name
4. Create Hotfix Branch
When an urgent fix is needed, create a hotfix branch from the master branch.
git flow hotfix start hotfix-name
After the fix is complete, merge the hotfix branch back to the master and develop branches, and add a version tag.
git flow hotfix finish hotfix-name
Practical Example
Below is a comprehensive example of actually using Git Flow.
Initialize Git Flow:
git flow init
Create and finish feature branch:
git flow feature start new-feature # develop new feature git flow feature finish new-feature
Create and finish release branch:
git flow release start v1.0.0 # testing and fixing git flow release finish v1.0.0
Create and finish hotfix branch:
git flow hotfix start hotfix-1.0.1. # fix urgent issue git flow hotfix finish hotfix-1.0.1
Advantages and Disadvantages
Advantages
- Clear branch model: Clear branch naming and usage rules make the development process orderly.
- Isolation of development and release: Development and release processes are separated, reducing the impact of uncertainties in development on release.
- Version management: Each release and fix is tagged with a version, making it easy to trace back and manage.
Disadvantages
- Complexity: For small teams or simple projects, the Git Flow branch model may seem overly complex.
- Frequent merges: In large teams, frequent branch merges may lead to increased merge conflicts.
Git Flow is a structured branch management model that, by defining clear branches and workflows, helps teams better manage software development and release processes. Although it adds a certain level of complexity, for large projects and team collaboration, Git Flow provides powerful support and management capabilities.
YouTip