青江的个人站

“保持热爱,奔赴星海”

  • 主页
  • 目录
  • 图床
  • 留言板
  • -关于我-
友链 搜索文章 >>

青江的个人站

“保持热爱,奔赴星海”

  • 主页
  • 目录
  • 图床
  • 留言板
  • -关于我-

自动化插件git-Flow的基础使用教程(配合Fork实操)


阅读数: 0次    2025-09-02
字数:2.6k字 | 预计阅读时长:11分钟

Git Flow是一种标准Git分支模型,阅读本文前,建议先阅读文章“标准Git分支模型介绍(配合Fork实操) | 青江的个人站”以了解Git Flow的基本流程。

可以看到,Git Flow流程中有着大量的新建、提交、合并分支的操作,导致整个过程非常繁琐,加大了开发的工作量。

因此,Git Flow的创造者Vincent Driessen开发了git-flow插件(nvie/gitflow: Git extensions to provide high-level repository operations for Vincent Driessen’s branching model.),可以通过一行命令来自动化地创建或完成支持分支,从而简化操作。

此外,Fork支持git-flow,可以在图形界面中完成整个流程。

本文将配合Fork对git-flow插件的使用做基础的介绍。

值得一提的是,Fork有一个非常好用的功能,当点击Fork页面上方信息输出框的左下角的“Activity Manager”时,弹出的窗口中可以看到所有历史操作,点击每一个操作即可看到进行此操作时后台实际运行的命令以及运行后的控制台输出信息。

1

初学者可以尽量在进行一些操作后在此查看后台运行的命令,有助于提升对Git命令行的理解。

1. 简介

git-flow并不是Git本身的功能,而是一个高级命令行插件。它为Git提供了一套标准的命令,来自动化执行基于功能、发布和热修复分支的协作工作流程。

例如,创建特性分支可以使用git flow feature start xxx,完成特性分支可以使用git flow feature finish xxx,运行命令后,git-flow会自动完成创建分支、分支合并等操作,不需要像上一节那样一步步手动操作,减少工作量。

2. 安装

对于Windows,在Git官网直接下载安装的Git已经默认包含了git-flow插件,可以在Git Bash中运行git flow version来确认,如果正确输出了版本号,则git-flow可以使用。

如果安装了Fork,其内部自带的Git也默认包含了git-flow插件,可以随便打开一个仓库,点击Fork页面右上方的“Console”,在命令行中运行git flow version来确认,如果正确输出了版本号,则git-flow可以使用。

2

1
2
$ git flow version
1.12.3 (AVH Edition)

Linux的安装也比较简单,以Ubuntu为例,直接使用包管理器安装即可:sudo apt install git-flow。

其他系统的安装方法可以参考官方Wiki:Installation · nvie/gitflow Wiki

3. 初始化git-flow

在一个Git仓库中打开Git Bash,运行git flow init,即可初始化git-flow插件。

出现的选项可以选择或设置每一种分支的分支名格式,一般主分支为main,其余分支保持默认,直接回车确认即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ git flow init

Which branch should be used for bringing forth production releases?
- develop
- main
Branch name for production releases: [main]

