git的命令:-Git 命令大全:从入门到高手,100 个最常用命令速查(2027 版) - 2 仓库维护

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力神器 👁️ 152 次阅读

Git 命令大全:从入门到高手,100 个最常用命令速查(2026 版)

收藏这一篇,告别 Git 命令遗忘! 从 git init 到 git bisect,100 个命令按难度、场景、频率分类,附带代码示例和避坑指南,堪称 Git 版的"cmd 命令大全"。

 目录

一、Git 命令速查总览二、入门篇:20 个必会命令(⭐)三、进阶篇:30 个常用命令(⭐⭐)四、高级篇:30 个进阶命令(⭐⭐⭐)五、专家篇:20 个高阶命令(⭐⭐⭐⭐)六、场景速查索引七、Git 工作流程图解八、常见问题与避坑指南九、总结

一、Git 命令速查总览

1.1 命令难度金字塔

```mermaid

graph TB

subgraph 专家级 ⭐⭐⭐⭐

A1[git bisect]

A2[git filter-branch]

A3[git reflog]

A4[git submodule]

end

subgraph 高级 ⭐⭐⭐

B1[git rebase]

B2[git cherry-pick]

B3[git stash]

B4[git reset --hard]

end

subgraph 进阶 ⭐⭐

C1[git branch]

C2[git merge]

C3[git remote]

C4[git fetch]

end

subgraph 入门 ⭐

D1[git init]

D2[git add]

D3[git commit]

D4[git push]

end

专家级 ⭐⭐⭐⭐ -->高级 ⭐⭐⭐

高级 ⭐⭐⭐ -->进阶 ⭐⭐

进阶 ⭐⭐ -->入门 ⭐

### 1.2 命令使用频率分布

| 频率等级 | 使用占比 | 典型命令 | 掌握优先级 |

|:---:|:---:|:---|:---:|

|  高频 | 60% | `add`, `commit`, `push`, `pull`, `status` | P0 |

|  中频 | 25% | `branch`, `merge`, `stash`, `rebase` | P1 |

|  低频 | 10% | `cherry-pick`, `bisect`, `reflog` | P2 |

| ❄️ 冷门 | 5% | `filter-branch`, `submodule` | P3 |

### 1.3 命令场景分类

| 场景 | 命令数量 | 核心命令 |

|:---|:---:|:---|

| ️ 初始化与配置 | 10 | `init`, `clone`, `config` |

|  提交与暂存 | 15 | `add`, `commit`, `restore` |

|  分支管理 | 20 | `branch`, `checkout`, `switch` |

|  合并与变基 | 15 | `merge`, `rebase`, `cherry-pick` |

|  远程仓库 | 15 | `remote`, `fetch`, `push`, `pull` |

|  查看与搜索 | 15 | `log`, `diff`, `grep`, `show` |

| ️ 调试与修复 | 10 | `bisect`, `blame`, `reflog` |

---

## 二、入门篇:20 个必会命令(⭐)

> **适用人群**:Git 新手、刚入职的开发者

> **学习目标**:能够完成日常的代码提交、拉取、推送操作

### 2.1 初始化与克隆

####  `git init` — 创建新仓库

**使用频率**:每次新项目 | **难度**:⭐

```bash

# 在当前目录创建新仓库

git init

# 在指定目录创建新仓库

git init my-project

# 创建裸仓库(用于服务器)

git init --bare

场景:开始一个新项目时,将当前目录变成 Git 仓库。

 git clone — 克隆远程仓库

使用频率:每周 2-3 次 | 难度:⭐

# 克隆远程仓库

git clone https://github.com/user/repo.git

# 克隆到指定目录

git clone https://github.com/user/repo.git my-folder

# 浅克隆(只克隆最近一次提交,节省空间)

git clone --depth 1 https://github.com/user/repo.git

# 克隆指定分支

git clone -b develop https://github.com/user/repo.git

场景:加入新团队、参与开源项目时,获取远程代码。

2.2 配置管理

 git config — 配置 Git

使用频率:每台电脑一次 | 难度:⭐

# 设置用户名和邮箱(全局)

git config --global user.name "Your Name"

git config --global user.email "your@email.com"

# 查看当前配置

git config --list

# 设置默认编辑器

git config --global core.editor "code --wait"

# 设置默认分支名

git config --global init.defaultBranch main

# 设置别名(提高效率)

git config --global alias.st status

git config --global alias.co checkout

git config --global alias.br branch

git config --global alias.ci commit

场景:新电脑环境配置、自定义 Git 行为。

2.3 基本操作

 git status — 查看状态

使用频率:每天 10+ 次 | 难度:⭐

# 查看工作区状态

git status

# 简洁模式

git status -s

# 显示分支信息

git status -sb

输出解读:

On branch main

Changes to be committed:

(use "git restore --staged..." to unstage)

modified: index.js # 已暂存,等待提交

Changes not staged for commit:

(use "git add..." to update what will be committed)

modified: app.js # 已修改,未暂存

Untracked files:

(use "git add..." to include in what will be committed)

new.js # 新文件,未跟踪

 git add — 添加到暂存区

使用频率:每天 20+ 次 | 难度:⭐

# 添加单个文件

git add index.js

# 添加多个文件

git add index.js app.js utils.js

# 添加所有修改

git add .

# 添加所有文件(包括删除)

git add -A

# 交互式添加(逐块选择)

git add -p

# 添加所有 .js 文件

git add "*.js"

避坑指南:

git add . 只添加当前目录及子目录的文件git add -A 添加整个仓库的所有变更

 git commit — 提交更改

使用频率:每天 10+ 次 | 难度:⭐

# 提交暂存区的更改

git commit -m "feat: 添加用户登录功能"

# 提交并添加详细描述

git commit -m "feat: 添加用户登录功能" -m "详细描述:

- 支持 OAuth2.0 登录

- 添加登录状态持久化

- 修复登录页面样式问题"

# 跳过暂存区直接提交

git commit -a -m "fix: 修复登录bug"

# 修改上次提交信息

