在这里插入图片描述

🌈 say-fall:个人主页
🚀 专栏:《手把手教你学会C++》 | 《系统深入Linux操作系统》 | 《数据结构与算法》 | 《代码练习》
💪 格言:做好你自己,才能吸引更多人,与他们共赢,这才是最好的成长方式。

📝 前言

本篇是 Git 系列的第 6 篇(命令速查手册),也是整个系列的收官工具

前面 5 篇博客我们把 Git 从入门到企业级讲透了——本地仓库、分支管理、远程操作、多人协作、企业模型。但5 篇博客里用了 100+ 个命令,新人最常问的问题就是:

  • “那个命令的参数我忘了”
  • “这个场景该用哪个命令”
  • “命令这么多有没有一张表能查”

本篇就是来"治"这个的。

我把前面 5 篇里出现过的所有命令按功能分类整理成手册,每个命令配一句话功能说明 + 最常用参数 + 典型示例建议收藏——以后写代码卡壳了直接翻。

🎯 本节目标:成为你手边的 Git 命令字典。本篇不深入讲原理(原理在第 1-5 篇),只讲怎么用

通过本文,你将掌握:

技能 应用场景
12 大类 Git 命令的速查 写代码时随时翻
80+ 核心命令的标准用法 覆盖 95% 日常场景
效率别名的配置 让 Git 更好用
常用操作的标准流程 团队统一规范

📌 前置知识: 阅读过前 5 篇任意一篇。本篇是工具书,可以直接当字典用


一、🔧 配置类命令

命令 功能 常用参数 示例
git config 配置 Git --global(全局)
--local(当前仓库)
--list(查看)
git config --global user.name "Your Name"
git config --global user.name 设置用户名 - git config --global user.name "say_fall"
git config --global user.email 设置邮箱 - git config --global user.email "your@email.com"
git config --global init.defaultBranch 设置默认分支名 - git config --global init.defaultBranch main
git config --global color.ui 开关颜色 auto / false / true git config --global color.ui auto
git config --global pull.rebase pull 行为 true / false git config --global pull.rebase true
git config --global rerere.enabled 开启冲突记忆 true / false git config --global rerere.enabled true
git config --global core.editor 设置默认编辑器 - git config --global core.editor vim
git config --list 查看所有配置 - git config --list
git config --get <key> 查看某项配置 - git config --get user.name
git config --unset <key> 取消某项配置 - git config --unset user.email

💡 配置分 3 层--system(系统级)→ --global(用户级)→ --local(仓库级)。后一层覆盖前一层

常用一次性配置(新人必跑)

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global color.ui auto
git config --global pull.rebase true
git config --global core.editor vim

二、📦 仓库初始化与克隆

命令 功能 常用参数 示例
git init 初始化本地仓库 - git init
git init <dir> 在指定目录初始化 - git init my-project
git init --bare 初始化裸仓库(远程用) - git init --bare shared-repo.git
git clone <url> 克隆远程仓库 - git clone git@github.com:user/repo.git
git clone <url> <dir> 克隆到指定目录 - git clone <url> my-folder
git clone -b <branch> <url> 克隆指定分支 - git clone -b dev <url>
git clone --depth 1 <url> 浅克隆(只拉最近 1 次) - git clone --depth 1 <url>
git clone --bare <url> 克隆为裸仓库 - git clone --bare <url>

💡 init vs cloneinit 是"从零创建"(本地已有代码想用 Git 管理),clone 是"复制远程仓库"(获取别人的代码)。


三、📝 文件操作