Which branch should be used for integration of the "next release"?
- develop
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Bugfix branches? [bugfix/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
Hooks and filters directory? [F:/Work/Gitea/hanqingjiang/test/.git/hooks]

Fork支持git-flow,因此可以直接在Fork中完成git-flow的初始化和各种分支操作。

在Fork中点击Repository-Git Flow-Initialize Git Flow。

如果想要清除git-flow的初始化,可以在同样的菜单中点击“Deinitialize Git Flow”。

3

在弹出的窗口中即可配置初始化git-flow。

同样的,一般主分支为main,其余分支保持默认即可。

4

4. 特性分支(Feature branches)

4.1 新建特性分支

假设新特性名称是feature_3,在命令行中运行git flow feature start feature_3,即可自动创建名为feature/feature_3的分支。

1
2
3
4
5
6
7
8
9
10
$ git flow feature start feature_3
Switched to a new branch 'feature/feature_3'

Summary of actions:
- A new branch 'feature/feature_3' was created, based on 'develop'
- You are now on branch 'feature/feature_3'

Now, start committing on your feature. When done, use:

git flow feature finish feature_3

打印输出很清晰,现在可以在新分支上开发新特性了。

在Fork中,点击右上角新建分支按钮右边的箭头展开选项,点击“Git Flow”中的“Start Feature…”,即可创建新的特性分支。

5

在创建特性分支窗口,直接填写特性名即可,Fork会自动创建名为feature/feature_3的分支。

6

在“Activity Manager”窗口中可以看到执行的命令和输出,输出与命令行基本相同,此后不再赘述。

7

4.2 开发新特性

自动化创建分支后,即可在这个新的分支上开发新特性了。

此处简要模拟新特性的开发,在特性分支上创建若干个Commit。

8

4.3 完成特性分支

在命令行中运行命令git flow feature finish feature_3,即可完成当前特性分支。

git-flow会自动合并分支并删除本地特性分支。

1
2
3
4
5
6
7
8
9
10
11
12
$ git flow feature finish feature_3
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
Merge made by the 'ort' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
Deleted branch feature/feature_3 (was 904a908).

Summary of actions:
- The feature branch 'feature/feature_3' was merged into 'develop'
- Feature branch 'feature/feature_3' has been locally deleted
- You are now on branch 'develop'

在Fork中,点击右上角新建分支按钮右边的箭头展开选项,点击“Git Flow”中的“Finish ‘feature/feature_3’…”,即可完成当前特性分支。

9

与Git Flow的思想相同,完成分支的时候尽量选择“No fast-forward”,以保留特性分支历史存在的信息。

10

经过测试,如果不选择“No fast-forward”,git-flow也会在一个特性分支包含多个提交时候默认使用--no-ff选项进行分支合并,但当一个特性分支仅包含一个提交时,git-flow将采用“fast-forward”合并分支,这就会导致特性分支历史丢失。

参考nvie在一个Issue中的评论:feature finish not doing a --no-ff merge · Issue #100 · nvie/gitflow

因此任何合并都建议勾选“No fast-forward”选项。

至此,一个特性分支已经完成,点击Push即可将本地的更改推送到远程。

11

5. 发布分支(Release branches)

5.1 新建发布分支

假设新的发布分支的版本号是1.1,则应该在命令行中运行git flow release start 1.1来自动化创建新的发布分支。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git flow release start 1.1
Switched to a new branch 'release/1.1'

Summary of actions:
- A new branch 'release/1.1' was created, based on 'develop'
- You are now on branch 'release/1.1'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:

git flow release finish '1.1'

在Fork中点击“Start Release…”,在窗口中输入版本号,即可创建新的发布分支。

12

5.2 提升版本号或其他发布所需操作

此时可以提升代码中的版本号并提交。或在发布分支上完成所有发布项目所需的操作。

13

5.3 完成发布分支

在命令行中运行git flow release finish 1.1,或在Fork中的Branch菜单中选择“Finish ‘release/1.1…’”,即可自动化完成当前发布分支。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ git flow release finish 1.1
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Merge made by the 'ort' strategy.
README.md | 4 ++++
1 file changed, 4 insertions(+)
Already on 'main'
Your branch is ahead of 'origin/main' by 9 commits.
(use "git push" to publish your local commits)
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
Merge made by the 'ort' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
Deleted branch release/1.1 (was 03c2a27).

Summary of actions:
- Release branch 'release/1.1' has been merged into 'main'
- The release was tagged '1.1'
- Release tag '1.1' has been back-merged into 'develop'
- Release branch 'release/1.1' has been locally deleted
- You are now on branch 'develop'

此时一个特性分支的发布已完成,分别对main分支和develop分支执行一次Push操作即可将本地的更改同步到远程。

14

6. 热修复分支

6.1 创建热修复分支

假设新发布的版本中有一些BUG,我们需要通过热修复来快速解决发布版本中的问题,假设热修复之后的版本号为1.1.1,则创建热修复分支可以在命令行中运行git flow hotfix start 1.1.1。

在Fork中,可以直接点击Branch菜单中的“Start Hotfix…”。

热修复分支默认在main分支上创建。

此处的热修复分支名可以是需要修复的BUG的简称,不一定为版本号。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git flow hotfix start 1.1.1
Switched to a new branch 'hotfix/1.1.1'

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

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

git flow hotfix finish '1.1.1'

6.2 提升版本号并完成热修复

在新创建的热修复分支上,需要先更新版本号并提交。

更新版本号后,即可开始正式的错误修复,将修复内容提交到一个或多个独立的提交中。

15

6.3 完成热修复分支

热修复完成后,使用git flow hotfix finish 1.1.1,或在Fork中点击Branch菜单下的“Finish Hotfix ‘1.1.1’”,即可自动化完成一个热修复分支。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ git flow hotfix finish 1.1.1
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Merge made by the 'ort' strategy.
README.md | 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 'ort' strategy.
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Deleted branch hotfix/1.1.1 (was 4a2cf18).

Summary of actions:
- Hotfix branch 'hotfix/1.1.1' has been merged into 'main'
- The hotfix was tagged '1.1.1'
- Hotfix tag '1.1.1' has been back-merged into 'develop'
- Hotfix branch 'hotfix/1.1.1' has been locally deleted
- You are now on branch 'develop'

此时,一个热修复分支已经完成,分别对main分支和develop分支执行一次Push操作,即可将所有更改推送到远程。

16

至此,git-flow插件的基础使用已介绍完成,其他进阶的使用方法和高级选项可以参考这篇文章:git-flow 插件使用手册 - song的博客 | Song Blog,或参考官方Wiki:Home · nvie/gitflow Wiki。

本文来源: 青江的个人站
本文链接: https://hanqingjiang.com/2025/09/02/20250902_gitFlowWithFork/
版权声明: 本作品采用 CC BY-NC-SA 4.0 进行许可。转载请注明出处!
知识共享许可协议
赏

谢谢你请我喝可乐~

支付宝
微信
  • Git

扫一扫,分享到微信

微信分享二维码
一种现代化的Git分支模型:Lean Branching
标准Git分支模型介绍(配合Fork实操)
  1. 1. 1. 简介
  2. 2. 2. 安装
  3. 3. 3. 初始化git-flow
  4. 4. 4. 特性分支(Feature branches)
    1. 4.1. 4.1 新建特性分支
    2. 4.2. 4.2 开发新特性
    3. 4.3. 4.3 完成特性分支
  5. 5. 5. 发布分支(Release branches)
    1. 5.1. 5.1 新建发布分支
    2. 5.2. 5.2 提升版本号或其他发布所需操作
    3. 5.3. 5.3 完成发布分支
  6. 6. 6. 热修复分支
    1. 6.1. 6.1 创建热修复分支
    2. 6.2. 6.2 提升版本号并完成热修复
    3. 6.3. 6.3 完成热修复分支
© 2021-2025 青江的个人站
晋ICP备2024051277号-1
powered by Hexo & Yilia
  • 友链
  • 搜索文章 >>

tag:

  • 新年快乐!
  • 生日快乐🎂
  • 小技巧
  • Linux
  • 命令
  • 语录
  • Blog
  • Notes
  • 复刻
  • Android
  • FPGA
  • Homework
  • MATLAB
  • C
  • Server
  • Vivado
  • Git

  • 引路人-稚晖
  • Bilibili-稚晖君
  • 超有趣讲师-Frank
  • Bilibili-Frank