git commit --amend -m "新的提交信息"

# 修改上次提交(不修改信息)

git commit --amend --no-edit

提交信息规范(Conventional Commits):

类型说明示例feat新功能feat: 添加用户注册功能fix修复 bugfix: 修复登录验证问题docs文档更新docs: 更新 READMEstyle代码格式style: 格式化代码refactor重构refactor: 重构用户模块test测试test: 添加单元测试chore构建/工具chore: 更新依赖版本

 git push — 推送到远程

使用频率:每天 5+ 次 | 难度:⭐

# 推送当前分支到远程

git push

# 首次推送并设置上游分支

git push -u origin main

# 推送所有分支

git push --all

# 强制推送(危险!)

git push --force

# 安全强制推送(推荐)

git push --force-with-lease

# 推送标签

git push --tags

避坑指南:

--force 会覆盖远程历史,团队协作慎用--force-with-lease 更安全,会检查远程是否有新提交

 git pull — 拉取远程更新

使用频率:每天 5+ 次 | 难度:⭐

# 拉取当前分支的更新

git pull

# 拉取指定分支

git pull origin main

# 拉取并变基(推荐)

git pull --rebase

# 拉取所有分支信息

git pull --all

git pull vs git fetch:

git pull = git fetch + git merge

git pull --rebase = git fetch + git rebase

2.4 查看历史

 git log — 查看提交历史

使用频率:每天 5+ 次 | 难度:⭐

# 查看提交历史

git log

# 单行显示

git log --oneline

# 图形化显示分支

git log --oneline --graph --all

# 显示最近 N 次提交

git log -5

# 显示文件变更统计

git log --stat

# 显示每次提交的差异

git log -p

# 按作者筛选

git log --author="张三"

# 按时间筛选

git log --since="2026-01-01" --until="2026-03-01"

# 按提交信息筛选

git log --grep="fix"

# 自定义格式

git log --pretty=format:"%h - %an, %ar : %s"

格式占位符:

占位符说明%H完整哈希值%h短哈希值%an作者名字%ae作者邮箱%ad作者日期%ar作者日期(相对)%s提交说明

 git diff — 查看差异

使用频率:每天 5+ 次 | 难度:⭐

# 查看工作区与暂存区的差异

git diff

# 查看暂存区与最新提交的差异

git diff --staged

git diff --cached # 同上

# 查看工作区与最新提交的差异

git diff HEAD

# 查看两个提交之间的差异

git diff commit1 commit2

# 查看指定文件的差异

git diff -- path/to/file

# 只显示文件名

git diff --name-only

# 显示统计信息

git diff --stat

2.5 入门命令速查表

命令说明频率难度git init创建新仓库⭐git clone克隆远程仓库⭐git config配置 Git⭐git status查看状态⭐git add添加到暂存区⭐git commit -m "msg"提交更改⭐git push推送到远程⭐git pull拉取远程更新⭐git log查看提交历史⭐git diff查看差异⭐git rm删除文件⭐git mv重命名文件⭐git restore撤销修改⭐git restore --staged取消暂存⭐git show显示提交详情⭐git blame查看文件修改历史⭐git clean -fd删除未跟踪文件⭐git version查看 Git 版本❄️⭐git help查看帮助❄️⭐git archive导出仓库❄️⭐

三、进阶篇:30 个常用命令(⭐⭐)

适用人群:有 1-2 年经验的开发者 学习目标:熟练使用分支、合并、远程仓库操作

3.1 分支管理

 git branch — 分支操作

使用频率:每天 3+ 次 | 难度:⭐⭐

# 查看所有本地分支

git branch

# 查看所有远程分支

git branch -r

# 查看所有分支(本地+远程)

git branch -a

# 创建新分支

git branch feature/login

# 创建并切换分支

git branch feature/login && git checkout feature/login

# 或者使用

git checkout -b feature/login

# 删除分支

git branch -d feature/login

# 强制删除未合并的分支

git branch -D feature/login

# 重命名分支

git branch -m old-name new-name

# 查看分支详细信息

git branch -vv

# 查看已合并到当前分支的分支

git branch --merged

# 查看未合并到当前分支的分支

git branch --no-merged

 git checkout — 切换分支/恢复文件

使用频率:每天 5+ 次 | 难度:⭐⭐

# 切换分支

git checkout main

# 创建并切换分支

git checkout -b feature/login

# 基于远程分支创建本地分支

git checkout -b feature/login origin/feature/login

# 切换到上一个分支

git checkout -

# 切换到指定提交(游离状态)

git checkout abc123

# 恢复工作区文件

git checkout -- file.js

# 恢复暂存区文件到工作区

git checkout HEAD -- file.js

注意:git checkout 功能太多,Git 2.23+ 推荐使用:

git switch — 切换分支git restore — 恢复文件

 git switch — 切换分支(新)

使用频率:每天 5+ 次 | 难度:⭐⭐

# 切换分支

git switch main

# 创建并切换分支

git switch -c feature/login

# 切换到上一个分支

git switch -

# 基于远程分支创建并切换

git switch -c feature/login origin/feature/login

3.2 合并操作

 git merge — 合并分支

使用频率:每周 3+ 次 | 难度:⭐⭐

# 合并指定分支到当前分支

git merge feature/login

# 合并并生成合并提交

git merge --no-ff feature/login

# 合并但不生成合并提交(快进合并)

git merge --ff-only feature/login

# 中止合并

git merge --abort

# 继续合并(解决冲突后)

git merge --continue

# 压缩合并(所有提交压缩为一个)

git merge --squash feature/login

合并策略对比:

策略说明适用场景--ff快进合并(默认)功能分支未偏离主分支--no-ff禁用快进合并保留分支历史--ff-only仅快进合并确保无分叉历史--squash压缩合并合并多个小提交

解决合并冲突:

# 1. 查看冲突文件

git status

# 2. 手动编辑冲突文件

# <<<<<<< HEAD

# 当前分支的内容

# =======

# 要合并分支的内容

# >>>>>>> feature/login

# 3. 标记为已解决