命令 功能 常用参数 示例
git status 查看工作区状态 -s(简洁)
--short
git status
git add <file> 添加文件到暂存区 - git add app.py
git add . 添加所有改动 - git add .
git add -A 添加所有改动(包括删除) - git add -A
git add -p 交互式添加(按块) - git add -p
git add -u 只添加已追踪文件的修改 - git add -u
git diff 查看工作区与暂存区差异 - git diff
git diff --staged 查看暂存区与最新 commit 差异 - git diff --staged
git diff <branch> 查看当前分支与目标分支差异 - git diff master
git rm <file> 删除文件并暂存 - git rm app.py
git mv <old> <new> 重命名文件 - git mv old.py new.py
git restore <file> 恢复文件到暂存区状态 - git restore app.py
git restore --staged <file> 取消暂存 - git restore --staged app.py
git clean -fd 删除未追踪文件/目录 -f(强制)
-d(目录)
git clean -fd
gitignore 忽略文件(手动编辑 .gitignore - 见下方说明

.gitignore 常用模板

# Python
__pycache__/
*.pyc
.venv/
.env

# Java
*.class
*.jar
target/

# IDE
.idea/
.vscode/
*.iml

# OS
.DS_Store
Thumbs.db

# Node
node_modules/
npm-debug.log

# 自定义
logs/
*.log

四、💾 提交与回退

命令 功能 常用参数 示例
git commit -m "msg" 提交暂存区 -m(消息) git commit -m "feat: add login"
git commit -am "msg" add + commit 一起 -a(自动 add 已追踪) git commit -am "fix bug"
git commit --amend 修改最后一次 commit - git commit --amend -m "new msg"
git commit --amend --no-edit 修改最后一次 commit(不改消息) - 同上
git commit -v 提交时显示 diff - git commit -v
git commit --allow-empty 允许空提交 - git commit --allow-empty -m "trigger CI"
git reset <file> 取消暂存指定文件 - git reset app.py
git reset --soft HEAD~1 软重置(保留改动到暂存区) --soft git reset --soft HEAD~1
git reset --mixed HEAD~1 混合重置(默认,保留到工作区) --mixed git reset HEAD~1
git reset --hard HEAD~1 硬重置(丢弃所有改动) --hard git reset --hard HEAD~1
git reset --hard <commit> 硬重置到指定 commit - git reset --hard abc1234
git revert <commit> 反转指定 commit - git revert abc1234
git revert --no-edit <commit> 反转(不弹编辑器) - git revert --no-edit HEAD
git reflog 查看所有操作记录 - git reflog

⚠️ reset vs revert 区别reset 改写历史(危险),revert 创建反向 commit(安全)。已 push 的 commit 用 revert。

reset 三种模式速记

--soft   ← HEAD 位置改变,工作区和暂存区不动
--mixed  ← HEAD 和暂存区改变,工作区不动(默认)
--hard   ← 全部改变(最危险)

五、🔍 历史查看

命令 功能 常用参数 示例
git log 查看提交历史 - git log
git log --oneline 简洁模式(每行 1 个 commit) - git log --oneline
git log -n 5 只看最近 5 个 - git log -n 5
git log --graph 图形化显示分支 - git log --graph --oneline
git log --all 显示所有分支 - git log --all
git log --author="name" 按作者筛选 - git log --author="say_fall"
git log --since="2 days ago" 按时间筛选 - git log --since="2 days ago"
git log -p 显示完整 diff - git log -p
git log --stat 显示改动统计 - git log --stat
git log -- <file> 查看某文件的修改历史 - git log -- app.py
git log --grep="fix" 按 commit message 搜索 - git log --grep="fix"
git log -S "function" 按代码内容搜索 - git log -S "login"
git show <commit> 显示某次提交的详情 - git show abc1234
git show <commit> --stat 显示某次提交的改动统计 - git show abc1234 --stat
git blame <file> 显示文件每行是谁改的 - git blame app.py
git shortlog 按作者聚合统计 - git shortlog -sn

推荐的 log 别名(后面有详细配置):

git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all

六、🌿 分支操作

命令 功能 常用参数 示例
git branch 列出本地分支 - git branch
git branch -a 列出所有分支(含远程) - git branch -a
git branch -r 只列远程分支 - git branch -r
git branch -vv 显示分支跟踪信息 - git branch -vv
git branch <name> 创建分支 - git branch dev
git branch -d <name> 删除已合并分支 -d git branch -d dev
git branch -D <name> 强制删除分支 -D git branch -D dev
git branch --merged 列出已合并分支 - git branch --merged
git branch --no-merged 列出未合并分支 - git branch --no-merged
git branch -m <new> 重命名当前分支 - git branch -m main
git branch -u <remote>/<branch> 设置上游分支 - git branch -u origin/dev
git checkout <name> 切换分支(旧语法) - git checkout dev
git checkout -b <name> 创建并切换(旧语法) - git checkout -b dev
git switch <name> 切换分支(新语法) - git switch dev
git switch -c <name> 创建并切换(新语法) -c git switch -c dev
git switch -c <name> origin/<name> 创建并跟踪远程分支 - git switch -c dev origin/dev
git merge <branch> 合并分支 - git merge dev
git merge --no-ff <branch> 非快进合并 --no-ff git merge --no-ff dev
git merge --squash <branch> 压缩合并 --squash git merge --squash dev
git merge --abort 取消合并 - git merge --abort
git rebase <branch> 变基到目标分支 - git rebase master
git rebase -i HEAD~3 交互式 rebase -i git rebase -i HEAD~3
git rebase --continue 冲突解决后继续 - git rebase --continue
git rebase --abort 取消 rebase - git rebase --abort
git rebase --skip 跳过当前 commit - git rebase --skip

merge vs rebase 速记

维度 merge rebase
历史 树状(merge commit) 线性
适用 共享分支 本地 feature
危险度 高(改写历史)

七、📦 暂存操作

命令 功能 常用参数 示例
git stash 暂存当前改动 - git stash
git stash push -m "msg" 暂存并加消息 -m git stash push -m "login 写到一半"
git stash -u 包括未追踪文件 -u git stash -u
git stash list 查看所有 stash - git stash list
git stash pop 恢复并删除最新 stash - git stash pop
git stash apply stash@{0} 恢复但不删除 - git stash apply stash@{0}
git stash drop stash@{0} 删除指定 stash - git stash drop stash@{0}
git stash show stash@{0} 看 stash 的改动 - git stash show stash@{0}
git stash show -p stash@{0} 看 stash 的完整 diff -p git stash show -p stash@{0}
git stash clear 清空所有 stash - git stash clear
git stash branch <name> 从 stash 创建分支 - git stash branch dev stash@{0}

💡 stash 是栈结构(LIFO:后进先出)。stash@{0} 是最新的,stash@{1} 是上一个。


八、🌐 远程操作

命令 功能 常用参数 示例
git remote 列出远程仓库别名 - git remote
git remote -v 列出远程仓库 URL - git remote -v
git remote add <name> <url> 添加远程仓库 - git remote add origin git@github.com:user/repo.git
git remote remove <name> 删除远程仓库 - git remote remove origin
git remote rename <old> <new> 重命名远程 - git remote rename origin upstream
git remote set-url <name> <url> 修改远程 URL - git remote set-url origin <new-url>
git remote show <name> 查看远程详情 - git remote show origin
git remote prune <name> 清理已删除的远程跟踪分支 - git remote prune origin
git fetch <remote> 抓取远程更新(不合并) - git fetch origin
git fetch --all 抓取所有远程 - git fetch --all
git fetch -p 抓取并清理已删除远程分支 -p git fetch -p
git pull 抓取 + 合并(默认) - git pull
git pull <remote> <branch> 拉取指定分支 - git pull origin dev
git pull --rebase 拉取并 rebase - git pull --rebase
git push 推送到上游分支 - git push
git push <remote> <branch> 推送到指定分支 - git push origin dev
git push -u <remote> <branch> 推送并设置上游 -u git push -u origin master
git push --all 推送所有分支 - git push --all
git push --tags 推送所有 tag - git push --tags
git push --delete <branch> 删除远程分支 - git push origin --delete dev
git push --force 强制推送(危险) --force git push --force origin master
git push --force-with-lease 安全强制推送 --force-with-lease git push --force-with-lease origin master

fetch vs pull 速记

命令 行为 何时用
fetch 只下载,不合并 想"先看看再合并"
pull fetch + merge 信任远程,直接同步

九、🏷️ 标签

命令 功能 常用参数 示例
git tag 列出所有 tag - git tag
git tag -l "v1.*" 模糊搜索 tag -l git tag -l "v1.*"
git tag v1.0.0 创建轻量 tag - git tag v1.0.0
git tag -a v1.0.0 -m "msg" 创建附注 tag(推荐) -a(annotated)
-m
git tag -a v1.0.0 -m "发布 1.0.0"
git tag -a v1.0.0 <commit> 给历史 commit 打 tag - git tag -a v1.0.0 abc1234
git show v1.0.0 查看 tag 详情 - git show v1.0.0
git push origin v1.0.0 推送单个 tag - git push origin v1.0.0
git push origin --tags 推送所有 tag --tags git push origin --tags
git tag -d v1.0.0 删除本地 tag -d git tag -d v1.0.0
git push origin --delete v1.0.0 删除远程 tag - git push origin --delete v1.0.0
git checkout v1.0.0 切到指定 tag(detached HEAD) - git checkout v1.0.0

💡 轻量 tag 只是个名字,附注 tag 包含打 tag 者的信息、日期、说明。发版推荐用附注 tag


十、🏢 企业级工具

10.1 git-flow(Git Flow 工具)

命令 功能 示例
git flow init 初始化 Git Flow git flow init
git flow feature start <name> 开始 feature git flow feature start login
git flow feature finish <name> 完成 feature git flow feature finish login
git flow feature publish <name> 发布 feature 到远程 git flow feature publish login
git flow feature pull <remote> <name> 拉取远程 feature git flow feature pull origin login
git flow release start <v> 开始 release git flow release start v1.0.0
git flow release finish <v> 完成 release git flow release finish v1.0.0
git flow hotfix start <name> 开始 hotfix git flow hotfix start pay-bug
git flow hotfix finish <name> 完成 hotfix git flow hotfix finish pay-bug

10.2 git worktree(多工作目录)

命令 功能 示例
git worktree add <path> <branch> 添加工作目录 git worktree add ../feature-branch feature/x
git worktree list 列出所有工作目录 git worktree list
git worktree remove <path> 删除工作目录 git worktree remove ../feature-branch
git worktree prune 清理无效工作目录 git worktree prune

10.3 git submodule(子模块)

命令 功能 示例
git submodule add <url> <path> 添加子模块 git submodule add <url> libs/foo
git submodule init 初始化子模块 git submodule init
git submodule update 更新子模块 git submodule update --init --recursive
git submodule status 查看子模块状态 git submodule status

10.4 git archive(打包)

命令 功能 示例
git archive --format=zip HEAD > out.zip 打包当前版本 git archive --format=zip HEAD > release.zip
git archive --format=tar v1.0.0 打包指定 tag git archive --format=tar v1.0.0 > v1.0.0.tar

10.5 git bisect(二分查找 bug)

命令 功能 示例
git bisect start 开始二分查找 git bisect start
git bisect bad 标记当前为坏版本 git bisect bad
git bisect good <commit> 标记某 commit 为好版本 git bisect good v1.0.0
git bisect reset 退出二分查找 git bisect reset

💡 git bisect 是定位"哪个 commit 引入 bug"的神器——用二分查找,比逐个 commit 试快 100 倍。

10.6 git grep(代码搜索)

命令 功能 示例
git grep "pattern" 在工作区搜索 git grep "TODO"
git grep -n "pattern" 显示行号 git grep -n "TODO"
git grep "pattern" HEAD~5 在指定 commit 搜索 git grep "function_name" v1.0.0

十一、⚡ 效率别名(强烈推荐)

11.1 一键配置

把下面这段直接复制到终端运行,配置就生效了:

# log 美化
git config --global alias.lg "log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all"

# 简洁 log
git config --global alias.l "log --oneline --graph -10"

# 全部 add + 状态
git config --global alias.st "status"

# 简洁 status
git config --global alias.ss "status -s"

# 全部 add
git config --global alias.a "add -A"

# 带信息的 commit
git config --global alias.cm "commit -m"

# 撤销最后一次 commit(保留改动)
git config --global alias.undo "reset --soft HEAD~1"

# 查看所有分支
git config --global alias.br "branch -a"

# 看远程
git config --global alias.rv "remote -v"

# diff 工具
git config --global alias.d "diff"
git config --global alias.dc "diff --cached"

11.2 别名效果

配好之后:

# 等价 git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all
git lg

# 简洁 status
git ss

# 看所有分支
git br

# 撤销最后一次 commit(保留改动)
git undo

💡 配置一次,终身受用。把上面那段命令保存到笔记里,新机器配置时直接复制粘贴。

11.3 更多推荐别名

# 推送到当前分支的上游
git config --global alias.ps "push"

# 拉取并 rebase
git config --global alias.pl "pull --rebase"

# 看所有 stash
git config --global alias.sl "stash list"

# 看 stashes 详情
git config --global alias.ss2 "stash show -p"

# 找到删除的文件
git config --global alias.df "diff --diff-filter=D --summary"

# 今天的 commit
git config --global alias.today "log --since=midnight --author=\$(git config user.name) --oneline"

# 我上周的 commit
git config --global alias.week "log --since='1 week ago' --oneline"

十二、📋 常用操作标准流程

12.1 第一次用 Git(新人必跑)

# 1. 配置身份
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# 2. 配置默认分支
git config --global init.defaultBranch main

# 3. 配置编辑器
git config --global core.editor vim

# 4. 开启颜色
git config --global color.ui auto

# 5. (可选)配 SSH Key
ssh-keygen -t ed25519 -C "your@email.com"
cat ~/.ssh/id_ed25519.pub
# 复制到 Gitee/GitHub

12.2 新项目开发流程

# 1. clone 远程仓库
git clone git@github.com:user/repo.git
cd repo

# 2. 创建 feature 分支
git switch -c feature/awesome-feature

# 3. 开发 + 提交
echo "code" > app.py
git add .
git commit -m "feat: add awesome feature"

# 4. 推送到远程
git push -u origin feature/awesome-feature

# 5. 在平台创建 PR
# 6. 团队 review + 合并

12.3 同步远程最新代码

# 标准流程(推荐)
git fetch origin
git rebase origin/master  # 或 git merge origin/master

# 或一条命令(已配置 pull.rebase = true)
git pull --rebase

12.4 解决冲突

# 1. 冲突时先看哪些文件冲突
git status

# 2. 编辑冲突文件(删 <<<<<<< ======= >>>>>>>)

# 3. 标记解决
git add <file>

# 4. 完成合并/rebase
git commit                      # merge 时
git rebase --continue           # rebase 时

# 5. 验证
git status  # 不应该有 Unmerged paths

12.5 紧急修复 Bug

# 1. 创建 hotfix 分支
git switch -c hotfix/critical-bug

# 2. 修复 + 提交
echo "fix" > bug.py
git add .
git commit -m "hotfix: critical bug"

# 3. 推送
git push -u origin hotfix/critical-bug

# 4. 创建 PR + 快速 review
# 5. 合并到 master + 打 tag
git switch master
git merge --no-ff hotfix/critical-bug
git tag v1.0.1
git push origin v1.0.1

12.6 发版

# 1. 切到 master
git switch master

# 2. 拉取最新
git pull --rebase

# 3. 打 tag
git tag -a v1.0.0 -m "Release 1.0.0"

# 4. 推送 tag
git push origin v1.0.0

12.7 撤销操作

# 撤销工作区修改
git restore <file>

# 取消暂存
git restore --staged <file>

# 修改最后一次 commit 信息
git commit --amend

# 撤销最后一次 commit(保留改动)
git reset --soft HEAD~1

# 撤销最后一次 commit(丢弃改动)
git reset --hard HEAD~1

# 撤销已 push 的 commit
git revert <commit>

12.8 找回"消失"的代码

# 1. 查所有操作记录
git reflog

# 2. 找回去
git reset --hard <commit-id>

十三、📐 命令数与场景对照表

“这么多命令,平时到底用多少?”

场景 必用命令(5 个以内) 频率
日常开发 statusaddcommitpushpull ⭐⭐⭐⭐⭐
查看历史 logdiffshow ⭐⭐⭐⭐
分支开发 branchswitchmerge ⭐⭐⭐⭐
冲突解决 statusaddcommit ⭐⭐⭐
团队协作 remotefetchpull --rebase ⭐⭐⭐⭐
发版 tagpush --tags ⭐⭐
紧急修复 switch -c hotfix/mergetag ⭐⭐
事故回滚 revertresetreflog

💡 日常工作 90% 只用 16 个核心命令。其他的是"进阶武器",用到再查本手册。


十四、❓ 几个常见疑问

1️⃣ 这么多命令怎么记?

答:分 3 层。

层级 数量 记忆方法
核心 16 个 16 每天用,自然记住
常用 30 个 30 反复查本手册
不常用 50 个 50+ 知道有,用到再查

核心 16 个(必背):

config, init, clone, status, add, commit,
log, diff, branch, switch, merge,
remote, fetch, pull, push, reflog

2️⃣ 命令太长了不想打怎么办?

答:配别名。

参考第 11 节。配一次终身受用。

3️⃣ 哪个命令最容易出错?

答:reset --hardpush --forceclean -fd

3 个都是不可逆的操作。执行前三思

  • 真的不需要了吗?
  • 有没有备份?
  • reflog 能找回来吗?

4️⃣ 怎么查命令的详细帮助?

答:用 git helpman git

# 查某个命令的帮助
git help commit
# 或
git commit --help
# 或
man git-commit

# 查所有命令列表
git help -a

# 查常用工作流
git help workflows

5️⃣ 命令记不住有没有图形化工具?

答:有,但不推荐。

工具 平台 说明
GitHub Desktop Windows/Mac 简单直观
Sourcetree Windows/Mac 功能强大
GitKraken 全平台 颜值高
VS Code - 内置 Git 集成
IntelliJ IDEA - JetBrains 全家桶

:工具能"上手"但不能"深入"。想真正懂 Git,还是得用命令行


🎬 写在最后

到这里,《Git 命令速查手册》就写完了 🎉

本篇是 Git 系列的第 6 篇**,也是工具书。本系列完整 6 篇全部完结。**

6 篇博客的完整路径

主题 适合什么时候看
第 1 篇 本地仓库完全指南 入门时看
第 2 篇 分支管理深度实战 学完第 1 篇
第 3 篇 远程操作全解 学完第 1-2 篇
第 4 篇 多人协作实战 学完第 1-3 篇
第 5 篇 企业级开发模型 学完第 1-4 篇
第 6 篇 命令速查手册 写代码时随时翻

推荐使用方式

💡 前 5 篇按顺序读,最后 1 篇收藏备查。本手册能让你 90% 的 Git 操作不用 Google。

最后一句话

Git 是程序员最值得"投资"学习的工具——它用 16 个核心命令,能撑起一个 100 人公司的协作流程。掌握 Git 是每个程序员的必修课

Happy coding! 🚀


✅ 本节完… Git 系列 6 篇全部完结 🎉

📝 作者:say-fall | 编辑:say-fall | 🌟 原创不易,如果对你有帮助,记得 👍 点赞 + ⭐ 收藏 哦!

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