Skip to content

附录 A:Git 命令速查表

配置

bash
git config --global user.name "Your Name"       # 设置全局用户名
git config --global user.email "you@email.com"  # 设置全局邮箱
git config --global core.editor vim             # 设置默认编辑器
git config --global init.defaultBranch main     # 设置默认分支名
git config --list                               # 查看所有配置
git config --global --edit                      # 编辑全局配置文件

仓库操作

bash
git init                    # 初始化新仓库
git init --bare             # 初始化裸仓库
git clone <url>             # 克隆远程仓库
git clone <url> <dir>       # 克隆到指定目录
git clone --depth 1 <url>   # 浅克隆(只取最近 1 次提交)
git clone --single-branch --branch main <url>  # 只克隆单个分支

文件状态

bash
git status                  # 查看工作区状态
git status -s               # 简洁格式
git add <file>              # 添加指定文件到暂存区
git add .                   # 添加所有变更到暂存区
git add -p                  # 交互式添加(按块选择)
git rm <file>               # 删除文件(同时从工作区删除)
git rm --cached <file>      # 从暂存区删除(保留工作区文件)
git mv <old> <new>          # 移动/重命名文件

提交

bash
git commit -m "message"     # 提交暂存区内容
git commit -am "message"    # 自动暂存已跟踪文件并提交
git commit --amend          # 修改最后一次提交
git commit --amend --no-edit    # 修改最后一次提交(不改消息)
git commit --allow-empty -m "msg"  # 允许空提交

查看历史

bash
git log                     # 查看提交历史
git log --oneline           # 单行格式
git log --oneline --graph --all  # 图形化显示所有分支
git log -n 10               # 最近 10 条
git log --since="2026-01-01"    # 指定日期之后
git log --author="Name"     # 指定作者
git log --grep="keyword"    # 提交消息搜索
git log -S "code"           # 搜索代码变更(pickaxe)
git log --follow <file>     # 跟踪文件重命名历史
git log -p                  # 显示每次提交的差异
git shortlog -sn            # 按提交数统计作者
git show <commit>           # 查看特定提交内容

差异比较

bash
git diff                    # 工作区 vs 暂存区
git diff --cached           # 暂存区 vs HEAD(即将提交的内容)
git diff HEAD               # 工作区 vs HEAD
git diff main..feature      # 两个分支对比
git diff main...feature     # feature 相对 main 的变更
git diff --stat             # 只显示统计信息
git diff <commit1> <commit2>    # 两个提交对比

分支操作

bash
git branch                  # 列出本地分支
git branch -a               # 列出所有分支(含远程)
git branch -v               # 显示各分支最后一次提交
git branch <name>           # 创建分支
git branch <name> <commit>  # 从特定提交创建分支
git branch -d <name>        # 删除已合并的分支
git branch -D <name>        # 强制删除分支
git branch -m <old> <new>   # 重命名分支
git switch <branch>         # 切换分支(新语法)
git switch -c <branch>      # 创建并切换分支(新语法)
git checkout <branch>       # 切换分支(旧语法)
git checkout -b <branch>    # 创建并切换分支(旧语法)
git checkout -              # 切换到上一个分支

合并与变基

bash
git merge <branch>          # 合并分支
git merge --no-ff <branch>  # 禁用快进合并
git merge --squash <branch> # squash 合并(合并为一次提交)
git merge --abort           # 中止合并
git rebase <branch>         # 变基到指定分支
git rebase -i HEAD~3        # 交互式变基(最近 3 次提交)
git rebase --onto <newbase> <upstream> <branch>  # 精确变基
git rebase --abort          # 中止变基
git rebase --continue       # 解决冲突后继续变基
git cherry-pick <commit>    # 选择性合并单个提交
git cherry-pick A..B        # 选择性合并多个提交(不含 A)
git cherry-pick A^..B       # 选择性合并多个提交(含 A)

撤销操作