git add conflicted-file.js

# 4. 完成合并

git commit

3.3 远程仓库

 git remote — 管理远程仓库

使用频率:每周 2+ 次 | 难度:⭐⭐

# 查看远程仓库

git remote

# 查看远程仓库详细信息

git remote -v

# 添加远程仓库

git remote add origin https://github.com/user/repo.git

# 删除远程仓库

git remote remove origin

# 重命名远程仓库

git remote rename old-name new-name

# 修改远程仓库 URL

git remote set-url origin https://github.com/user/new-repo.git

# 查看远程仓库详情

git remote show origin

# 清理无效的远程分支引用

git remote prune origin

 git fetch — 获取远程更新

使用频率:每天 3+ 次 | 难度:⭐⭐

# 获取所有远程分支的更新

git fetch

# 获取指定远程仓库的更新

git fetch origin

# 获取指定分支的更新

git fetch origin main

# 获取所有远程仓库的更新

git fetch --all

# 获取并清理无效引用

git fetch -p

git fetch --prune # 同上

fetch vs pull:

git fetch → 只下载,不合并

git pull → 下载 + 自动合并

3.4 撤销操作

 git reset — 重置提交

使用频率:每周 2+ 次 | 难度:⭐⭐

# 软重置:保留工作区和暂存区

git reset --soft HEAD~1

# 混合重置:保留工作区,清空暂存区(默认)

git reset --mixed HEAD~1

git reset HEAD~1 # 同上

# 硬重置:清空工作区和暂存区(危险!)

git reset --hard HEAD~1

# 重置到指定提交

git reset --hard abc123

# 重置暂存区文件

git reset HEAD file.js

reset 模式对比:

模式工作区暂存区提交历史适用场景--soft✅ 保留✅ 保留❌ 撤销撤销提交,保留修改--mixed✅ 保留❌ 清空❌ 撤销撤销提交和暂存--hard❌ 清空❌ 清空❌ 撤销彻底回退(危险)

 git revert — 撤销提交(安全)

使用频率:每周 1+ 次 | 难度:⭐⭐

# 撤销最近一次提交

git revert HEAD

# 撤销指定提交

git revert abc123

# 撤销多个提交

git revert abc123 def456

# 撤销但不自动提交

git revert -n abc123

# 撤销合并提交

git revert -m 1 abc123

reset vs revert:

操作历史记录团队协作安全性reset修改历史❌ 不推荐⚠️ 危险revert新增提交✅ 推荐✅ 安全

3.5 暂存操作

 git stash — 暂存工作区

使用频率:每周 3+ 次 | 难度:⭐⭐

# 暂存当前工作

git stash

# 暂存并添加说明

git stash save "WIP: 登录功能"

# 查看暂存列表

git stash list

# 应用最近一次暂存

git stash apply

# 应用指定暂存

git stash apply stash@{2}

# 应用并删除暂存

git stash pop

# 删除最近一次暂存

git stash drop

# 删除指定暂存

git stash drop stash@{2}

# 清空所有暂存

git stash clear

# 查看暂存内容

git stash show

# 查看暂存详细差异

git stash show -p

# 从暂存创建分支

git stash branch feature/stashed-work

典型场景:

# 场景:正在开发功能 A,突然需要修复紧急 bug

# 1. 暂存当前工作

git stash save "WIP: 功能A开发中"

# 2. 切换到主分支修复 bug

git checkout main

git checkout -b hotfix/bug-123

# ... 修复并提交 ...

# 3. 回到功能分支,恢复工作

git checkout feature/A

git stash pop

3.6 标签管理

 git tag — 标签操作

使用频率:每月 2+ 次 | 难度:⭐⭐

# 查看所有标签

git tag

# 创建轻量标签

git tag v1.0.0

# 创建附注标签(推荐)

git tag -a v1.0.0 -m "版本 1.0.0 发布"

# 为指定提交创建标签

git tag -a v0.9.0 abc123 -m "版本 0.9.0"

# 查看标签信息

git show v1.0.0

# 删除本地标签

git tag -d v1.0.0

# 推送标签到远程

git push origin v1.0.0

# 推送所有标签

git push --tags

# 删除远程标签

git push origin --delete v1.0.0

# 按模式筛选标签

git tag -l "v1.*"

语义化版本号:

v1.2.3

│ │ │

│ │ └─ 补丁版本(bug 修复)

│ └─── 次版本(新功能,向后兼容)

└───── 主版本(重大变更,可能不兼容)

3.7 进阶命令速查表

命令说明频率难度git branch分支操作⭐⭐git checkout切换分支/恢复⭐⭐git switch切换分支(新)⭐⭐git restore恢复文件(新)⭐⭐git merge合并分支⭐⭐git remote远程仓库管理⭐⭐git fetch获取远程更新⭐⭐git reset重置提交⭐⭐git revert撤销提交⭐⭐git stash暂存工作区⭐⭐git tag标签操作⭐⭐git rebase变基操作⭐⭐⭐git cherry-pick精选提交⭐⭐⭐git reflog引用日志⭐⭐⭐git gc垃圾回收❄️⭐⭐git fsck检查仓库❄️⭐⭐git prune清理无效对象❄️⭐⭐git count-objects统计对象❄️⭐⭐git instawebWeb 界面❄️⭐⭐git request-pull生成拉取请求❄️⭐⭐

四、高级篇:30 个进阶命令(⭐⭐⭐)

适用人群:有 2-3 年经验的高级开发者 学习目标:掌握变基、精选提交、历史重写等高级操作

4.1 变基操作

 git rebase — 变基操作

使用频率:每周 3+ 次 | 难度:⭐⭐⭐

# 将当前分支变基到 main

git rebase main

# 交互式变基(最近 3 次提交)

git rebase -i HEAD~3

# 交互式变基(指定提交之后)

git rebase -i abc123

# 继续变基(解决冲突后)

git rebase --continue

# 跳过当前提交

git rebase --skip

# 中止变基

git rebase --abort

# 变基到远程分支

git rebase origin/main

