Starting from scratch: GitFlow detailed tutorial, easy to master branch strategy

prologue

GitFlow is a model for managing software development workflows in Git repositories. It provides a structured approach to handling feature development, version releases, and maintenance. The following is a detailed GitFlow tutorial to help you understand the basic concepts and usage of GitFlow.

Install GitFlow

First, make sure you have Git installed. GitFlow can then be installed via:

Windows
uses Git for Windows, which already includes the GitFlow tool.

macOS
can use Homebrew to install GitFlow:

brew install git-flow-avh

Linux
uses a package manager to install GitFlow. For example, on Ubuntu you can use the following command:

sudo apt-get install git-flow

Initialize GitFlow

Once GitFlow is installed, you can initialize it in your Git repository:

git flow init

This command will walk you through the initialization process, including choosing a branch naming convention and basic setup of the branch.

GitFlow branching overview

GitFlow defines several different types of branches, each serving a specific purpose:

  • Master branch: The master branch is used to store stable, production-ready code. Typically, only released code is merged into the master branch.

  • Development branch (develop): The development branch is the main development branch and contains the development work currently in progress. All new features and bug fixes should be created from the develop branch.

  • Feature branch (feature): Feature branch is used to develop new features or add new feature modules. Typically created from the develop branch and merged back into the develop branch when complete.

  • Release branch (release): The release branch is used to prepare a release version. You can do final testing and fixes on this branch before releasing. Release branches are usually created from the develop branch and merged back into the develop and master branches after release.

  • Hotfix branch: Hotfix branch is used to urgently fix problems in the production environment. They are created from the master branch and merged back into the master and develop branches after fixes.

Using GitFlow

Once your Git repository has GitFlow initialized, you can use it by following these steps:

Create a feature branch

git flow feature start feature-name

This will create a new feature branch and switch you to it so you can work on it.

Completing the feature branch
After completing feature development, you can merge the feature branch back into the develop branch:

git flow feature finish feature-name

Creating a Release Branch
When you are ready to release a new version, you can create a release branch:

git flow release start release-version

Completing the Release Branch
After the final testing and fixes have been made on the release branch, the release branch can be finalized:

git flow release finish release-version

Create a hotfix branch
If a problem is discovered in the production environment, you can create a hotfix branch:

git flow hotfix start hotfix-name

Completing the hotfix branch
After fixing the problem, you can complete the hotfix branch:

git flow hotfix finish hotfix-name

Sample workflow

Here is a simple GitFlow workflow example:

  • Create a new feature branch to develop a new feature:
git flow feature start my-feature
  • Develop on a feature branch and commit your changes:
# 进行代码更改
git add .
git commit -m "Add new feature"
  • Complete the feature branch and merge the changes back to the develop branch:
git flow feature finish my-feature
  • Create a new release branch to prepare for release:
git flow release start 1.0.0
  • Test and fix on the release branch to ensure the code is stable:
# 进行测试和修复
git add .
git commit -m "Fix bugs"
  • Complete the release branch and merge the changes back to the develop and master branches:
git flow release finish 1.0.0
  • If a problem is discovered in the production environment, a hotfix branch can be created to resolve the issue:
git flow hotfix start hotfix-1.0.1
  • Make the fix on the hotfix branch and then finish it:
# 进行修复
git add .
git commit -m "Fix critical bug"
git flow hotfix finish hotfix-1.0.1

This is just basic usage of GitFlow, it can be customized and extended according to the needs of the project. GitFlow provides a structured method to manage the development and release process of a project, helping teams better collaborate and manage code.

Guess you like

Origin blog.csdn.net/weixin_40808668/article/details/132642385