bash
git restore <file>          # 丢弃工作区修改(新语法)
git restore --staged <file> # 取消暂存(新语法)
git checkout -- <file>      # 丢弃工作区修改(旧语法)
git reset HEAD <file>       # 取消暂存(旧语法)
git reset --soft HEAD~1     # 撤销最后一次提交(保留暂存区)
git reset --mixed HEAD~1    # 撤销最后一次提交(保留工作区)
git reset --hard HEAD~1     # 撤销最后一次提交(不保留)
git revert <commit>         # 反转提交(创建新提交)
git revert -m 1 <merge-commit>  # 反转合并提交
git clean -fd               # 删除未跟踪文件和目录
git clean -n                # 预览 clean 操作

储藏(Stash)

bash
git stash                   # 储藏当前修改
git stash push -m "message" # 储藏并添加描述
git stash list              # 查看储藏列表
git stash pop               # 应用并删除最新储藏
git stash apply stash@{0}   # 应用指定储藏(不删除)
git stash drop stash@{0}    # 删除指定储藏
git stash clear             # 清空所有储藏
git stash branch <branch>   # 从储藏创建分支
git stash -p                # 交互式储藏
git stash --include-untracked  # 包含未跟踪文件

标签

bash
git tag                     # 列出标签
git tag v1.0.0              # 创建轻量标签
git tag -a v1.0.0 -m "msg"  # 创建附注标签
git tag -a v1.0.0 <commit>  # 为历史提交打标签
git tag -l "v1.*"           # 通配符过滤标签
git show v1.0.0             # 查看标签信息
git push origin v1.0.0      # 推送指定标签
git push origin --tags      # 推送所有标签
git push origin :v1.0.0     # 删除远程标签
git tag -d v1.0.0           # 删除本地标签

远程操作

bash
git remote -v               # 查看远程仓库
git remote add origin <url> # 添加远程仓库
git remote rename origin upstream  # 重命名远程
git remote remove <name>    # 删除远程
git remote set-url origin <url>    # 修改远程 URL
git remote show origin      # 显示远程详细信息
git fetch                   # 获取所有远程更新
git fetch origin            # 获取指定远程更新
git pull                    # 拉取并合并
git pull --rebase           # 拉取并变基
git push                    # 推送到远程
git push -u origin main     # 推送并设置上游分支
git push --force-with-lease # 安全强制推送
git push origin --delete <branch>  # 删除远程分支
git push origin --tags      # 推送所有标签
git branch -r               # 查看远程追踪分支
git branch --set-upstream-to=origin/main  # 设置上游分支
git fetch --prune           # 获取并清理过期远程追踪分支

子模块

bash
git submodule add <url> <path>  # 添加子模块
git submodule init              # 初始化子模块
git submodule update            # 更新子模块
git submodule update --init --recursive  # 初始化并更新所有子模块
git clone --recurse-submodules <url>     # 克隆含子模块的仓库

工作树

bash
git worktree add <path> <branch>  # 添加工作树
git worktree list              # 列出所有工作树
git worktree remove <path>     # 删除工作树

调试与诊断

bash
git bisect start            # 开始二分查找
git bisect good <commit>    # 标记正常提交
git bisect bad <commit>     # 标记问题提交
git bisect run <script>     # 自动化二分查找
git bisect reset            # 结束二分查找
git blame <file>            # 查看文件每行的最后修改
git blame -L 10,20 <file>   # 查看指定行范围
git log --follow <file>     # 追踪文件重命名历史
git reflog                  # 查看 HEAD 移动历史
git fsck                    # 检查仓库完整性

内部命令(Plumbing)

bash
git cat-file -t <hash>      # 查看对象类型
git cat-file -p <hash>      # 查看对象内容
git hash-object <file>      # 计算文件的 Git 对象哈希
git ls-tree HEAD            # 列出树对象内容
git ls-files --stage        # 查看索引(暂存区)内容
git count-objects -v        # 统计对象数量和大小
git gc                      # 垃圾回收和打包
git prune                   # 删除不可达对象

常用别名参考

bash
# 在 ~/.gitconfig 中添加
[alias]
    st = status
    co = checkout
    br = branch
    sw = switch
    cm = commit
    lg = log --oneline --graph --all --decorate
    aa = add .
    unstage = restore --staged
    last = log -1 HEAD
    undo = reset --soft HEAD~1
    aliases = config --get-regexp alias