# 自动解决冲突(使用当前分支)

git rebase -X ours main

# 自动解决冲突(使用目标分支)

git rebase -X theirs main

交互式变基操作:

git rebase -i HEAD~3

pick abc123 feat: 添加登录功能

squash def456 fix: 修复登录bug # 合并到上一个提交

reword ghi789 feat: 添加注册功能 # 修改提交信息

edit jkl012 fix: 修复样式问题 # 停下来修改

drop mno345 docs: 更新文档 # 删除提交

操作说明pick §使用提交reword ®使用提交,但修改信息edit (e)使用提交,但停下来修改squash (s)合并到前一个提交,保留信息fixup (f)合并到前一个提交,丢弃信息drop (d)删除提交

merge vs rebase:

#mermaid-svg-DlKr3rXh95dzSNxy{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-DlKr3rXh95dzSNxy .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-DlKr3rXh95dzSNxy .error-icon{fill:#552222;}#mermaid-svg-DlKr3rXh95dzSNxy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-DlKr3rXh95dzSNxy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-DlKr3rXh95dzSNxy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-DlKr3rXh95dzSNxy .marker.cross{stroke:#333333;}#mermaid-svg-DlKr3rXh95dzSNxy svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-DlKr3rXh95dzSNxy p{margin:0;}#mermaid-svg-DlKr3rXh95dzSNxy .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-DlKr3rXh95dzSNxy .cluster-label text{fill:#333;}#mermaid-svg-DlKr3rXh95dzSNxy .cluster-label span{color:#333;}#mermaid-svg-DlKr3rXh95dzSNxy .cluster-label span p{background-color:transparent;}#mermaid-svg-DlKr3rXh95dzSNxy .label text,#mermaid-svg-DlKr3rXh95dzSNxy span{fill:#333;color:#333;}#mermaid-svg-DlKr3rXh95dzSNxy .node rect,#mermaid-svg-DlKr3rXh95dzSNxy .node circle,#mermaid-svg-DlKr3rXh95dzSNxy .node ellipse,#mermaid-svg-DlKr3rXh95dzSNxy .node polygon,#mermaid-svg-DlKr3rXh95dzSNxy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DlKr3rXh95dzSNxy .rough-node .label text,#mermaid-svg-DlKr3rXh95dzSNxy .node .label text,#mermaid-svg-DlKr3rXh95dzSNxy .image-shape .label,#mermaid-svg-DlKr3rXh95dzSNxy .icon-shape .label{text-anchor:middle;}#mermaid-svg-DlKr3rXh95dzSNxy .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-DlKr3rXh95dzSNxy .rough-node .label,#mermaid-svg-DlKr3rXh95dzSNxy .node .label,#mermaid-svg-DlKr3rXh95dzSNxy .image-shape .label,#mermaid-svg-DlKr3rXh95dzSNxy .icon-shape .label{text-align:center;}#mermaid-svg-DlKr3rXh95dzSNxy .node.clickable{cursor:pointer;}#mermaid-svg-DlKr3rXh95dzSNxy .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-DlKr3rXh95dzSNxy .arrowheadPath{fill:#333333;}#mermaid-svg-DlKr3rXh95dzSNxy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-DlKr3rXh95dzSNxy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-DlKr3rXh95dzSNxy .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DlKr3rXh95dzSNxy .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-DlKr3rXh95dzSNxy .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DlKr3rXh95dzSNxy .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-DlKr3rXh95dzSNxy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-DlKr3rXh95dzSNxy .cluster text{fill:#333;}#mermaid-svg-DlKr3rXh95dzSNxy .cluster span{color:#333;}#mermaid-svg-DlKr3rXh95dzSNxy 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-DlKr3rXh95dzSNxy .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-DlKr3rXh95dzSNxy rect.text{fill:none;stroke-width:0;}#mermaid-svg-DlKr3rXh95dzSNxy .icon-shape,#mermaid-svg-DlKr3rXh95dzSNxy .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DlKr3rXh95dzSNxy .icon-shape p,#mermaid-svg-DlKr3rXh95dzSNxy .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-DlKr3rXh95dzSNxy .icon-shape .label rect,#mermaid-svg-DlKr3rXh95dzSNxy .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DlKr3rXh95dzSNxy .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-DlKr3rXh95dzSNxy .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-DlKr3rXh95dzSNxy :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

Rebase变基

A

B

C

Merge合并

A

B

C

Merge Commit

4.2 精选提交

 git cherry-pick — 精选提交

使用频率:每周 2+ 次 | 难度:⭐⭐⭐

# 精选单个提交

git cherry-pick abc123

# 精选多个提交

git cherry-pick abc123 def456

# 精选提交范围

git cherry-pick abc123..def456

# 精选但不自动提交

git cherry-pick -n abc123

# 继续精选(解决冲突后)

git cherry-pick --continue

# 中止精选

git cherry-pick --abort

# 跳过当前提交

git cherry-pick --skip

典型场景:

# 场景:将 hotfix 分支的修复应用到 main 分支

# 1. 切换到 main 分支

git checkout main

# 2. 精选 hotfix 提交

git cherry-pick hotfix-commit-hash

# 3. 推送

git push

4.3 引用日志

 git reflog — 引用日志

使用频率:每月 2+ 次 | 难度:⭐⭐⭐

# 查看引用日志

git reflog

# 查看指定分支的引用日志

git reflog show main

# 查看最近 10 条

git reflog -10

# 恢复误删的提交

git reflog

# 找到误删前的提交

git reset --hard abc123

# 恢复误删的分支

git reflog

# 找到分支删除前的提交

git checkout -b recovered-branch abc123

救命场景:

# 场景:误执行 git reset --hard HEAD~3

# 1. 查看引用日志

git reflog

# 输出:

# abc1234 HEAD@{0}: reset: moving to HEAD~3

# def5678 HEAD@{1}: commit: 重要提交 ← 这是要恢复的

# 2. 恢复

git reset --hard def5678

4.4 二分查找

 git bisect — 二分查找 Bug

