Git 常用场景示例合集

示例 1:基础功能开发流程(Feature Branch)

main feature/login git init git commit -m 'init' 1. git checkout -b feature/login git commit -m 'feat: 登录页面' git commit -m 'feat: JWT验证' git commit -m 'test: 登录测试' 2. git checkout main 3. git merge feature/login git tag v1.0 v1.0

mermaid

gitGraph
   commit id: "git init"
   commit id: "git commit -m 'init'"
   branch feature/login
   checkout feature/login
   commit id: "1. git checkout -b feature/login"
   commit id: "git commit -m 'feat: 登录页面'"
   commit id: "git commit -m 'feat: JWT验证'"
   commit id: "git commit -m 'test: 登录测试'"
   checkout main
   commit id: "2. git checkout main"
   merge feature/login id: "3. git merge feature/login"
   commit id: "git tag v1.0" tag: "v1.0"

示例 2:紧急热修复流程(Hotfix)

main hotfix/crash dev main: 线上稳定版 v1.0 main: 正常迭代中 1. git checkout -b hotfix/crash fix: 修复空指针崩溃 test: 验证修复 2. git merge hotfix/crash 3. git tag v1.0.1 v1.0.1 4. git cherry-pick hotfix (同步修复)

mermaid

gitGraph
   commit id: "main: 线上稳定版" tag: "v1.0"
   commit id: "main: 正常迭代中"
   branch hotfix/crash
   checkout hotfix/crash
   commit id: "1. git checkout -b hotfix/crash" type: REVERSE
   commit id: "fix: 修复空指针崩溃" type: REVERSE
   commit id: "test: 验证修复" type: REVERSE
   checkout main
   merge hotfix/crash id: "2. git merge hotfix/crash"
   commit id: "3. git tag v1.0.1" tag: "v1.0.1" type: HIGHLIGHT
   branch dev
   checkout dev
   commit id: "4. git cherry-pick hotfix (同步修复)" type: HIGHLIGHT

示例 3:多人协作 rebase 同步(Rebase)

main dev feature/user-A main: 初始提交 dev: 公共基础代码 A: git checkout -b feature/user-A A: feat: 用户列表 A: feat: 用户详情 dev: 其他同事提交了新代码 dev: 又有新提交 1. git fetch origin dev 2. git rebase dev (变基到最新) A: feat: 用户编辑(基于最新代码) 3. git merge feature/user-A (干净的线性历史)

mermaid

gitGraph
   commit id: "main: 初始提交"
   branch dev
   checkout dev
   commit id: "dev: 公共基础代码"
   branch feature/user-A
   checkout feature/user-A
   commit id: "A: git checkout -b feature/user-A"
   commit id: "A: feat: 用户列表"
   commit id: "A: feat: 用户详情"
   checkout dev
   commit id: "dev: 其他同事提交了新代码"
   commit id: "dev: 又有新提交"
   checkout feature/user-A
   commit id: "1. git fetch origin dev" type: HIGHLIGHT
   commit id: "2. git rebase dev (变基到最新)" type: HIGHLIGHT
   commit id: "A: feat: 用户编辑(基于最新代码)"
   checkout dev
   merge feature/user-A id: "3. git merge feature/user-A (干净的线性历史)"

示例 4:版本发布流程(Release Branch)

main dev release/2.0 main: v1.0 v1.0 feat: 订单模块 feat: 支付模块 feat: 通知模块 1. git checkout -b release/2.0 chore: bump version → 2.0.0 fix: 发布前修复问题A fix: 发布前修复问题B docs: 更新CHANGELOG 2. git merge release/2.0 3. git tag -a v2.0 -m 'release' v2.0 4. git merge release/2.0 (修复同步回dev)

mermaid

gitGraph
   commit id: "main: v1.0" tag: "v1.0"
   branch dev
   checkout dev
   commit id: "feat: 订单模块"
   commit id: "feat: 支付模块"
   commit id: "feat: 通知模块"
   branch release/2.0
   checkout release/2.0
   commit id: "1. git checkout -b release/2.0"
   commit id: "chore: bump version → 2.0.0"
   commit id: "fix: 发布前修复问题A"
   commit id: "fix: 发布前修复问题B"
   commit id: "docs: 更新CHANGELOG"
   checkout main
   merge release/2.0 id: "2. git merge release/2.0"
   commit id: "3. git tag -a v2.0 -m 'release'" tag: "v2.0" type: HIGHLIGHT
   checkout dev
   merge release/2.0 id: "4. git merge release/2.0 (修复同步回dev)"

示例 5:撤销与回退操作(Revert / Reset)

