Skip to main content

GitFlow

Gitflow 是另一种 Git 分支模型,它涉及使用功能分支和多个主分支。

macOS
brew install git-flow-avh
Linux
apt-get install git-flow
Windows
wget -q -O - --no-check-certificate https://raw.github.com/petervanderdoes/gitflow-avh/develop/contrib/gitflow-installer.sh install stable | bash

1

仓库初始化​

这个工作流不是一个 main 分支,而是使用两个分支来记录项目的历史。该 main 分支存储官方发布历史,该 develop 分支用作功能的集成分支。 main 使用版本号标记分支中的所有提交也很方便。

  1. 第一步是 main 用一个 develop 分支来补充默认值。一种简单的方法是让开发人员在 develop 本地创建一个空分支并将其推送到服务器:

git branch develop
git push -u origin develop

这个分支将包含项目的完整历史,而 main 将包含一个删节版本。其他开发人员现在应该克隆中央存储库并为 develop。

  1. 使用 git-flow 扩展库时,在现有 repo 上执行将创建分支:
git flow init develop

# 基于 develop 分支生成新分支 feature/<name>,并切到该分支

=======


```bash

git flow init -f

Branch name for production releases: [main]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []

git branch
* develop
main

解释

master 分支名: master development 分支名: develop feature 分支名:feature release 分支名:release hotfix 分支名:hotfix support 分支名:support version 标签前缀:v

开发​

每个新功能都应该驻留在自己的分支中,可以将其推送到中央存储库 以进行备份/协作。 但是,分支不是从 分支出来,而是 main 用作 feature 它们 develop 的父分支。当一个特性完成时,它会被合并回 develop 。特性不应该直接与 main。

2

  1. 执行下面命令
git flow feature start <name>

建立当前任务的 feature ,如 git flow feature start aa_login,即建立一个分支名为 feature/aa_login,并切到该分支。

操作命令
git checkout -b feature/<name> develop
  1. 在 feature 分支进行开发,如需调试,可 push 到远端

  2. git flow feature finish <name> 结束 feature 分支,并 merge 至 develop

老版本的 git

新版本的 gitflow 只要 finish 之后自动合并到 develop

检查当前 feature 和 develop 分支是否有冲突,有则提示解决冲突,没有则继续以下流程:


git checkout develop

  • 切换到 develop 分支

git merge --no-ff feature/<name>

分支,并保留该分支 git 记录

merge feature/<name>

删除本地 feature 分支

git branch -d feature/<name>

git push 上传 develop 分支

修复​

3

hotfix
> git flow hotfix start 1.9
Switched to a new branch 'hotfix/1.9'

Summary of actions:
- A new branch 'hotfix/1.9' was created, based on 'main'
- You are now on branch 'hotfix/1.9'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

git flow hotfix finish '1.9'

git flow hotfix finish 1.9
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
Merge made by the 'recursive' strategy.
c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.
Merge made by the 'recursive' strategy.
c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Deleted branch hotfix/1.9 (was 83e4746).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'main'
- The hotfix was tagged 'v1.9'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/1.9' has been deleted

git flow hotfix start <name>

基于 master 分支 新建分支 hotfix/<name>

在该分支上进行 hotfix

git flow hotfix finish <name>

结束 hotfix 分支,并 merge 到 master 分支和 develop 分支

  • 具体操作:

      git checkout main
    git checkout -b hotfix_branch
    git checkout develop
    git merge hotfix_branch
    git checkout main
    git merge hotfix_branch

上传 master 分支的修改

git checkout develop

git push

上传 develop 分支的修改

Gitflow 的整体流程
  1. 一个 develop 分支是从创建的 main
  2. 一个 release 分支是从创建的 develop
  3. Feature 分支是从创建的 develop
  4. 当 feature 完成时,它被合并到 develop 分支中
  5. 当 release 分支完成后,它被合并到 develop 和 main
  6. 如果 main 检测到问题, hotfix 则创建一个分支 main
  7. 一旦 hotfix 完成,它就会合并到两者 develop 和 main