使用频率:每月 1+ 次 | 难度:⭐⭐⭐

# 开始二分查找

git bisect start

# 标记当前提交为坏提交 拼多多帮助力入口

git bisect bad

# 标记已知的好提交

git bisect good v1.0.0

# Git 会自动切换到中间提交

# 测试后标记

git bisect good # 如果是好的

git bisect bad # 如果是坏的

# 找到问题提交后,重置

git bisect reset

# 使用脚本自动二分

git bisect run npm test

工作原理:

查找次数

=

log

⁡

2

(

N

)

\text{查找次数} = \log_2(N)

查找次数=log2​(N)

其中

N

N

N 为提交数量。对于 1000 次提交,最多需要

⌈

log

⁡

2

(

1000

)

⌉

=

10

\lceil \log_2(1000) \rceil = 10

⌈log2​(1000)⌉=10 次查找。

4.5 子模块

 git submodule — 子模块管理

使用频率:每月 1+ 次 | 难度:⭐⭐⭐

# 添加子模块

git submodule add https://github.com/user/lib.git libs/lib

# 初始化子模块

git submodule init

# 更新子模块

git submodule update

# 克隆包含子模块的仓库

git clone --recursive https://github.com/user/repo.git

# 更新所有子模块到最新

git submodule update --remote

# 批量更新子模块

git submodule foreach git pull origin main

# 删除子模块

git submodule deinit libs/lib

git rm libs/lib

rm -rf .git/modules/libs/lib

4.6 工作树

 git worktree — 多工作树

使用频率:每月 1+ 次 | 难度:⭐⭐⭐

# 添加工作树

git worktree add ../hotfix-branch hotfix/bug-123

# 基于新分支添加工作树

git worktree add -b feature/new ../feature-worktree

# 列出所有工作树

git worktree list

# 删除工作树

git worktree remove ../hotfix-branch

# 清理无效的工作树引用

git worktree prune

典型场景:

# 场景:正在开发功能,需要紧急修复 bug

# 1. 创建新的工作树

git worktree add ../hotfix hotfix/bug-123

# 2. 在新目录工作

cd ../hotfix

# ... 修复并提交 ...

# 3. 完成后删除工作树

cd ../main-project

git worktree remove ../hotfix

4.7 高级命令速查表

命令说明频率难度git rebase变基操作⭐⭐⭐git rebase -i交互式变基⭐⭐⭐git cherry-pick精选提交⭐⭐⭐git reflog引用日志⭐⭐⭐git bisect二分查找⭐⭐⭐git submodule子模块⭐⭐⭐git worktree多工作树⭐⭐⭐git filter-branch重写历史❄️⭐⭐⭐⭐git filter-repo重写历史(新)❄️⭐⭐⭐⭐git blame查看文件历史⭐⭐git grep搜索内容⭐⭐git shortlog提交统计⭐⭐git describe生成版本描述⭐⭐git rev-parse解析引用❄️⭐⭐⭐git cat-file查看对象❄️⭐⭐⭐git ls-tree查看树对象❄️⭐⭐⭐git hash-object计算哈希❄️⭐⭐⭐git mktree创建树对象❄️⭐⭐⭐git update-ref更新引用❄️⭐⭐⭐git symbolic-ref符号引用❄️⭐⭐⭐

五、专家篇:20 个高阶命令(⭐⭐⭐⭐)

适用人群:资深开发者、DevOps 工程师 学习目标:掌握历史重写、仓库维护、底层操作

5.1 历史重写

❄️ git filter-branch — 重写历史

使用频率:每年 1-2 次 | 难度:⭐⭐⭐⭐

# 删除所有提交中的敏感文件

git filter-branch --force --index-filter \

'git rm --cached --ignore-unmatch path/to/sensitive-file' \

--prune-empty --tag-name-filter cat -- --all

# 修改作者信息

git filter-branch --env-filter '

OLD_EMAIL="old@email.com"

CORRECT_NAME="New Name"

CORRECT_EMAIL="new@email.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]

then

export GIT_COMMITTER_NAME="$CORRECT_NAME"

export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"

fi

if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]

then

export GIT_AUTHOR_NAME="$CORRECT_NAME"

export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"

fi

' --tag-name-filter cat -- --branches --tags

# 删除空提交

git filter-branch --prune-empty HEAD

注意:filter-branch 已被弃用,推荐使用 git-filter-repo。

❄️ git filter-repo — 重写历史(推荐)

使用频率:每年 1-2 次 | 难度:⭐⭐⭐⭐

# 安装

pip install git-filter-repo

# 删除文件

git filter-repo --path path/to/sensitive-file --invert-paths

# 删除文件夹

git filter-repo --path path/to/folder --invert-paths

# 修改邮箱

git filter-repo --email-callback '

return email if email != b"old@email.com" else b"new@email.com"

'

# 提取子目录为新仓库

git filter-repo --subdirectory-filter path/to/subdir

5.2 仓库维护

❄️ git gc — 垃圾回收

使用频率:每月 1+ 次 | 难度:⭐⭐⭐

# 执行垃圾回收

git gc

# 更积极的压缩

git gc --aggressive

# 自动垃圾回收

git gc --auto

# 清理过期对象

git gc --prune=now

❄️ git fsck — 检查仓库完整性

使用频率:每季度 1+ 次 | 难度:⭐⭐⭐

# 检查仓库

git fsck

# 显示所有对象

git fsck --full

# 检查 dangling 对象

git fsck --dangling

# 检查未引用的对象

git fsck --unreachable

5.3 底层命令

❄️ git cat-file — 查看对象内容

使用频率:调试时 | 难度:⭐⭐⭐⭐

# 查看对象类型

git cat-file -t abc123

# 查看对象内容

git cat-file -p abc123

# 查看对象大小

git cat-file -s abc123

# 批量查询

git cat-file --batch

❄️ git ls-files — 列出文件

使用频率:脚本中 | 难度:⭐⭐⭐

# 列出所有跟踪文件

git ls-files

# 列出已修改文件

git ls-files -m

