使用可扩展的git flow建立团队项目工作流

git flow 是什么

Git flow是一种基于git分支模型的工作流程,旨在简化软件开发过程中的合并和发布管理。主要包括以下分支。

分支 功能
master 生产环境的稳定版本
develop 开发分支
feature 功能分支
release 预发布分支
hotfix 紧急修复生产环境的bug
bugfix 修复开发环境的bug

举一个简单的使用案例。

git flow feature start xxx会从develop分支创建一个feature/xxx分支。在新分支上完成功能开发之后使用git flow feature finish xxx令新分支合并到develop分支并删除新分支。这就是一个完整的功能开发流程。

git flow 有什么问题

  • 分支类型固定

只能创建以上几种类型的分支,不一定满足某些开发团队的需求。

  • 没有固定的配置,无法形成团队规范

使用git flow之前需要使用git flow init来初始化。在这个过程中,可以设置master和develop的名字以及其他几种类型分支的前缀。但这些信息都会被保存在.git目录下。也就是说只能在本地作用,无法与团队分享,一旦重新拉取远程仓库,就需要重新初始化。

可扩展的git flow

git-flow-rs是一款遵循配置的,可扩展的git flow替代品。

可以通过cargo install git-flow-rs安装,也可以下载release

以下是可用的命令。

xml 复制代码
Extensible git flow written in rust.

Usage: git flow <command>

Avaliable commands:

-h, --help
	Print help
-v, --version
	Print version
start (<branch_type> <branch_name>)/(<full_branch_name>)
	start a task
finish (<branch_type> <branch_name>)/(<full_branch_name>)
	finish a task
drop (<branch_type> <branch_name>)/(<full_branch_name>)
	give up a task
track (<branch_type> <branch_name>)/(<full_branch_name>)
	track a task
sync remote/local [--override]
	sync branches to remote/local

Configured branch types:

feature
	from dev to dev
  • start: 启动一个任务
  • finish: 完成一个任务
  • drop: 放弃一个任务
  • track: 追踪一个任务
  • sync: 将所有分支同步到远程/本地,加上--override意味着会删除多余的分支

使用之前需要先进行配置,以下是一个案例。

  • 全局配置文件应放在~/.config/git-flow/.git-flow.jsonC:\Users\YourUsername\AppData\Roaming\git-flow\.git-flow.json
  • 项目配置文件应放在<GitRoot>/.git-flow.json
json 复制代码
[
  {
    "type": "feature",
    "name": "feature/{new_branch}",
    "from": "dev",
    "to": [
      {
        "branch": "dev",
        "strategy": "merge"
      }
    ],
    "hooks": {
      "after_start": {
        "command": "git",
        "args": ["push", "origin", "feature/{new_branch}:feature/{new_branch}"]
      },
      "after_finish": {
        "command": "git",
        "args": ["push", "origin", "--delete", "feature/{new_branch}"]
      }
    }
  },
  {
    "type": "hotfix",
    "name": "hotfix/{new_branch}",
    "from": "main",
    "to": [
      {
        "branch": "main",
        "strategy": "merge"
      },
      {
        "branch": "dev",
        "strategy": "merge"
      },
      {
        "branch": "feature/*",
        "strategy": "merge"
      }
    ]
  },
  {
    "type": "bugfix",
    "name": "bugfix/{new_branch}",
    "from": "dev",
    "to": [
      {
        "branch": "dev",
        "strategy": "merge"
      },
      {
        "branch": "feature/*",
        "strategy": "merge"
      }
    ]
  },
  {
    "type": "release",
    "name": "release/{new_branch}",
    "from": "dev",
    "to": [
      {
        "branch": "main",
        "strategy": "merge"
      }
    ]
  }
]
  • type表示分支类型
  • name定义了分支名,创建新分支时{new_branch}将被替换。
  • from指定来源分支。
  • to指定目标分支,其中strategy表示合并策略,可选mergerebasecherry-pick,branch上可用正则表达式。
  • hooks可选before_startafter_startbefore_finishafter_finish

举一个简单的案例。使用git flow start hotfix xxx(也可以是git flow start hotfix/xxx),从main分支上创建hotfix/xxx,修复bug后使用git flow finish hotfix xxx,该分支会被合并到maindev以及所有的功能分支上,然后被删除。

与可视化工具集成

其他没用过,给个lazygit的配置。

less 复制代码
customCommands:
  - key: "I"
    description: "git flow finish"
    context: "localBranches"
    prompts:
      - type: "confirm"
        title: "git flow"
        body: "Are you sure to finish this branch?"
    command: "git-flow finish {{.SelectedLocalBranch.Name}}"
  - key: "i"
    description: "git flow start"
    context: "localBranches"
    prompts:
      - type: "menuFromCommand"
        title: "git flow branch type"
        key: "BranchType"
        command: "sh path_to_gitflow.sh"
        filter: '"(.*)"'
        valueFormat: "{{ .group_1 }}"
        labelFormat: "{{ .group_1 }}"
      - type: "input"
        title: "git flow branch name"
        key: "BranchName"
    command: "git-flow start {{.Form.BranchType}} {{.Form.BranchName}}"

注意把path_to_gitflow.sh换一下。

gitflow.sh内容如下。

sh 复制代码
[ -f .git-flow.json ] && cat .git-flow.json | jq '.[].type' || cat ~/.config/git-flow/.git-flow.json | jq '.[].type'

优势

  • 遵循配置,便于统一团队规范。
  • 可扩展,便于自定义工作流。
  • 灵活。新分支取名更灵活,可开发更多功能,如通过分支名进行多级分类。目标分支名可设置为正则表达式,提供了批处理的能力。
  • 支持自定义hooks,一定程度上提供了与远程仓库交互的能力。如启动任务时将新建分支推送到远程。

其他

这东西只是灵感乍现写着玩玩,虽然我自己用着没什么问题,但仍不是一个成熟的产品。如果觉得有意思或者有帮助可以试用。有什么bug或者需求欢迎提issue。

相关推荐
int WINGsssss6 小时前
Git使用
git
用户0760530354388 小时前
Git Revert:安全移除错误提交的方式
git
Good_Starry20 小时前
Git介绍--github/gitee/gitlab使用
git·gitee·gitlab·github
云端奇趣1 天前
探索 3 个有趣的 GitHub 学习资源库
经验分享·git·学习·github
F_D_Z1 天前
【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
网络·git·网络协议·ssl
等风来不如迎风去1 天前
【git】main|REBASE 2/6
git
艾伦~耶格尔1 天前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea
云兮杜康1 天前
IDEA中用过git之后文件名颜色全变红
java·git·intellij-idea
睡不醒的小泽1 天前
git merge 和 git rebase
git
艾伦~耶格尔1 天前
Git 下载及安装超详教程(2024)
git·gitee·码仓