当前位置: 首页 > news >正文

关于git分支堆叠

git stack 是一种 堆叠式分支管理(Stacked Branch Management)工作流工具,它的核心思想是: 把多个有依赖关系的功能分支像“积木”一样堆叠起来,按顺序开发、审查和合并,而不是一次性在一个大分支里完成所有改动。
https://www.stacking.dev/
https://graphite.dev/blog/stacked-prs
https://graphite.dev/blog/the-ideal-pr-is-50-lines-long
https://graphite.dev/blog/how-large-prs-slow-down-development
https://andrewlock.net/working-with-stacked-branches-in-git-part-1/
https://andrewlock.net/working-with-stacked-branches-in-git-part-2/

堆叠

gitGraphcommit id: "M0"branch feature/basecommit id: "base1"commit id: "base2"branch feature/middlecommit id: "middle1"branch feature/topcommit id: "top1"commit id: "top2"checkout maincommit id: "M1"

image

$ git checkout feature/top
Already on 'feature/top'
$ git rebase main --update-refs
Successfully rebased and updated refs/heads/feature/top.
Updated the following refs with --update-refs:refs/heads/feature/baserefs/heads/feature/middle

image

长分支

核心目标

  • 多人并行开发:每个人负责一个子功能,但都挂在同一个功能栈(stack)上
  • 保持主干稳定:功能没完成前,主干(main/master)不被污染
  • 可控合并顺序:按依赖关系逐层合并,避免一次性大冲击

协作小技巧

  • 命名规范:feature/xxx-partN,方便识别顺序
  • 频繁同步:每天 git fetch + git rebase,避免冲突堆积
  • 并行开发:不同成员可在同一 stack 的不同分支上工作,但要明确依赖关系
  • 自动化检查:CI/CD 针对每个子分支运行测试,提前发现问题

初始化

点击查看代码
# M0
chcp 65001
mkdir Test
cd Test
git init -b main
echo 初始化内容 > init.txt
git add .
git commit -m "初始化提交"# base1
chcp 65001
cd Test
git checkout main
git checkout -b feature/base
echo base内容-1 > base.txt
git add .
git commit -m "base提交-1"# middle1
chcp 65001
cd Test
git checkout feature/base
git checkout -b feature/middle
echo middle内容-1 > middle.txt
git add .
git commit -m "middle提交-1"# top1
chcp 65001
cd Test
git checkout feature/middle
git checkout -b feature/top
echo top内容-1 > top.txt
git add .
git commit -m "top提交-1"# top2
chcp 65001
cd Test
git checkout feature/top
echo top内容-2 >> top.txt
git add .
git commit -m "top提交-2"# base2
chcp 65001
cd Test
git checkout feature/base
echo base内容-2 >> base.txt
git add .
git commit -m "base提交-2"# M1
chcp 65001
cd Test
git checkout main
echo main-1 >> init.txt
git add .
git commit -m "main-1提交"
gitGraphcommit id: "M0"branch feature/basecommit id: "base1"branch feature/middlecommit id: "middle1"branch feature/topcommit id: "top1"commit id: "top2"checkout feature/basecommit id: "base2"checkout maincommit id: "M1"

image

切换到feature/top进行rebase main

$ git checkout feature/top
Already on 'feature/top'
$ git rebase main --update-refs
Successfully rebased and updated refs/heads/feature/top.
Updated the following refs with --update-refs:refs/heads/feature/middle

image

切换到feature/base进行rebase main

$ git checkout feature/base
Switched to branch 'feature/base'
$ git rebase main --update-refs
Successfully rebased and updated refs/heads/feature/base.

image

切换到feature/top进行rebase base

$ git checkout feature/top
Already on 'feature/top'
$ git rebase feature/base --update-refs
warning: skipped previously applied commit 935fa8b
hint: use --reapply-cherry-picks to include skipped commits
hint: Disable this message with "git config advice.skippedCherryPicks false"
Successfully rebased and updated refs/heads/feature/top.
Updated the following refs with --update-refs:refs/heads/feature/middle

image

#解决冲突继续rebase
git rebase --continue
#终止rebase
git rebase --abort#设置默认启用--update-refs
git config --global --add --bool rebase.updateRefs true

普通rebase

image

gitlab上操作

base、middle、top合并完成效果
image

git命令扩展

--update-refs 是 Git 在 2.38 版本中引入的新选项
git default-branch 默认分支
git merge-base-origin 获取当前分支与默认分支最近共同祖先
git stack 查看当前分支堆叠
git push-stack 推送所在分支及堆叠上游分支
在 C:\Users\UserName\.gitconfig 文件中添加

[alias]default-branch = "!git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"merge-base-origin ="!f() { git merge-base ${1-HEAD} origin/$(git default-branch); };f "stack = "!f() { \BRANCH=${1-HEAD}; \MERGE_BASE=$(git merge-base-origin $BRANCH); \git log --decorate-refs=refs/heads --simplify-by-decoration --pretty=format:\"%(decorate:prefix=,suffix=,tag=,separator=%n)\" $MERGE_BASE..$BRANCH; \};f "push-stack = "!f() { \BRANCH=${1-HEAD}; \git stack $BRANCH | xargs -I {} git push --force-with-lease origin {}; \};f "

git-spice

https://github.com/abhinav/git-spice

安装

下载gs命令解压后把gs.exe拷贝到 C:\Program Files\Git\cmd 目录

配置

https://abhinav.github.io/git-spice/setup/auth/#gitlab-self-hosted

# 自托管设置
git config spice.forge.gitlab.url https://gitlab.example.com# gitlab 用户设置 -> 访问令牌 -> 添加新token 后auth
gs auth login

使用

Your first stack

https://abhinav.github.io/git-spice/start/stack/

Your first stacked Change Requests

https://abhinav.github.io/git-spice/start/submit/

gs branch submit 到远端效果
image

http://www.sczhlp.com/news/16095/

相关文章:

  • 吉林市城市建设学校网站做一个个人网站
  • 企业营销管理软件百度关键字优化价格
  • 洛阳做网站的军事网站大全军事网
  • php整站开发 企业网站教程亚马逊关键词工具哪个最准
  • 小说网站的内容做友情链接教程
  • 一个微信公众号可以做几个网站西安专业网络推广公司
  • wordpress网站破解业务员用什么软件找客户
  • 中国建设银行网站荆门网点查询seo优化工作内容
  • 网站集约化建设做法seo推广是做什么
  • After Effects 2025中文免费版下载安装教程 (附安装包)After Effects 2025中文免费版下载安装教程
  • 使用OpenWRT搭建内网DNS服务器
  • 人类命运共同体厦门seo优化多少钱
  • 网站开发集网页设计一般用什么软件
  • app要有网站做基础知识北京百度搜索优化
  • 盐城市城乡和住房建设厅网站太原百度关键词排名
  • 找不同 网站开发营销型网站建设目标
  • 桂林市生活网官方网站网站网络营销公司
  • 义乌外贸建站手机建站教程
  • 网站介绍经过下拉怎么做谷歌浏览器app
  • 乐山市规划和建设局门户网站郑州网络推广效果
  • wordpress 网站建设成人英语培训
  • 照片制作网站百度客服投诉中心
  • arduino开发你好小智(2-2)外设定时提醒任务 - MKT
  • 会计恒等式
  • 漂亮的网站底部代码网络营销学校
  • 一小时做网站企业网站设计的基本内容包括哪些
  • 58企业网站怎么做seo是什么意思中文
  • 规模以上工业企业数量seo网络推广排名
  • 如何做网站美工郑州百度网站优化排名
  • Homebrew基本命令总结