# 列出已删除文件

git ls-files -d

# 列出未跟踪文件

git ls-files -o

# 列出冲突文件

git ls-files -u

# 显示文件状态

git ls-files -s

5.4 专家命令速查表

命令说明频率难度git filter-branch重写历史(已弃用)❄️⭐⭐⭐⭐git filter-repo重写历史(推荐)❄️⭐⭐⭐⭐git gc垃圾回收❄️⭐⭐⭐git fsck检查仓库❄️⭐⭐⭐git prune清理无效对象❄️⭐⭐⭐git cat-file查看对象❄️⭐⭐⭐⭐git ls-files列出文件❄️⭐⭐⭐git ls-tree查看树对象❄️⭐⭐⭐⭐git hash-object计算哈希❄️⭐⭐⭐⭐git update-index更新索引❄️⭐⭐⭐⭐git read-tree读取树❄️⭐⭐⭐⭐git write-tree写入树❄️⭐⭐⭐⭐git commit-tree提交树❄️⭐⭐⭐⭐git update-ref更新引用❄️⭐⭐⭐⭐git symbolic-ref符号引用❄️⭐⭐⭐⭐git pack-objects打包对象❄️⭐⭐⭐⭐git unpack-objects解包对象❄️⭐⭐⭐⭐git receive-pack接收推送❄️⭐⭐⭐⭐git upload-pack发送推送❄️⭐⭐⭐⭐git http-backendHTTP 协议❄️⭐⭐⭐⭐

六、场景速查索引

6.1 日常开发场景

场景命令组合开始新功能git checkout -b feature/xxx保存进度git add . && git commit -m "WIP"暂存切换git stash && git checkout main恢复暂存git stash pop拉取更新git pull --rebase推送代码git push -u origin feature/xxx查看状态git status -sb查看历史git log --oneline -10

6.2 分支管理场景

场景命令组合创建分支git branch feature/xxx切换分支git switch feature/xxx合并分支git merge --no-ff feature/xxx删除分支git branch -d feature/xxx重命名分支git branch -m old new查看分支git branch -vv追踪远程git branch -u origin/xxx

6.3 紧急修复场景

场景命令组合暂存当前工作git stash save "WIP"切换修复分支git checkout -b hotfix/xxx修复并提交git commit -am "fix: xxx"合并回主分支git checkout main && git merge hotfix/xxx恢复工作git checkout feature && git stash pop

6.4 错误恢复场景

场景命令组合撤销暂存git restore --staged撤销修改git restore撤销提交git reset --soft HEAD~1恢复误删提交git reflog && git reset --hard xxx撤销已推送git revert HEAD恢复误删分支git reflog && git checkout -b xxx

七、Git 工作流程图解

7.1 Git 文件状态流转

#mermaid-svg-z7F2OpZEuVlpudv6{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-z7F2OpZEuVlpudv6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-z7F2OpZEuVlpudv6 .error-icon{fill:#552222;}#mermaid-svg-z7F2OpZEuVlpudv6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-z7F2OpZEuVlpudv6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-z7F2OpZEuVlpudv6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 .marker.cross{stroke:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-z7F2OpZEuVlpudv6 p{margin:0;}#mermaid-svg-z7F2OpZEuVlpudv6 defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-z7F2OpZEuVlpudv6 g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-z7F2OpZEuVlpudv6 g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-z7F2OpZEuVlpudv6 g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-z7F2OpZEuVlpudv6 g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-z7F2OpZEuVlpudv6 .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-z7F2OpZEuVlpudv6 .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-z7F2OpZEuVlpudv6 .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-z7F2OpZEuVlpudv6 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-z7F2OpZEuVlpudv6 .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-z7F2OpZEuVlpudv6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-z7F2OpZEuVlpudv6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-z7F2OpZEuVlpudv6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-z7F2OpZEuVlpudv6 .edgeLabel .label text{fill:#333;}#mermaid-svg-z7F2OpZEuVlpudv6 .label div .edgeLabel{color:#333;}#mermaid-svg-z7F2OpZEuVlpudv6 .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-z7F2OpZEuVlpudv6 .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-z7F2OpZEuVlpudv6 .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-z7F2OpZEuVlpudv6 .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 #statediagram-barbEnd{fill:#333333;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-z7F2OpZEuVlpudv6 .cluster-label,#mermaid-svg-z7F2OpZEuVlpudv6 .nodeLabel{color:#131300;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-z7F2OpZEuVlpudv6 .note-edge{stroke-dasharray:5;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-note text{fill:black;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram-note .nodeLabel{color:black;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagram .edgeLabel{color:red;}#mermaid-svg-z7F2OpZEuVlpudv6 #dependencyStart,#mermaid-svg-z7F2OpZEuVlpudv6 #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-z7F2OpZEuVlpudv6 .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-z7F2OpZEuVlpudv6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

新建文件

git add

git commit

编辑文件

git add

git commit

git rm

git restore

git restore --staged

Untracked

Staged

Unmodified

Modified

7.2 Git 分支工作流

