Git 常用场景示例合集示例 1基础功能开发流程Feature Branchmainfeature/logingit initgit commit -m init1. git checkout -b feature/logingit commit -m feat: 登录页面git commit -m feat: JWT验证git commit -m test: 登录测试2. git checkout main3. git merge feature/logingit tag v1.0v1.0mermaidgitGraph 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紧急热修复流程Hotfixmainhotfix/crashdevmain: 线上稳定版v1.0main: 正常迭代中1. git checkout -b hotfix/crashfix: 修复空指针崩溃test: 验证修复2. git merge hotfix/crash3. git tag v1.0.1v1.0.14. git cherry-pick hotfix (同步修复)mermaidgitGraph 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 同步Rebasemaindevfeature/user-Amain: 初始提交dev: 公共基础代码A: git checkout -b feature/user-AA: feat: 用户列表A: feat: 用户详情dev: 其他同事提交了新代码dev: 又有新提交1. git fetch origin dev2. git rebase dev (变基到最新)A: feat: 用户编辑基于最新代码3. git merge feature/user-A (干净的线性历史)mermaidgitGraph 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 Branchmaindevrelease/2.0main: v1.0v1.0feat: 订单模块feat: 支付模块feat: 通知模块1. git checkout -b release/2.0chore: bump version → 2.0.0fix: 发布前修复问题Afix: 发布前修复问题Bdocs: 更新CHANGELOG2. git merge release/2.03. git tag -a v2.0 -m releasev2.04. git merge release/2.0 (修复同步回dev)mermaidgitGraph 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 / Resetmainrevert-demoamend-demofeat: 功能A正常feat: 功能B正常feat: 功能C有问题1. git revert HEAD (生成反向提交)功能C已被安全撤销历史保留2. git reset --soft HEAD~1 (保留改动退回暂存)3. 重新修改后 git commitgit commit -m 手误写错的注释4. git commit --amend -m 正确注释mermaidgitGraph 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示例 6stash 暂存现场切换分支Stashmainfeature/big-taskhotfix/urgentmain: 稳定代码feat: 大需求开发到一半...1. git stash (临时保存现场)2. 切去修紧急bugfix: 紧急修复完成git merge hotfix/urgent3. git stash pop (恢复现场继续开发)feat: 继续完成大需求feat: 大需求完工git merge feature/big-taskmermaidgitGraph 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示例 7Squash 合并压缩提交整洁历史mainfeature/messymain: 干净的主线wip: 先提交一下fix: 刚才搞错了fix: 又改了一下wip: 临时保存feat: 终于写完了fix: review修改1. git merge --squash feature/messy2. git commit -m feat: 新功能(压缩为1个提交)git tag v1.1v1.1mermaidgitGraph 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多版本并行维护Backportmainsupport/v1.xhotfix/securitymain: 长期维护中feat: v2新特性Afeat: v2新特性Bgit tag v2.0v2.01. 维护旧版本 v1.x 分支git tag v1.5v1.52. git checkout -b hotfix/securityfix: 高危安全漏洞修复3. git merge → maingit tag v2.0.1v2.0.14. git cherry-pick (backport到v1)git tag v1.5.1v1.5.1mermaidgitGraph 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