Git 的自我修养
从日常命令到内部原理,再到工程化实践------系统化整理 Git 的使用知识。
定位:面向「有一定经验、想系统化整理并深入原理」的读者。
目录
- 第 0 章 · 认识版本控制与 Git
- 第 1 章 · Git 的核心概念与对象模型
- 第 2 章 · 配置与初始化
- 第 3 章 · 日常核心命令
- 第 4 章 · 分支与合并
- 第 5 章 · 提交规范与提交信息
- 第 6 章 · 远程协作
- 第 7 章 · 撤销与修复(场景速查)
- 第 8 章 · Git 内部原理
- 第 9 章 · 钩子与自动化
- 第 10 章 · 工程化使用方案
- 附录 · 速查表
第 0 章 · 认识版本控制与 Git
0.1 为什么需要版本控制
版本控制(Version Control System, VCS)用于跟踪文件随时间的变更,让你可以:
- 随时回退到历史任意版本
- 并行开发、对比差异
- 多人协作时合并各自的改动
- 记录「谁、什么时候、为什么」改了代码
0.2 三类版本控制工具
| 类型 | 代表 | 特点 |
|---|---|---|
| 本地版本控制 | RCS | 只在单机记录补丁,无法多人协作 |
| 集中式 | SVN / CVS | 单一中央服务器,客户端只存快照,离线无法提交 |
| 分布式 | Git / Mercurial | 每个客户端都是完整仓库,离线可提交,天然支持分支 |
0.3 Git 的核心特点
- 快照而非差异:每次提交保存的是完整文件快照(未变化的文件用指针指向旧版本),而不是只存差异。
- 分布式:每个克隆都是完整仓库,本地即可完成绝大部分操作。
- 离线可用:提交、查看历史、建分支都不需要联网。
- 数据完整性:所有内容通过 SHA-1(新版本逐步迁移到 SHA-256)哈希寻址,篡改必被发现。
- 分支廉价:分支只是一个可移动的指针,创建和切换极快。
第 1 章 · Git 的核心概念与对象模型
1.1 四个区域
Git 把文件划分为四个逻辑区域:
工作区 暂存区 本地仓库 远程仓库
Working Staging Local Remote
Directory Area (index) Repository Repository
│ git add │ git commit │ git push │
├────────────►├───────────────►├────────────────►│
│ │ │ │
│◄────────────┼────────────────┼─────────────────┤
│ checkout/restore git fetch/pull │
- 工作区(Working Directory):你实际看到的文件,正在编辑的内容。
- 暂存区(Staging Area / Index) :
git add之后、git commit之前存放「下次要提交的内容」的地方。 - 本地仓库(Local Repository) :
.git目录,保存了所有提交历史。 - 远程仓库(Remote Repository):托管在服务器(GitHub / GitLab / Gitee 等)上的仓库。
用一张流程图把四个区域之间的数据流向串起来:
#mermaid-svg-FcjBgBidsZhHgRSJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-FcjBgBidsZhHgRSJ .error-icon{fill:#552222;}#mermaid-svg-FcjBgBidsZhHgRSJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FcjBgBidsZhHgRSJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FcjBgBidsZhHgRSJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FcjBgBidsZhHgRSJ .marker.cross{stroke:#333333;}#mermaid-svg-FcjBgBidsZhHgRSJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FcjBgBidsZhHgRSJ p{margin:0;}#mermaid-svg-FcjBgBidsZhHgRSJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster-label text{fill:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster-label span{color:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster-label span p{background-color:transparent;}#mermaid-svg-FcjBgBidsZhHgRSJ .label text,#mermaid-svg-FcjBgBidsZhHgRSJ span{fill:#333;color:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ .node rect,#mermaid-svg-FcjBgBidsZhHgRSJ .node circle,#mermaid-svg-FcjBgBidsZhHgRSJ .node ellipse,#mermaid-svg-FcjBgBidsZhHgRSJ .node polygon,#mermaid-svg-FcjBgBidsZhHgRSJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-FcjBgBidsZhHgRSJ .rough-node .label text,#mermaid-svg-FcjBgBidsZhHgRSJ .node .label text,#mermaid-svg-FcjBgBidsZhHgRSJ .image-shape .label,#mermaid-svg-FcjBgBidsZhHgRSJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-FcjBgBidsZhHgRSJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-FcjBgBidsZhHgRSJ .rough-node .label,#mermaid-svg-FcjBgBidsZhHgRSJ .node .label,#mermaid-svg-FcjBgBidsZhHgRSJ .image-shape .label,#mermaid-svg-FcjBgBidsZhHgRSJ .icon-shape .label{text-align:center;}#mermaid-svg-FcjBgBidsZhHgRSJ .node.clickable{cursor:pointer;}#mermaid-svg-FcjBgBidsZhHgRSJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-FcjBgBidsZhHgRSJ .arrowheadPath{fill:#333333;}#mermaid-svg-FcjBgBidsZhHgRSJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-FcjBgBidsZhHgRSJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-FcjBgBidsZhHgRSJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-FcjBgBidsZhHgRSJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-FcjBgBidsZhHgRSJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-FcjBgBidsZhHgRSJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster text{fill:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ .cluster span{color:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-FcjBgBidsZhHgRSJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-FcjBgBidsZhHgRSJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-FcjBgBidsZhHgRSJ .icon-shape,#mermaid-svg-FcjBgBidsZhHgRSJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-FcjBgBidsZhHgRSJ .icon-shape p,#mermaid-svg-FcjBgBidsZhHgRSJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-FcjBgBidsZhHgRSJ .icon-shape .label rect,#mermaid-svg-FcjBgBidsZhHgRSJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-FcjBgBidsZhHgRSJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-FcjBgBidsZhHgRSJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-FcjBgBidsZhHgRSJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} git add
git commit
git push
git fetch / pull
git checkout / restore
git restore --staged
工作区 Working Directory
暂存区 Index
本地仓库 Repository
远程仓库 Remote
1.2 文件的三种状态
| 状态 | 含义 | 相对位置 |
|---|---|---|
| 已修改(modified) | 工作区改了但还没 add | 只在工作区 |
| 已暂存(staged) | add 了但还没 commit | 在暂存区 |
| 已提交(committed) | 已经 commit 到本地仓库 | 在本地仓库 |
1.3 底层对象模型
Git 是一个「内容寻址」的文件系统,核心是四种对象:
| 对象 | 说明 | 类比 |
|---|---|---|
| blob | 文件内容,不含文件名 | 文件本身 |
| tree | 目录结构,记录文件名和指向 blob/tree 的引用 | 文件夹 |
| commit | 一次提交,指向一个 tree + 父提交 + 元信息(作者、时间、信息) | 快照 |
| tag | 指向某个 commit 的引用,可附注 | 标签 |
text
commit (v2)
├── tree (根目录)
│ ├── blob (README.md 内容)
│ └── tree (src/)
│ └── blob (main.c 内容)
└── parent → commit (v1)
关键点:相同的文件内容只存一次(哈希相同即复用),未变化的文件在新提交中只是指向旧 blob 的引用。
四种对象之间的引用关系可以这样理解:
#mermaid-svg-hBvvOwYGGFHRZKa1{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hBvvOwYGGFHRZKa1 .error-icon{fill:#552222;}#mermaid-svg-hBvvOwYGGFHRZKa1 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hBvvOwYGGFHRZKa1 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .marker.cross{stroke:#333333;}#mermaid-svg-hBvvOwYGGFHRZKa1 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hBvvOwYGGFHRZKa1 p{margin:0;}#mermaid-svg-hBvvOwYGGFHRZKa1 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster-label text{fill:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster-label span{color:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster-label span p{background-color:transparent;}#mermaid-svg-hBvvOwYGGFHRZKa1 .label text,#mermaid-svg-hBvvOwYGGFHRZKa1 span{fill:#333;color:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .node rect,#mermaid-svg-hBvvOwYGGFHRZKa1 .node circle,#mermaid-svg-hBvvOwYGGFHRZKa1 .node ellipse,#mermaid-svg-hBvvOwYGGFHRZKa1 .node polygon,#mermaid-svg-hBvvOwYGGFHRZKa1 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .rough-node .label text,#mermaid-svg-hBvvOwYGGFHRZKa1 .node .label text,#mermaid-svg-hBvvOwYGGFHRZKa1 .image-shape .label,#mermaid-svg-hBvvOwYGGFHRZKa1 .icon-shape .label{text-anchor:middle;}#mermaid-svg-hBvvOwYGGFHRZKa1 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .rough-node .label,#mermaid-svg-hBvvOwYGGFHRZKa1 .node .label,#mermaid-svg-hBvvOwYGGFHRZKa1 .image-shape .label,#mermaid-svg-hBvvOwYGGFHRZKa1 .icon-shape .label{text-align:center;}#mermaid-svg-hBvvOwYGGFHRZKa1 .node.clickable{cursor:pointer;}#mermaid-svg-hBvvOwYGGFHRZKa1 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .arrowheadPath{fill:#333333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hBvvOwYGGFHRZKa1 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hBvvOwYGGFHRZKa1 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hBvvOwYGGFHRZKa1 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster text{fill:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 .cluster span{color:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-hBvvOwYGGFHRZKa1 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hBvvOwYGGFHRZKa1 rect.text{fill:none;stroke-width:0;}#mermaid-svg-hBvvOwYGGFHRZKa1 .icon-shape,#mermaid-svg-hBvvOwYGGFHRZKa1 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hBvvOwYGGFHRZKa1 .icon-shape p,#mermaid-svg-hBvvOwYGGFHRZKa1 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hBvvOwYGGFHRZKa1 .icon-shape .label rect,#mermaid-svg-hBvvOwYGGFHRZKa1 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hBvvOwYGGFHRZKa1 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hBvvOwYGGFHRZKa1 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hBvvOwYGGFHRZKa1 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 指向
parent
包含
包含
包含
指向
commit 提交对象
tree 根目录
父 commit
blob 文件内容
tree 子目录
blob 文件内容
tag 标签对象
1.4 引用与指针
- HEAD:指向当前所在位置(通常是某个分支,也可「游离」到某个 commit)。
- 分支(branch):一个指向 commit 的可移动指针。
- 远程跟踪分支(remote-tracking branch) :如
origin/main,记录「上次与远程交互时远程的状态」。 - reflog:记录 HEAD 和分支指针的移动历史,用于找回「丢失」的提交。
第 2 章 · 配置与初始化
2.1 三级作用域
git config 的配置按作用域分层,优先级从低到高:
bash
git config --system <key> <value> # 系统级,/etc/gitconfig,所有用户
git config --global <key> <value> # 用户级,~/.gitconfig,当前用户
git config --local <key> <value> # 仓库级,.git/config,当前仓库(默认,最高)
查看配置:
bash
git config --list # 列出所有生效配置
git config --list --show-origin # 附带来源,排查「哪来的配置」
git config user.name # 查看单项
2.2 常用配置项
bash
# 身份(提交必须)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 别名,提升效率
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "log -1 HEAD"
# 编辑器与工具
git config --global core.editor vim
git config --global merge.tool vimdiff
# 换行符(跨平台协作)
git config --global core.autocrlf input # macOS/Linux 推荐
git config --global core.autocrlf true # Windows 推荐
# 提交信息模板
git config --global commit.template ~/.gitmessage
# 默认分支名
git config --global init.defaultBranch main
2.3 .gitignore 与 .gitattributes
.gitignore:忽略不需要纳入版本控制的文件(构建产物、依赖、日志、密钥等)。
gitignore
# 注释
node_modules/
dist/
*.log
.env
.idea/
!important.log # ! 反向取回(取回之前要先取消父目录的忽略)
.gitattributes:控制文件的属性(换行符处理、diff 行为、Git LFS 等)。
gitattributes
*.png binary
*.sh text eol=lf
2.4 init vs clone
git init:在已有目录中新建仓库(不包含任何提交)。git clone <url>:复制一个完整仓库到本地,自动建立origin远程和远程跟踪分支。
第 3 章 · 日常核心命令
3.1 记录变更
bash
git add <file> # 暂存单个文件
git add . # 暂存当前目录所有改动
git add -p # 交互式按「块」暂存(精细控制)
git commit -m "msg" # 提交暂存区
git commit -a -m "msg" # 跳过 add,直接提交已跟踪文件的改动(新文件仍需 add)
git commit --amend -m "new msg" # 修改上一次提交(信息或内容)
--amend会「改写」上一次提交,产生新的 commit 哈希。若已推送,需--force-with-lease。
3.2 查看状态与差异
bash
git status # 工作区 + 暂存区状态
git status -s # 简洁模式
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 本地仓库(也叫 --cached)
git diff HEAD # 工作区 vs 最新提交
git diff <commit1> <commit2> # 任意两个提交之间
3.3 查看历史
bash
git log # 完整历史
git log --oneline # 一行一条
git log --graph --decorate --all --oneline # 图形化分支图(推荐 alias 成 lg)
git log -p # 附带每个提交的 diff
git log --stat # 每个提交的改动统计
git log --author="name" # 按作者过滤
git log -S"keyword" # 查找内容出现/消失的提交
git log <file> # 某个文件的历史
git blame <file> # 逐行查看「谁改的」
3.4 reset 三档(理解本质)
git reset 移动 HEAD(和当前分支指针),三档决定「暂存区、工作区」是否一起动:
| 模式 | HEAD/分支 | 暂存区 | 工作区 | 典型用途 |
|---|---|---|---|---|
--soft |
移动 | 保留 | 保留 | 撤销 commit,但保留改动在暂存区,方便重新组织提交 |
--mixed(默认) |
移动 | 重置 | 保留 | 撤销 commit 和 add,改动回到工作区 |
--hard |
移动 | 重置 | 重置 | 完全丢弃,回到指定提交(危险,慎用) |
bash
git reset HEAD~1 # 撤销上一次提交,改动回到工作区(默认 mixed)
git reset --soft HEAD~1 # 撤销提交,改动保留在暂存区
git reset --hard HEAD~1 # 彻底丢弃
与
reset相对的restore/switch是较新的、更「安全语义化」的命令(见 3.5)。
3.5 restore 与 switch(新命令)
Git 2.23 起拆分 checkout 的双重职责:
bash
git restore <file> # 丢弃工作区改动(= 老命令 checkout -- <file>)
git restore --staged <file> # 把文件从暂存区撤回到工作区(= reset HEAD <file>)
git switch <branch> # 切换分支
git switch -c <branch> # 新建并切换分支
3.6 临时保存 stash
bash
git stash # 保存当前工作区+暂存区的改动,恢复干净状态
git stash -u # 连同未跟踪文件一起 stash
git stash list # 列出所有 stash
git stash pop # 恢复最近一次 stash 并删除它
git stash apply # 恢复但不删除
git stash drop # 删除某次 stash
git stash pop stash@{1} # 指定恢复某一次
3.7 标签 tag
bash
git tag # 列出标签
git tag v1.0.0 # 轻量标签(只是一个指针)
git tag -a v1.0.0 -m "release" # 附注标签(含打标签者、时间、信息)
git tag -a v1.0.0 9fceb02 # 给历史提交补打标签
git push origin v1.0.0 # 推送单个标签
git push origin --tags # 推送所有标签
3.8 拾取与回滚
bash
git cherry-pick <commit> # 把某次提交的改动「摘」到当前分支
git cherry-pick <A>..<B> # 摘取一系列提交(不含 A)
git revert <commit> # 生成一个新提交来「撤销」某次提交(不改写历史)
revertvsreset:revert安全(保留历史、可协作),reset改写历史(个人分支可用)。
第 4 章 · 分支与合并
4.1 分支的本质
分支只是一个指向某个 commit 的可移动指针。新建分支只是新建一个指针,几乎零成本。
bash
git branch # 列出本地分支
git branch <name> # 新建分支(不切换)
git branch -d <name> # 删除已合并分支
git branch -D <name> # 强制删除未合并分支
git branch -vv # 查看分支与上游的关联关系
4.2 merge:快进与三方合并
git merge <branch> 把目标分支合并到当前分支:
- Fast-forward(快进):当前分支没有分叉,直接把指针前移,不产生新提交。
- 三方合并(3-way merge):两个分支各自有提交,Git 以「共同祖先 + 两边」生成合并提交。
bash
git merge feature # 默认快进(若可分叉也可快进)
git merge --no-ff feature # 强制生成合并提交(保留分支历史,常用于主干)
git merge --squash feature # 把改动合并但压成一次(需手动 commit)
两种合并方式的差异:
#mermaid-svg-m0pyqMfeYfL7tRIm{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-m0pyqMfeYfL7tRIm .error-icon{fill:#552222;}#mermaid-svg-m0pyqMfeYfL7tRIm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-m0pyqMfeYfL7tRIm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-m0pyqMfeYfL7tRIm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-m0pyqMfeYfL7tRIm .marker.cross{stroke:#333333;}#mermaid-svg-m0pyqMfeYfL7tRIm svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-m0pyqMfeYfL7tRIm p{margin:0;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-id,#mermaid-svg-m0pyqMfeYfL7tRIm .commit-msg,#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms',verdana,arial,sans-serif;font-family:var(--mermaid-font-family);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label0{fill:#ffffff;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit0{stroke:hsl(240, 100%, 46.2745098039%);fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight0{stroke:hsl(60, 100%, 3.7254901961%);fill:hsl(60, 100%, 3.7254901961%);}#mermaid-svg-m0pyqMfeYfL7tRIm .label0{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow0{stroke:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label1{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit1{stroke:hsl(60, 100%, 43.5294117647%);fill:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight1{stroke:rgb(0, 0, 160.5);fill:rgb(0, 0, 160.5);}#mermaid-svg-m0pyqMfeYfL7tRIm .label1{fill:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow1{stroke:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label2{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit2{stroke:hsl(80, 100%, 46.2745098039%);fill:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight2{stroke:rgb(48.8333333334, 0, 146.5000000001);fill:rgb(48.8333333334, 0, 146.5000000001);}#mermaid-svg-m0pyqMfeYfL7tRIm .label2{fill:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow2{stroke:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label3{fill:#ffffff;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit3{stroke:hsl(210, 100%, 46.2745098039%);fill:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight3{stroke:rgb(146.5000000001, 73.2500000001, 0);fill:rgb(146.5000000001, 73.2500000001, 0);}#mermaid-svg-m0pyqMfeYfL7tRIm .label3{fill:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow3{stroke:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label4{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit4{stroke:hsl(180, 100%, 46.2745098039%);fill:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight4{stroke:rgb(146.5000000001, 0, 0);fill:rgb(146.5000000001, 0, 0);}#mermaid-svg-m0pyqMfeYfL7tRIm .label4{fill:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow4{stroke:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label5{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit5{stroke:hsl(150, 100%, 46.2745098039%);fill:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight5{stroke:rgb(146.5000000001, 0, 73.2500000001);fill:rgb(146.5000000001, 0, 73.2500000001);}#mermaid-svg-m0pyqMfeYfL7tRIm .label5{fill:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow5{stroke:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label6{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit6{stroke:hsl(300, 100%, 46.2745098039%);fill:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight6{stroke:rgb(0, 146.5000000001, 0);fill:rgb(0, 146.5000000001, 0);}#mermaid-svg-m0pyqMfeYfL7tRIm .label6{fill:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow6{stroke:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch-label7{fill:black;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit7{stroke:hsl(0, 100%, 46.2745098039%);fill:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight7{stroke:rgb(0, 146.5000000001, 146.5000000001);fill:rgb(0, 146.5000000001, 146.5000000001);}#mermaid-svg-m0pyqMfeYfL7tRIm .label7{fill:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow7{stroke:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .branch{stroke-width:1;stroke:#333333;stroke-dasharray:2;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-label{font-size:10px;fill:#000021;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-label-bkg{font-size:10px;fill:#ffffde;opacity:0.5;}#mermaid-svg-m0pyqMfeYfL7tRIm .tag-label{font-size:10px;fill:#131300;}#mermaid-svg-m0pyqMfeYfL7tRIm .tag-label-bkg{fill:#ECECFF;stroke:hsl(240, 60%, 86.2745098039%);}#mermaid-svg-m0pyqMfeYfL7tRIm .tag-hole{fill:#333;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-merge{stroke:#ECECFF;fill:#ECECFF;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-reverse{stroke:#ECECFF;fill:#ECECFF;stroke-width:3;}#mermaid-svg-m0pyqMfeYfL7tRIm .commit-highlight-inner{stroke:#ECECFF;fill:#ECECFF;}#mermaid-svg-m0pyqMfeYfL7tRIm .arrow{stroke-width:8;stroke-linecap:round;fill:none;}#mermaid-svg-m0pyqMfeYfL7tRIm .gitTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-m0pyqMfeYfL7tRIm :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} main feature A B C D E 三方合并
4.3 rebase:变基
rebase 把当前分支的提交「重放」到目标分支最新提交之上,得到一条线性历史:
bash
git rebase main # 把当前分支变基到 main 之上
git rebase -i HEAD~3 # 交互式变基,重写最近 3 个提交
交互式变基 -i 的常用操作:
| 命令 | 含义 |
|---|---|
pick |
保留该提交 |
reword |
修改提交信息 |
edit |
停下来修改该提交内容 |
squash |
合并到上一个提交并合并信息 |
fixup |
合并到上一个提交,丢弃本条信息 |
drop |
删除该提交 |
rebase 与 merge 的历史形态对比:
#mermaid-svg-VOU0wHiquo4zmC8p{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-VOU0wHiquo4zmC8p .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-VOU0wHiquo4zmC8p .error-icon{fill:#552222;}#mermaid-svg-VOU0wHiquo4zmC8p .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VOU0wHiquo4zmC8p .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VOU0wHiquo4zmC8p .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VOU0wHiquo4zmC8p .marker.cross{stroke:#333333;}#mermaid-svg-VOU0wHiquo4zmC8p svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VOU0wHiquo4zmC8p p{margin:0;}#mermaid-svg-VOU0wHiquo4zmC8p .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VOU0wHiquo4zmC8p .cluster-label text{fill:#333;}#mermaid-svg-VOU0wHiquo4zmC8p .cluster-label span{color:#333;}#mermaid-svg-VOU0wHiquo4zmC8p .cluster-label span p{background-color:transparent;}#mermaid-svg-VOU0wHiquo4zmC8p .label text,#mermaid-svg-VOU0wHiquo4zmC8p span{fill:#333;color:#333;}#mermaid-svg-VOU0wHiquo4zmC8p .node rect,#mermaid-svg-VOU0wHiquo4zmC8p .node circle,#mermaid-svg-VOU0wHiquo4zmC8p .node ellipse,#mermaid-svg-VOU0wHiquo4zmC8p .node polygon,#mermaid-svg-VOU0wHiquo4zmC8p .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VOU0wHiquo4zmC8p .rough-node .label text,#mermaid-svg-VOU0wHiquo4zmC8p .node .label text,#mermaid-svg-VOU0wHiquo4zmC8p .image-shape .label,#mermaid-svg-VOU0wHiquo4zmC8p .icon-shape .label{text-anchor:middle;}#mermaid-svg-VOU0wHiquo4zmC8p .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-VOU0wHiquo4zmC8p .rough-node .label,#mermaid-svg-VOU0wHiquo4zmC8p .node .label,#mermaid-svg-VOU0wHiquo4zmC8p .image-shape .label,#mermaid-svg-VOU0wHiquo4zmC8p .icon-shape .label{text-align:center;}#mermaid-svg-VOU0wHiquo4zmC8p .node.clickable{cursor:pointer;}#mermaid-svg-VOU0wHiquo4zmC8p .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-VOU0wHiquo4zmC8p .arrowheadPath{fill:#333333;}#mermaid-svg-VOU0wHiquo4zmC8p .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VOU0wHiquo4zmC8p .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VOU0wHiquo4zmC8p .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VOU0wHiquo4zmC8p .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-VOU0wHiquo4zmC8p .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VOU0wHiquo4zmC8p .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-VOU0wHiquo4zmC8p .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VOU0wHiquo4zmC8p .cluster text{fill:#333;}#mermaid-svg-VOU0wHiquo4zmC8p .cluster span{color:#333;}#mermaid-svg-VOU0wHiquo4zmC8p div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VOU0wHiquo4zmC8p .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-VOU0wHiquo4zmC8p rect.text{fill:none;stroke-width:0;}#mermaid-svg-VOU0wHiquo4zmC8p .icon-shape,#mermaid-svg-VOU0wHiquo4zmC8p .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VOU0wHiquo4zmC8p .icon-shape p,#mermaid-svg-VOU0wHiquo4zmC8p .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-VOU0wHiquo4zmC8p .icon-shape .label rect,#mermaid-svg-VOU0wHiquo4zmC8p .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VOU0wHiquo4zmC8p .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-VOU0wHiquo4zmC8p .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-VOU0wHiquo4zmC8p :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} rebase:线性历史
A
B
E
C'
D'
merge:保留分叉
A
B
E
C
D
Merge
4.4 merge vs rebase 的选择
| 维度 | merge | rebase |
|---|---|---|
| 历史形态 | 分叉 + 合并提交(真实记录) | 线性(更清爽) |
| 是否改写历史 | 否 | 是 |
| 协作安全性 | 安全 | 有风险(见黄金法则) |
| 适用场景 | 合并长期分支 / 主干 | 整理个人分支 / 保持同步 |
rebase 黄金法则:不要 rebase 已经推送到共享仓库、且别人可能基于其开发的提交。
常见实践:个人/特性分支内部用 rebase 保持整洁,合并进主干用 merge(或 squash merge)。
4.5 解决冲突
发生冲突时,Git 在文件中标记:
text
<<<<<<< HEAD
当前分支的内容
=======
被合并/变基的内容
>>>>>>> feature
解决步骤:
bash
# 1. 手动编辑文件,删掉冲突标记,保留正确内容
# 2. 标记已解决
git add <file>
# 3. 完成合并(merge 时)或继续(rebase 时)
git commit # merge
git rebase --continue # rebase
# 放弃操作
git merge --abort
git rebase --abort
4.6 主流分支模型
| 模型 | 核心思想 | 适用场景 |
|---|---|---|
| Git Flow | main + develop + feature/release/hotfix 多条长期分支 | 有明确版本发布节奏的传统项目 |
| GitHub Flow | main + 短期 feature 分支 + PR 评审 + 频繁部署 | 持续部署的 Web 应用 |
| Trunk-Based | 所有人直接/近乎直接提交主干,用 feature flag 隔离 | 追求极速交付、成熟工程团队 |
第 5 章 · 提交规范与提交信息
5.1 为什么要有规范
- 历史可读:一眼看出每次改动「做了什么」
- 自动生成 changelog / 版本号(语义化版本)
- 快速定位问题、回溯原因
- 团队协作的统一语言
5.2 Conventional Commits 格式
标准格式:
text
<type>(<scope>): <subject>
<body>
<footer>
示例:
text
feat(auth): 新增邮箱验证码登录
- 支持发送验证码与校验
- 新增对应单元测试
Closes #123
text
fix(parser): 修复数组越界导致的崩溃
当输入为空数组时下标越界,现已加边界判断。
BREAKING CHANGE: parse() 的返回值由数组改为对象
5.3 常用 type 及含义
| type | 含义 | 说明 |
|---|---|---|
feat |
新功能 | 新增对用户可见的功能 |
fix |
修复 bug | 修复一个缺陷 |
docs |
文档 | 只改文档(README、注释等) |
style |
格式 | 不影响逻辑的格式调整(空格、缩进、分号等) |
refactor |
重构 | 既不新增功能也不修 bug 的代码改动 |
perf |
性能 | 提升性能的改动 |
test |
测试 | 新增或修改测试用例 |
build |
构建 | 影响构建系统或外部依赖(npm、webpack、依赖版本) |
ci |
持续集成 | CI 配置文件改动(GitHub Actions、Jenkins 等) |
chore |
杂项 | 不修改 src/test 的琐事(配置、构建脚本维护等) |
revert |
回滚 | 回滚某次提交 |
update |
更新 | 非官方扩展,部分团队用于「更新依赖/内容」 |
init / release |
自定义 | 团队约定扩展 |
5.4 type 边界辨析(易混点)
chorevsbuild:改动依赖版本、构建脚本 →build;改其他配置/琐事 →chore。chorevsupdate:update不是官方规范,通常约定为「更新依赖或已有功能的小幅调整」,与chore有重叠,团队需二选一约定。refactorvsfix:改了行为结果(修了 bug)→fix;行为不变只改内部实现 →refactor。refactorvsperf:明显为了性能 →perf;仅结构优化 →refactor。docsvsstyle:改注释/文档 →docs;改代码格式 →style。
5.5 subject / body / footer 写法要点
- subject:祈使句、首字母小写、结尾不加句号、控制在 50 字符内。
- scope :可选,标注影响范围,如
feat(auth)、fix(parser)。 - body:解释「为什么」以及「怎么做」,非强制但推荐。
- footer :
BREAKING CHANGE: <描述>标注破坏性变更Closes #123/Refs #123关联 issue
5.6 工具链落地
bash
# 提交信息模板
git config --global commit.template ~/.gitmessage
# commitlint 校验 + husky 钩子(前端项目常用)
npm i -D @commitlint/cli @commitlint/config-conventional husky
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
# 自动生成 changelog / 版本
# semantic-release / standard-version / conventional-changelog
第 6 章 · 远程协作
6.1 远程的基本操作
bash
git remote -v # 查看远程
git remote add origin <url> # 添加远程(origin 是约定俗成的名字)
git remote set-url origin <url> # 修改远程地址
git remote remove origin # 删除远程
6.2 fetch / pull / push 的关系
| 命令 | 作用 | 是否改动工作区 |
|---|---|---|
git fetch |
下载远程新数据,更新远程跟踪分支 | 否(安全) |
git pull |
= fetch + merge(或 rebase) | 是 |
git push |
把本地提交上传到远程 | 否 |
bash
git fetch origin # 只拉取,不合并
git pull # 拉取并合并当前分支
git pull --rebase # 拉取并变基(推荐,保持线性历史)
git push origin <branch> # 推送到指定分支
三者关系可以概括为:
#mermaid-svg-HaEJzqzcYaifr2Pi{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-HaEJzqzcYaifr2Pi .error-icon{fill:#552222;}#mermaid-svg-HaEJzqzcYaifr2Pi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-HaEJzqzcYaifr2Pi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-HaEJzqzcYaifr2Pi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-HaEJzqzcYaifr2Pi .marker.cross{stroke:#333333;}#mermaid-svg-HaEJzqzcYaifr2Pi svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-HaEJzqzcYaifr2Pi p{margin:0;}#mermaid-svg-HaEJzqzcYaifr2Pi .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster-label text{fill:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster-label span{color:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster-label span p{background-color:transparent;}#mermaid-svg-HaEJzqzcYaifr2Pi .label text,#mermaid-svg-HaEJzqzcYaifr2Pi span{fill:#333;color:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi .node rect,#mermaid-svg-HaEJzqzcYaifr2Pi .node circle,#mermaid-svg-HaEJzqzcYaifr2Pi .node ellipse,#mermaid-svg-HaEJzqzcYaifr2Pi .node polygon,#mermaid-svg-HaEJzqzcYaifr2Pi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-HaEJzqzcYaifr2Pi .rough-node .label text,#mermaid-svg-HaEJzqzcYaifr2Pi .node .label text,#mermaid-svg-HaEJzqzcYaifr2Pi .image-shape .label,#mermaid-svg-HaEJzqzcYaifr2Pi .icon-shape .label{text-anchor:middle;}#mermaid-svg-HaEJzqzcYaifr2Pi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-HaEJzqzcYaifr2Pi .rough-node .label,#mermaid-svg-HaEJzqzcYaifr2Pi .node .label,#mermaid-svg-HaEJzqzcYaifr2Pi .image-shape .label,#mermaid-svg-HaEJzqzcYaifr2Pi .icon-shape .label{text-align:center;}#mermaid-svg-HaEJzqzcYaifr2Pi .node.clickable{cursor:pointer;}#mermaid-svg-HaEJzqzcYaifr2Pi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-HaEJzqzcYaifr2Pi .arrowheadPath{fill:#333333;}#mermaid-svg-HaEJzqzcYaifr2Pi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-HaEJzqzcYaifr2Pi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-HaEJzqzcYaifr2Pi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-HaEJzqzcYaifr2Pi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-HaEJzqzcYaifr2Pi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-HaEJzqzcYaifr2Pi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster text{fill:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi .cluster span{color:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-HaEJzqzcYaifr2Pi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-HaEJzqzcYaifr2Pi rect.text{fill:none;stroke-width:0;}#mermaid-svg-HaEJzqzcYaifr2Pi .icon-shape,#mermaid-svg-HaEJzqzcYaifr2Pi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-HaEJzqzcYaifr2Pi .icon-shape p,#mermaid-svg-HaEJzqzcYaifr2Pi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-HaEJzqzcYaifr2Pi .icon-shape .label rect,#mermaid-svg-HaEJzqzcYaifr2Pi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-HaEJzqzcYaifr2Pi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-HaEJzqzcYaifr2Pi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-HaEJzqzcYaifr2Pi :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} git fetch
git merge / rebase
git push
git pull = fetch + merge
远程仓库
远程跟踪分支 origin/main
本地分支 main
6.3 远程跟踪分支机制
- 每个远程分支在本地都有对应的远程跟踪分支 ,如
origin/main。 fetch更新这些远程跟踪分支,反映「远程现在的状态」。- 本地分支与远程跟踪分支建立**上游(upstream)**关系后,可直接
git push/git pull。
bash
git branch --set-upstream-to=origin/main main # 手动设置上游
git push -u origin main # 推送并同时设置上游
git branch -vv # 查看上游关联
6.4 push 的细节
bash
git push # 推送到上游(若已设置)
git push origin main # 推送到 origin 的 main
git push --force # 强推(危险,覆盖远程)
git push --force-with-lease # 安全强推:仅当远程没被别人改动时才覆盖
git push --delete origin <branch> # 删除远程分支
6.5 协作范式
Fork + Pull Request(开源常见):
- Fork 主仓库到自己的账号
- Clone 自己的 fork,新建 feature 分支开发
- Push 到自己的 fork
- 发起 PR 请求合并到主仓库
- 评审通过后合并
直接分支 + MR/PR(团队常见):
- 从主干拉分支开发
- Push 分支到共享仓库
- 发起 MR/PR,走评审
- 合并到主干,删除特性分支
第 7 章 · 撤销与修复(场景速查)
7.1 「改错了怎么办」对照表
| 场景 | 命令 |
|---|---|
| 丢弃工作区某文件的改动 | git restore <file> |
| 撤销 add(暂存区 → 工作区) | git restore --staged <file> |
| 撤销最近一次 commit(保留改动) | git reset --soft HEAD~1 |
| 撤销 commit 和 add(改动回工作区) | git reset HEAD~1 |
| 彻底回到某次提交(丢弃改动) | git reset --hard <commit> |
| 撤销某次提交但保留历史(协作安全) | git revert <commit> |
| 修改上一次提交信息 | git commit --amend -m "..." |
| 找回误删的提交 | git reflog + git reset --hard <hash> |
7.2 找回「丢失」的提交
Git 的提交只要「可达」(从某个 ref 能追溯)就不会真正丢失,reflog 记录了所有指针移动:
bash
git reflog # 查看 HEAD 移动历史
git reflog show <branch> # 查看某分支的移动历史
git reset --hard <hash> # 回到 reflog 中的某个位置找回提交
7.3 修改历史
bash
git commit --amend # 修改最近一次提交
git rebase -i HEAD~N # 交互式重写最近 N 次提交
git filter-branch / git filter-repo # 大规模重写历史(如清除大文件、密钥)
改写历史后,若已推送,用
git push --force-with-lease更新远程(协作需谨慎沟通)。
第 8 章 · Git 内部原理
8.1 .git 目录结构
text
.git/
├── HEAD # 指向当前分支/提交
├── config # 仓库级配置
├── index # 暂存区(二进制)
├── objects/ # 所有对象(blob/tree/commit/tag)
├── refs/
│ ├── heads/ # 本地分支指针
│ ├── tags/ # 标签
│ └── remotes/ # 远程跟踪分支
├── hooks/ # 钩子脚本
└── logs/ # reflog
8.2 对象与哈希实验
Git 底层命令(plumbing)可以直观看到对象存储:
bash
git hash-object -w <file> # 计算并存储一个 blob 对象
git cat-file -p <hash> # 查看对象内容
git cat-file -t <hash> # 查看对象类型(blob/tree/commit/tag)
git ls-tree <hash> # 查看 tree 内容
git count-objects -v # 查看对象数量
对象按哈希命名,前两位为目录名,剩余为文件名:
text
.git/objects/9f/ceb02d0ae598e95dc970b74767f19372d61af8
8.3 打包与垃圾回收
- 松散对象(loose):单独存放的对象。
- 打包(packfile):Git 会把松散对象打包压缩,减少体积。
- 垃圾回收:删除不可达对象。
bash
git gc # 垃圾回收 + 打包
git gc --aggressive # 激进回收(更彻底,更耗时)
8.4 plumbing vs porcelain
- porcelain(上层) :面向用户的友好命令,如
add、commit、log。 - plumbing(底层) :面向脚本/内部,如
hash-object、cat-file、rev-parse、update-ref。
了解底层命令有助于真正理解 Git 的「内容寻址」本质,也方便编写自动化脚本。
第 9 章 · 钩子与自动化
9.1 常用钩子
钩子位于 .git/hooks/,文件名去掉 .sample 后缀即生效(也可用 core.hooksPath 自定义目录)。
| 钩子 | 触发时机 | 常见用途 |
|---|---|---|
pre-commit |
commit 之前 | 代码检查、格式化、运行测试 |
commit-msg |
写提交信息后 | 校验提交信息规范 |
pre-push |
push 之前 | 跑完整测试,防止推坏代码 |
post-commit |
commit 之后 | 通知、日志 |
post-merge |
merge 之后 | 更新依赖、提醒 |
9.2 与工具链集成
- husky:前端项目管理钩子的常用工具,跨团队共享钩子配置。
- lint-staged:只对暂存的文件跑 lint/格式化,速度快。
- commitlint:校验提交信息是否符合 Conventional Commits。
bash
# husky 4 语法(老)
"husky": { "hooks": { "pre-commit": "lint-staged" } }
# husky 7+ 语法(新)
npx husky add .husky/pre-commit "npx lint-staged"
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
9.3 CI 集成思路
- 在 CI 里跑完整测试、构建、lint
- PR 触发自动检查,通过后才能合并
- 合并后自动部署(CD)、自动生成 changelog / tag
第 10 章 · 工程化使用方案
10.1 大仓库治理
| 方案 | 适用场景 | 说明 |
|---|---|---|
| Git LFS | 大文件(媒体、二进制) | 用指针文件代替大文件,真实文件存远端 LFS 存储 |
| sparse-checkout | 超大仓库只需部分目录 | 只检出需要的子目录 |
| submodule | 引用其他独立仓库 | 主仓库只记录子模块的 commit 引用 |
| subtree | 将外部仓库并入子目录 | 历史并入,管理更简单(相对 submodule) |
bash
# Git LFS
git lfs install
git lfs track "*.psd"
# sparse-checkout
git sparse-checkout init --cone
git sparse-checkout set src docs
# submodule
git submodule add <url> <path>
git submodule update --init --recursive
# subtree
git subtree add --prefix=lib/<name> <url> main --squash
git subtree pull --prefix=lib/<name> <url> main --squash
10.2 monorepo vs multirepo
| 维度 | monorepo | multirepo |
|---|---|---|
| 结构 | 多项目放一个仓库 | 每个项目独立仓库 |
| 优点 | 原子提交、统一依赖、易于重构 | 仓库小、权限隔离清晰 |
| 缺点 | 仓库大、构建复杂 | 跨项目改动需多仓库协作 |
| 代表 | Google、Facebook 及很多前端团队 | 传统微服务团队 |
10.3 代码评审与保护分支
- 主干设为保护分支:禁止直接 push,必须走 PR/MR。
- 要求 review 通过、CI 通过才能合并。
- 合并策略选择:merge commit / squash merge / rebase merge。
10.4 版本发布与 tag
- 用语义化版本 (SemVer):
主版本.次版本.修订号,如v1.4.2。 - 发布时打附注标签,关联发布说明。
bash
git tag -a v1.4.2 -m "Release 1.4.2"
git push origin v1.4.2
10.5 团队协作规范样例
- 分支命名 :
feat/xxx、fix/xxx、chore/xxx、hotfix/xxx。 - 提交信息:遵循 Conventional Commits(见第 5 章)。
- 合并策略:统一约定 merge / squash,保持历史一致。
- 保护分支 :
main/master禁止直接 push。 - 大文件:统一用 Git LFS,避免仓库膨胀。
附录 · 速查表
A.1 常用命令速查
| 操作 | 命令 |
|---|---|
| 查看状态 | git status |
| 暂存 | git add <file> / git add . |
| 提交 | git commit -m "msg" |
| 查看差异 | git diff / git diff --staged |
| 查看历史 | git log --oneline --graph --all |
| 新建/切换分支 | git switch -c <name> / git switch <name> |
| 合并 | git merge <branch> |
| 变基 | git rebase <branch> / git rebase -i HEAD~N |
| 拉取 | git pull / git pull --rebase |
| 推送 | git push / git push -u origin <branch> |
| 暂存现场 | git stash / git stash pop |
| 打标签 | git tag -a v1.0.0 -m "..." |
| 撤销提交 | git reset --soft HEAD~1 |
| 回滚提交 | git revert <commit> |
| 拾取提交 | git cherry-pick <commit> |
| 找回提交 | git reflog |
A.2 高频场景速查
| 场景 | 命令 |
|---|---|
| 改错了工作区 | git restore <file> |
| 改错了暂存区 | git restore --staged <file> |
| 提交信息写错了 | git commit --amend -m "..." |
| 提交到错误分支 | git reset --soft HEAD~1 + git switch 到正确分支再提交 |
| 合并冲突了 | 编辑文件 → git add → git commit / git rebase --continue |
| 想放弃合并 | git merge --abort / git rebase --abort |
| 远端有别人改动 | git pull --rebase 后再 push |
| 强推但怕覆盖别人 | git push --force-with-lease |
| 误删分支 | git reflog 找到 commit 后 git branch <name> <hash> |
持续补充中。建议配合实际操作练习,尤其是
reset、rebase -i、冲突解决这三块,是理解 Git 的分水岭。