Git 高级用法与全命令手册(架构师版)
版本:2025-10 · 面向资深开发/架构师 · Markdown 版
目标:不仅会“用命令”,更要理解 Git 的数据模型与命令语义,能在复杂协作中自如切换fetch/merge/checkout/rebase
等策略。
目录(Table of Contents)
- 0. 心智模型与数据结构
- 1. 工作区/暂存区/本地库/远程库
- 2. 四大灵魂命令深度讲解:fetch/merge/checkout-rebase
- 3. Porcelain(高阶“瓷器”命令)详解与实战
- 3.1 初始化与配置:init / clone / config / help
- 3.2 检视与比较:status / diff / log / show / describe / grep
- 3.3 文件与暂存:add / rm / mv / restore / checkout
- 3.4 提交与历史:commit / reset / revert / reflog / notes
- 3.5 分支与协作:branch / merge / rebase / cherry-pick / range-diff
- 3.6 远程:remote / fetch / pull / push / submodule / subtree
- 3.7 暂存工作:stash / worktree
- 3.8 维护与清理:gc / fsck / prune / repack / clean / sparse-checkout
- 3.9 发布与打包:tag / archive / format-patch / am / apply / notes
- 3.10 调试与定位:bisect / blame / shortlog
- 4. Plumbing(底层“管道”命令)索引 + 典型用法
- 5. 协作策略与最佳实践
- 6. 配置项对行为的影响(必须掌握)
- 7. 性能与大仓优化:浅克隆/部分克隆/稀疏检出
- 8. 常见场景手册(一步到位的命令序列)
- 9. 命令速查表(Cheat Sheet)
- 附录A:全部常见命令清单与一句话语义
- 附录B:常见误区与救命技巧
0. 心智模型与数据结构
- Git 是内容寻址的对象数据库:
blob
(文件内容)、tree
(目录快照)、commit
(指针+元数据+父指针)、tag
。 - **引用(ref)**是指针(人类可读名):
refs/heads/main
、refs/remotes/origin/main
、HEAD
。 - 工作区 → 暂存区(index) → 本地库是三个层次;远程库是第四层。
编辑文件 → git add(写入 index) → git commit(写对象,移动分支指针) → git push(更新远程引用)
1. 工作区/暂存区/本地库/远程库
- 工作区:你编辑的目录。
- 暂存区(index):将将要提交的文件集合(
git add
写入)。 - 本地库:
.git/objects
+ 各种 ref。 - 远程库:协作副本(GitHub/GitLab)。
可视化查看差异:
git status # 哪些文件变了/已暂存
git diff # 工作区 vs 暂存
git diff --cached # 暂存 vs 本地库
git diff HEAD # 工作区 vs 最新提交
2. 四大灵魂命令深度讲解:fetch/merge/checkout-rebase
2.1 git fetch
(只更新远程引用,不动工作区)
- 作用:下载远程对象 + 更新
refs/remotes/origin/*
;不改变当前分支/工作区。 - 常用:
git fetch # 默认远程(origin)所有分支
git fetch --all # 所有 remote
git fetch origin main # 拉取 origin/main
git fetch --prune # 顺便清理远程已删分支的本地引用
git fetch --tags # 同步标签
git fetch --depth=1 # 浅抓取(仅最近提交)
- 思维图:
main: A--B (不变)
origin/main: A--B--C (fetch 后更新)
2.2 git merge
(把另一条时间线并入当前分支)
- 语义:把 另一个分支的 tip 合并进当前分支,可能产生合并提交。
- 常用:
git checkout main
git merge feature/login # 尝试快进(fast-forward),否则产生 merge commit
git merge --no-ff feature/login # 即使可快进,也强制产生 merge commit,保留分支轨迹
git merge --ff-only origin/main # 仅允许快进,不能则失败
git merge -s ours other # 冲突时偏向“ours”策略(慎用)
git merge -X theirs other # 单文件层面的冲突偏向“theirs”(ORT策略参数)
git merge --abort # 取消进行中的合并
- 冲突解决步骤:编辑冲突文件 →
git add
→git commit
(或--continue
)。 - 何时用:公共分支整合、保留真实历史。
2.3 git checkout
/ git switch
/ git restore
(切换与恢复)
- 切换分支:
git switch main # 推荐
git checkout main # 老命令
git switch -c feature-x # 新建并切换
- 恢复文件:
git restore path/file # 用 index 覆盖工作区
git restore --source=HEAD file # 用某版本覆盖
git checkout -- file # 老写法(等价于上面)
- 建议:用
switch
切分支、restore
恢复文件,更语义化。
2.4 git rebase
(重放提交,平直化历史)
- 语义:把当前分支的提交 “搬到” 目标分支 tip 之后,形成新的 commit id(历史被重写)。
- 常用:
git switch feature
git rebase main # 基本重放
git rebase --onto main A B # 将 A..B 的提交搬到 main 后
git rebase -i main # 交互式:reword/edit/squash/fixup
git rebase --rebase-merges # 尝试保留合并结构
git rebase --autosquash # 自动把 fixup!/squash! 提交压进前面的提交
git rebase --autostash # 重排前先暂存未提交更改
git rebase --abort | --continue | --skip
- 原则:不要对已推送到公共仓库的分支做 rebase(除非全员知情并配合)。
- 何时用:个人分支对齐主干、发布前压历史。
Rebase 与 Merge 的选择
- 团队可审计历史优先 → merge(保留真实分歧与合流点);
- 线性历史、整洁审阅优先 → rebase(个人分支用,PR 前整理)。
2.5 fetch
/merge
/pull
/rebase
对比速览
命令 | 是否改动工作区 | 是否新提交 | 是否改历史 | 典型用途 |
---|---|---|---|---|
fetch | 否 | 否 | 否 | 更新远程引用 |
merge | 是 | 可能(merge commit) | 否 | 汇合两条时间线 |
pull | 是 | 取决于配置(merge 或 rebase) | 取决于 rebase | “抓取+整合”的快捷方式 |
rebase | 是 | 否(但会生成新 commit id) | 是 | 平直化历史 / 个人分支对齐 |
git pull --rebase
=git fetch
+git rebase
。
建议设置:git config pull.rebase true
(或按团队约定)。
3. Porcelain(高阶“瓷器”命令)详解与实战
3.1 初始化与配置:init
/ clone
/ config
/ help
git init [--initial-branch=main] # 新仓库
git clone <url> [dir] # 克隆(可加 --depth=1 浅克隆)
git config --global user.name "You"
git config --global user.email you@example.com
git config --global init.defaultBranch main
git config --global pull.rebase true
git help <cmd> | git <cmd> -h
3.2 检视与比较:status
/ diff
/ log
/ show
/ describe
/ grep
git status -sb
git diff [--cached] [--name-only]
git log --graph --oneline --decorate --all
git show <commit>[:path] # 看提交/文件在该提交的内容
git describe --tags # 最近的可达标签描述
git grep -n "pattern" # 在历史/工作区中搜索
3.3 文件与暂存:add
/ rm
/ mv
/ restore
/ checkout
git add -p # 交互式分块暂存
git rm path # 删除并暂存删除
git mv a b # 重命名(保持历史追踪)
git restore --staged file # 从暂存区移除(保留工作区改动)
3.4 提交与历史:commit
/ reset
/ revert
/ reflog
/ notes
git commit -m "feat: ..." # 新提交
git commit --amend # 修正上一提交
git reset --soft HEAD~1 # 回退指针,保留暂存+工作区
git reset --mixed HEAD~1 # (默认)回退并清空暂存,保留工作区
git reset --hard HEAD~1 # 全部回退(危险)
git revert <commit> # 生成反向提交(不改历史)
git reflog # 所有 HEAD 变动轨迹,可救急找回
git notes add -m "extra meta" <commit> # 给提交加“旁注”
3.5 分支与协作:branch
/ merge
/ rebase
/ cherry-pick
/ range-diff
git branch -vv # 查看本地分支及上游
git branch -d|-D feature # 删除分支(-D 强制)
git cherry-pick <commit> # 拣选单个提交到当前分支
git cherry-pick A..B # 拣选一段(不含 A,含 B)
git range-diff old..new # 比较两个范围的差异(rebase 后审阅)
3.6 远程:remote
/ fetch
/ pull
/ push
/ submodule
/ subtree
git remote -v
git remote add origin <url>
git remote set-url origin <new-url>
git fetch --all --prune
git pull --rebase --autostash
git push -u origin feature # 建立上游
git push --force-with-lease # 安全强推(对比远端快照)
# 子模块
git submodule add <url> path
git submodule update --init --recursive
git submodule deinit -f path
# (备选)git subtree(非内置命令,常装作扩展)
3.7 暂存工作:stash
/ worktree
git stash push -m "wip: xx" [-- path] # 保存未提交改动
git stash list
git stash show -p stash@{0}
git stash apply|pop stash@{0}
git worktree add ../repo-fix <branch|commit> # 多工作区并行开发
git worktree list
git worktree remove ../repo-fix
3.8 维护与清理:gc
/ fsck
/ prune
/ repack
/ clean
/ sparse-checkout
git gc --aggressive # 垃圾回收与紧缩(谨慎用在大仓)
git fsck # 完整性检查
git prune # 移除不可达对象(配合 reflog 过期)
git repack -ad # 重新打包对象,优化大小
git clean -fdx # 清理未跟踪/忽略文件(危险)
git sparse-checkout init --cone
git sparse-checkout set dirA dirB # 只检出部分目录
3.9 发布与打包:tag
/ archive
/ format-patch
/ am
/ apply
/ notes
git tag -a v1.2.3 -m "release"
git push --tags
git archive -o release.zip HEAD # 导出归档
git format-patch origin/main..HEAD # 生成补丁序列
git am *.patch # 应用邮件补丁
git apply --3way diff.patch # 三方合并应用补丁
3.10 调试与定位:bisect
/ blame
/ shortlog
git bisect start
git bisect bad # 当前坏
git bisect good <commit> # 某历史好
# 按提示二分检索直至定位坏提交
git blame -L 10,50 path/file # 谁在何时改了哪些行
git shortlog -sn --all # 贡献者统计
4. Plumbing(底层“管道”命令)索引 + 典型用法
这些命令操纵对象/引用本体,理解它们能从根上吃透 Git。
- 对象/树/提交:
git hash-object
、git cat-file
、git ls-tree
、git write-tree
、git commit-tree
、git mktree
、git mktag
- 引用:
git rev-parse
、git update-ref
、git for-each-ref
、git show-ref
、git symbolic-ref
、git rev-list
- 索引/合并:
git update-index
、git read-tree
、git merge-tree
、git unpack-objects
、git pack-objects
、git verify-pack
- 替换/过滤:
git replace
、git filter-branch
(弃用,改用git filter-repo
外部工具) - 差异族:
git diff-tree
、git diff-index
、git diff-files
、git whatchanged
(历史命令,等价 log+diff)
示例:从零造一次提交(演示性)
echo hi | git hash-object -w --stdin # 产出 blob id
git mktree # 交互读入 "100644 blob <blob-id>\tfile"
git commit-tree <tree-id> -m "first" # 产出 commit id
git update-ref refs/heads/main <commit-id> # 建立分支指向它
5. 协作策略与最佳实践
- 主干优先(Trunk-based):小步快跑,PR 前
rebase
整理历史;集成用merge --ff-only
或 squash 合并。 - GitFlow:有
develop/release/hotfix
等,适合重发布流程。 - PR 审阅:
range-diff
比较 rebase 前后是否只重排不改语义。 - 强制推送:优先
--force-with-lease
,避免覆盖他人更新。 - 提交信息规范:Conventional Commits;签名
git config commit.gpgsign true
。
6. 配置项对行为的影响(必须掌握)
# 拉取策略
git config --global pull.rebase true # pull 默认 rebase
git config --global rebase.autoStash true # rebase 前自动 stash
git config --global fetch.prune true # fetch 自动 prune
git config --global merge.ff only # 只允许快进合并
git config --global rerere.enabled true # 记忆并自动复用冲突解决
# 行结尾与编码
git config --global core.autocrlf input # mac/linux 推荐,windows 可设 true
git config --global core.ignorecase false
# 默认分支
git config --global init.defaultBranch main
# 推送策略
git config --global push.default simple # 或 upstream/current
# 凭证
git config --global credential.helper store|manager-core|osxkeychain
# 签名
git config --global user.signingkey <GPG_KEY>
git config --global commit.gpgsign true
7. 性能与大仓优化:浅克隆/部分克隆/稀疏检出
git clone --depth=1 <url> # 浅克隆
git clone --filter=blob:none <url> # 部分克隆(需要较新服务器支持)
git sparse-checkout init --cone
git sparse-checkout set src/ docs/
git repack -Ad && git gc
8. 常见场景手册(一步到位的命令序列)
(1)把远程更新整合到当前分支,保持线性历史
git fetch origin
git rebase origin/main
(2)提交搞错信息,修正文案
git commit --amend -m "fix: correct message"
(3)回滚一个已发布的坏提交(不改历史)
git revert <bad-commit>
git push
(4)误删分支找回
git reflog
git branch recover <sha-from-reflog>
(5)把 bugfix 从主干“拣选”到旧分支
git switch release/1.2
git cherry-pick <fix-commit>
(6)保存现场切分支修紧急问题
git stash push -m "wip"
git switch -c hotfix/xxx
# ... 修完回到原分支
git switch -
git stash pop
9. 命令速查表(Cheat Sheet)
- 检查:
status
diff
log
show
grep
blame
reflog
- 变更:
add
restore
rm
mv
commit
reset
revert
- 分支:
branch
switch
checkout
merge
rebase
cherry-pick
- 远程:
remote
fetch
pull
push
submodule
- 打包:
tag
archive
format-patch
am
apply
- 暂存/多工:
stash
worktree
- 维护:
gc
fsck
prune
repack
clean
sparse-checkout
附录A:全部常见命令清单与一句话语义
注:覆盖绝大多数日常/高阶 + 常见 plumbing。
仓库/配置:init
(新仓库) clone
(克隆) config
(配置) help
(帮助) credential
(凭证)
检视/比较:status
diff
log
show
describe
shortlog
whatchanged
grep
reflog
文件/暂存:add
rm
mv
restore
checkout
(老) update-index
(plumbing)
提交/历史:commit
reset
revert
cherry-pick
rebase
merge
range-diff
notes
分支/引用:branch
switch
worktree
tag
for-each-ref
(plumbing) show-ref
symbolic-ref
远程:remote
fetch
pull
push
submodule
(子仓) ls-remote
打包/补丁:archive
format-patch
am
apply
patch-id
定位/调试:bisect
blame
维护/对象:gc
fsck
prune
repack
count-objects
verify-pack
hash-object
cat-file
ls-tree
write-tree
commit-tree
mktree
mktag
rev-parse
rev-list
describe
replace
大仓/稀疏:sparse-checkout
worktree
部分克隆(clone --filter
)
附录B:常见误区与救命技巧
- 误区:“
pull
总是安全的。”- 说明:它可能做 merge(产生新提交)或 rebase(重写历史)。先
fetch
再决定策略更稳。
- 说明:它可能做 merge(产生新提交)或 rebase(重写历史)。先
- 误区:“
reset --hard
就是回滚。”- 说明:它是移动分支并重置工作区,会丢改动;常用
revert
回滚已发布提交。
- 说明:它是移动分支并重置工作区,会丢改动;常用
- 误区:“强推直接
--force
。”- 建议用
--force-with-lease
,避免覆盖他人的新增提交。
- 建议用
- 救命:
reflog
能找回几乎所有“指针移动”前的状态。 - 冲突复用:打开
rerere.enabled
,多分支重复冲突时自动复用解决结果。
版权与使用:本手册可在团队内自由传播与二开。若需 PDF/HTML 版本可由本 Markdown 导出。
建议:把本文件放到你的知识库(Obsidian/Notion)并收藏关键段落。