#mermaid-svg-OAiCy3wSzrNBvsGW{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-OAiCy3wSzrNBvsGW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-OAiCy3wSzrNBvsGW .error-icon{fill:#552222;}#mermaid-svg-OAiCy3wSzrNBvsGW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OAiCy3wSzrNBvsGW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OAiCy3wSzrNBvsGW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OAiCy3wSzrNBvsGW .marker.cross{stroke:#333333;}#mermaid-svg-OAiCy3wSzrNBvsGW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OAiCy3wSzrNBvsGW p{margin:0;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-id,#mermaid-svg-OAiCy3wSzrNBvsGW .commit-msg,#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms',verdana,arial,sans-serif;font-family:var(--mermaid-font-family);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label0{fill:#ffffff;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit0{stroke:hsl(240, 100%, 46.2745098039%);fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight0{stroke:hsl(60, 100%, 3.7254901961%);fill:hsl(60, 100%, 3.7254901961%);}#mermaid-svg-OAiCy3wSzrNBvsGW .label0{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow0{stroke:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label1{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit1{stroke:hsl(60, 100%, 43.5294117647%);fill:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight1{stroke:rgb(0, 0, 160.5);fill:rgb(0, 0, 160.5);}#mermaid-svg-OAiCy3wSzrNBvsGW .label1{fill:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow1{stroke:hsl(60, 100%, 43.5294117647%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label2{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit2{stroke:hsl(80, 100%, 46.2745098039%);fill:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight2{stroke:rgb(48.8333333334, 0, 146.5000000001);fill:rgb(48.8333333334, 0, 146.5000000001);}#mermaid-svg-OAiCy3wSzrNBvsGW .label2{fill:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow2{stroke:hsl(80, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label3{fill:#ffffff;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit3{stroke:hsl(210, 100%, 46.2745098039%);fill:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight3{stroke:rgb(146.5000000001, 73.2500000001, 0);fill:rgb(146.5000000001, 73.2500000001, 0);}#mermaid-svg-OAiCy3wSzrNBvsGW .label3{fill:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow3{stroke:hsl(210, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label4{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit4{stroke:hsl(180, 100%, 46.2745098039%);fill:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight4{stroke:rgb(146.5000000001, 0, 0);fill:rgb(146.5000000001, 0, 0);}#mermaid-svg-OAiCy3wSzrNBvsGW .label4{fill:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow4{stroke:hsl(180, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label5{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit5{stroke:hsl(150, 100%, 46.2745098039%);fill:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight5{stroke:rgb(146.5000000001, 0, 73.2500000001);fill:rgb(146.5000000001, 0, 73.2500000001);}#mermaid-svg-OAiCy3wSzrNBvsGW .label5{fill:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow5{stroke:hsl(150, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label6{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit6{stroke:hsl(300, 100%, 46.2745098039%);fill:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight6{stroke:rgb(0, 146.5000000001, 0);fill:rgb(0, 146.5000000001, 0);}#mermaid-svg-OAiCy3wSzrNBvsGW .label6{fill:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow6{stroke:hsl(300, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch-label7{fill:black;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit7{stroke:hsl(0, 100%, 46.2745098039%);fill:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight7{stroke:rgb(0, 146.5000000001, 146.5000000001);fill:rgb(0, 146.5000000001, 146.5000000001);}#mermaid-svg-OAiCy3wSzrNBvsGW .label7{fill:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow7{stroke:hsl(0, 100%, 46.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .branch{stroke-width:1;stroke:#333333;stroke-dasharray:2;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-label{font-size:10px;fill:#000021;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-label-bkg{font-size:10px;fill:#ffffde;opacity:0.5;}#mermaid-svg-OAiCy3wSzrNBvsGW .tag-label{font-size:10px;fill:#131300;}#mermaid-svg-OAiCy3wSzrNBvsGW .tag-label-bkg{fill:#ECECFF;stroke:hsl(240, 60%, 86.2745098039%);}#mermaid-svg-OAiCy3wSzrNBvsGW .tag-hole{fill:#333;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-merge{stroke:#ECECFF;fill:#ECECFF;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-reverse{stroke:#ECECFF;fill:#ECECFF;stroke-width:3;}#mermaid-svg-OAiCy3wSzrNBvsGW .commit-highlight-inner{stroke:#ECECFF;fill:#ECECFF;}#mermaid-svg-OAiCy3wSzrNBvsGW .arrow{stroke-width:8;stroke-linecap:round;fill:none;}#mermaid-svg-OAiCy3wSzrNBvsGW .gitTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-OAiCy3wSzrNBvsGW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

main

develop

feature

hotfix

0-9deec90

1-cb02355

2-a09a78a

3-7a34ed2

4-f77ff47

7-dbc2c17

7.3 Git 命令执行流程

┌─────────────────────────────────────────────────────────────────┐

│ Git 工作流程 │

├─────────────────────────────────────────────────────────────────┤

│ │

│ 工作区 (Working Directory) │

│ ┌─────────────────────────────────────────────────────────┐ │

│ │ git add → 暂存区 │ │

│ │ git restore ← 撤销修改 │ │

│ │ git clean -fd → 删除未跟踪文件 │ │

│ └─────────────────────────────────────────────────────────┘ │

│ ↓ git add │

│ 暂存区 (Staging Area) │

│ ┌─────────────────────────────────────────────────────────┐ │

│ │ git commit → 本地仓库 │ │

│ │ git restore --staged ← 取消暂存 │ │

│ └─────────────────────────────────────────────────────────┘ │

│ ↓ git commit │

│ 本地仓库 (Local Repository) │

│ ┌─────────────────────────────────────────────────────────┐ │

│ │ git push → 远程仓库 │ │

│ │ git reset ← 回退提交 │ │

│ └─────────────────────────────────────────────────────────┘ │

│ ↓ git push │

│ 远程仓库 (Remote Repository) │

│ ┌─────────────────────────────────────────────────────────┐ │

│ │ git fetch → 获取更新 │ │

│ │ git pull → 获取并合并 │ │

│ └─────────────────────────────────────────────────────────┘ │

│ │

└─────────────────────────────────────────────────────────────────┘

八、常见问题与避坑指南

8.1 高频问题解决方案

Q1:如何撤销最后一次提交?

# 保留修改

git reset --soft HEAD~1

# 保留工作区修改,清空暂存区

git reset --mixed HEAD~1

# 彻底删除(危险!)

git reset --hard HEAD~1

Q2:如何修改最后一次提交信息?

# 只修改信息

git commit --amend -m "新的提交信息"

# 追加文件到最后一次提交

git add forgotten-file.js

git commit --amend --no-edit

Q3:如何撤销已推送的提交?

# 方法一:revert(推荐,安全)

git revert HEAD

git push

# 方法二:reset(危险,仅适用于个人分支)

git reset --hard HEAD~1

git push --force-with-lease

Q4:如何解决合并冲突?

# 1. 查看冲突文件

git status

# 2. 手动编辑冲突文件,解决冲突标记