main revert-demo amend-demo feat: 功能A(正常) feat: 功能B(正常) feat: 功能C(有问题!) 1. git revert HEAD (生成反向提交) 功能C已被安全撤销(历史保留) 2. git reset --soft HEAD~1 (保留改动退回暂存) 3. 重新修改后 git commit git commit -m '手误写错的注释' 4. git commit --amend -m '正确注释'

mermaid

gitGraph
   commit id: "feat: 功能A(正常)"
   commit id: "feat: 功能B(正常)"
   commit id: "feat: 功能C(有问题!)" type: REVERSE
   branch revert-demo
   checkout revert-demo
   commit id: "1. git revert HEAD (生成反向提交)" type: HIGHLIGHT
   commit id: "功能C已被安全撤销(历史保留)"
   checkout main
   commit id: "2. git reset --soft HEAD~1 (保留改动退回暂存)"  type: HIGHLIGHT
   commit id: "3. 重新修改后 git commit"
   branch amend-demo
   checkout amend-demo
   commit id: "git commit -m '手误写错的注释'" type: REVERSE
   commit id: "4. git commit --amend -m '正确注释'" type: HIGHLIGHT

示例 6:stash 暂存现场切换分支(Stash)

main feature/big-task hotfix/urgent main: 稳定代码 feat: 大需求开发到一半... 1. git stash (临时保存现场) 2. 切去修紧急bug fix: 紧急修复完成 git merge hotfix/urgent 3. git stash pop (恢复现场继续开发) feat: 继续完成大需求 feat: 大需求完工 git merge feature/big-task

mermaid

gitGraph
   commit id: "main: 稳定代码"
   branch feature/big-task
   checkout feature/big-task
   commit id: "feat: 大需求开发到一半..."
   commit id: "1. git stash (临时保存现场)" type: HIGHLIGHT
   checkout main
   branch hotfix/urgent
   checkout hotfix/urgent
   commit id: "2. 切去修紧急bug" type: REVERSE
   commit id: "fix: 紧急修复完成" type: REVERSE
   checkout main
   merge hotfix/urgent id: "git merge hotfix/urgent"
   checkout feature/big-task
   commit id: "3. git stash pop (恢复现场继续开发)" type: HIGHLIGHT
   commit id: "feat: 继续完成大需求"
   commit id: "feat: 大需求完工"
   checkout main
   merge feature/big-task id: "git merge feature/big-task"

示例 7:Squash 合并压缩提交(整洁历史)

main feature/messy main: 干净的主线 wip: 先提交一下 fix: 刚才搞错了 fix: 又改了一下 wip: 临时保存 feat: 终于写完了 fix: review修改 1. git merge --squash feature/messy 2. git commit -m 'feat: 新功能(压缩为1个提交)' git tag v1.1 v1.1

mermaid

gitGraph
   commit id: "main: 干净的主线"
   branch feature/messy
   checkout feature/messy
   commit id: "wip: 先提交一下"
   commit id: "fix: 刚才搞错了"
   commit id: "fix: 又改了一下"
   commit id: "wip: 临时保存"
   commit id: "feat: 终于写完了"
   commit id: "fix: review修改"
   checkout main
   commit id: "1. git merge --squash feature/messy" type: HIGHLIGHT
   commit id: "2. git commit -m 'feat: 新功能(压缩为1个提交)'" type: HIGHLIGHT
   commit id: "git tag v1.1" tag: "v1.1"

示例 8:多版本并行维护(Backport)

main support/v1.x hotfix/security main: 长期维护中 feat: v2新特性A feat: v2新特性B git tag v2.0 v2.0 1. 维护旧版本 v1.x 分支 git tag v1.5 v1.5 2. git checkout -b hotfix/security fix: 高危安全漏洞修复 3. git merge → main git tag v2.0.1 v2.0.1 4. git cherry-pick (backport到v1) git tag v1.5.1 v1.5.1

mermaid

gitGraph
   commit id: "main: 长期维护中"
   commit id: "feat: v2新特性A"
   commit id: "feat: v2新特性B"
   commit id: "git tag v2.0" tag: "v2.0" type: HIGHLIGHT
   branch support/v1.x
   checkout support/v1.x
   commit id: "1. 维护旧版本 v1.x 分支"
   commit id: "git tag v1.5" tag: "v1.5"
   branch hotfix/security
   checkout hotfix/security
   commit id: "2. git checkout -b hotfix/security" type: REVERSE
   commit id: "fix: 高危安全漏洞修复" type: REVERSE
   checkout main
   merge hotfix/security id: "3. git merge → main"
   commit id: "git tag v2.0.1" tag: "v2.0.1" type: HIGHLIGHT
   checkout support/v1.x
   merge hotfix/security id: "4. git cherry-pick (backport到v1)" type: HIGHLIGHT
   commit id: "git tag v1.5.1" tag: "v1.5.1" type: HIGHLIGHT
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