# <<<<<<< HEAD

# 当前分支内容

# =======

# 合并分支内容

# >>>>>>> branch-name

# 3. 标记为已解决

git add resolved-file.js

# 4. 完成合并

git commit

Q5:如何恢复误删的分支?

# 1. 查看引用日志

git reflog

# 2. 找到分支删除前的提交

# 3. 恢复分支

git checkout -b recovered-branch abc123

8.2 避坑指南

坑后果解决方案git push --force覆盖远程历史使用 --force-with-leasegit reset --hard丢失未提交修改先 git stash在 main 直接提交污染主分支使用功能分支忘记拉取就推送推送失败先 git pull --rebase大文件提交仓库膨胀使用 .gitignore敏感信息提交安全风险使用 git-filter-repo 清理

8.3 最佳实践

提交粒度:每个提交只做一件事提交信息:遵循 Conventional Commits 规范分支策略:使用 Git Flow 或 GitHub Flow代码审查:通过 Pull Request 合并代码持续集成:推送前运行测试定期清理:删除已合并的分支

九、总结

9.1 命令掌握路线图

#mermaid-svg-zUh9Vwuaku7hAipG{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-zUh9Vwuaku7hAipG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zUh9Vwuaku7hAipG .error-icon{fill:#552222;}#mermaid-svg-zUh9Vwuaku7hAipG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zUh9Vwuaku7hAipG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zUh9Vwuaku7hAipG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zUh9Vwuaku7hAipG .marker.cross{stroke:#333333;}#mermaid-svg-zUh9Vwuaku7hAipG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zUh9Vwuaku7hAipG p{margin:0;}#mermaid-svg-zUh9Vwuaku7hAipG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zUh9Vwuaku7hAipG .cluster-label text{fill:#333;}#mermaid-svg-zUh9Vwuaku7hAipG .cluster-label span{color:#333;}#mermaid-svg-zUh9Vwuaku7hAipG .cluster-label span p{background-color:transparent;}#mermaid-svg-zUh9Vwuaku7hAipG .label text,#mermaid-svg-zUh9Vwuaku7hAipG span{fill:#333;color:#333;}#mermaid-svg-zUh9Vwuaku7hAipG .node rect,#mermaid-svg-zUh9Vwuaku7hAipG .node circle,#mermaid-svg-zUh9Vwuaku7hAipG .node ellipse,#mermaid-svg-zUh9Vwuaku7hAipG .node polygon,#mermaid-svg-zUh9Vwuaku7hAipG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zUh9Vwuaku7hAipG .rough-node .label text,#mermaid-svg-zUh9Vwuaku7hAipG .node .label text,#mermaid-svg-zUh9Vwuaku7hAipG .image-shape .label,#mermaid-svg-zUh9Vwuaku7hAipG .icon-shape .label{text-anchor:middle;}#mermaid-svg-zUh9Vwuaku7hAipG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-zUh9Vwuaku7hAipG .rough-node .label,#mermaid-svg-zUh9Vwuaku7hAipG .node .label,#mermaid-svg-zUh9Vwuaku7hAipG .image-shape .label,#mermaid-svg-zUh9Vwuaku7hAipG .icon-shape .label{text-align:center;}#mermaid-svg-zUh9Vwuaku7hAipG .node.clickable{cursor:pointer;}#mermaid-svg-zUh9Vwuaku7hAipG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-zUh9Vwuaku7hAipG .arrowheadPath{fill:#333333;}#mermaid-svg-zUh9Vwuaku7hAipG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zUh9Vwuaku7hAipG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zUh9Vwuaku7hAipG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zUh9Vwuaku7hAipG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-zUh9Vwuaku7hAipG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zUh9Vwuaku7hAipG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-zUh9Vwuaku7hAipG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zUh9Vwuaku7hAipG .cluster text{fill:#333;}#mermaid-svg-zUh9Vwuaku7hAipG .cluster span{color:#333;}#mermaid-svg-zUh9Vwuaku7hAipG 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-zUh9Vwuaku7hAipG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-zUh9Vwuaku7hAipG rect.text{fill:none;stroke-width:0;}#mermaid-svg-zUh9Vwuaku7hAipG .icon-shape,#mermaid-svg-zUh9Vwuaku7hAipG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zUh9Vwuaku7hAipG .icon-shape p,#mermaid-svg-zUh9Vwuaku7hAipG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-zUh9Vwuaku7hAipG .icon-shape .label rect,#mermaid-svg-zUh9Vwuaku7hAipG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zUh9Vwuaku7hAipG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-zUh9Vwuaku7hAipG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-zUh9Vwuaku7hAipG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

入门级

进阶级

高级

专家级

init/clone/add/commit/push/pull

branch/merge/rebase/stash

cherry-pick/bisect/reflog

filter-repo/底层命令

9.2 核心命令速记

类别核心命令记忆口诀初始化init, cloneinit 创建,clone 克隆提交add, commit, pushadd 暂存,commit 提交,push 推送拉取fetch, pullfetch 只下载,pull 下载并合并分支branch, checkout, switchbranch 管理,checkout/switch 切换合并merge, rebasemerge 合并,rebase 变基撤销reset, revert, restorereset 回退,revert 安全撤销暂存stashstash 暂存工作区标签tagtag 打标签

9.3 学习建议

入门阶段:掌握 20 个基础命令,能完成日常开发进阶阶段:理解分支模型,掌握合并和变基高级阶段:学会历史重写和问题排查专家阶段:理解 Git 底层原理,能处理复杂问题

 收藏提示:本文涵盖 100 个 Git 命令,按难度和场景分类,建议收藏以便随时查阅。

 相关阅读:

Pro Git 官方文档Git Flow 工作流Conventional Commits 规范

参考资料:

Pro Git: https://git-scm.com/book/zh/v2Git 官方文档: https://git-scm.com/docsGitHub Git Cheat Sheet: https://training.github.com/downloads/zh/github-git-cheat-sheet/Atlassian Git Tutorials: https://www.atlassian.com/git

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