持续更新中 · Digital Garden
2026 · 07 · 01 · 第 18
前端 · 后端 · AI · 工程  —  著述与维护 Wyman · Est. MMXXIV · 北京
首页/档案/Git 命令速查
16·Git / GIT

Git 命令速查

按配置、仓库、分支、提交、远端、同步、差异、撤销、Stash、Tag 和 Submodule 分类整理 Git 常用命令。

占位符说明

占位符含义
<branch>分支名,例如 main
<remote>远端名,例如 origin
<commit>提交哈希
<tag>标签名,例如 v1.0.0
<file>文件路径
<path>目录路径
<url>远程仓库地址

基础命令

命令说明
git --version查看 Git 版本
git help查看 Git 帮助
git help <command>查看子命令帮助
git status查看工作区状态
git status -sb简洁查看工作区状态
git init初始化仓库
git init <path>在指定目录初始化仓库
git clone <url>克隆远程仓库
git clone <url> <path>克隆到指定目录
git clone --depth 1 <url>浅克隆仓库

配置命令

命令说明
git config --list查看当前配置
git config --global --list查看全局配置
git config user.name查看当前仓库用户名
git config user.email查看当前仓库邮箱
git config --global user.name "Name"设置全局用户名
git config --global user.email "mail@example.com"设置全局邮箱
git config user.name "Name"设置当前仓库用户名
git config user.email "mail@example.com"设置当前仓库邮箱
git config --global init.defaultBranch main设置默认初始分支
git config --global core.editor "code --wait"设置默认编辑器
git config --global pull.rebase false设置 pull 使用 merge
git config --global pull.rebase true设置 pull 使用 rebase
git config --global alias.st status添加命令别名
git config --global --unset <key>删除全局配置项

暂存和提交

命令说明
git add <file>暂存指定文件
git add <path>暂存指定目录
git add .暂存当前目录变化
git add -A暂存所有变化
git add -p交互式暂存
git commit -m "message"创建提交
git commit打开编辑器创建提交
git commit -am "message"暂存已跟踪文件并提交
git commit --amend修改上一次提交
git commit --amend -m "message"修改上一次提交信息
git commit --allow-empty -m "message"创建空提交
git restore --staged <file>取消暂存文件
git reset HEAD <file>取消暂存文件

分支命令

命令说明
git branch查看本地分支
git branch -a查看所有分支
git branch -r查看远端分支
git branch <branch>创建分支
git checkout <branch>切换分支
git switch <branch>切换分支
git checkout -b <branch>创建并切换分支
git switch -c <branch>创建并切换分支
git branch -m <new>重命名当前分支
git branch -m <old> <new>重命名指定分支
git branch -d <branch>删除已合并分支
git branch -D <branch>强制删除分支
git merge <branch>合并分支
git merge --no-ff <branch>使用合并提交合并分支
git merge --abort中止合并
git rebase <branch>变基到指定分支
git rebase --continue继续变基
git rebase --abort中止变基
git cherry-pick <commit>应用指定提交
git cherry-pick --continue继续 cherry-pick
git cherry-pick --abort中止 cherry-pick

远端命令

命令说明
git remote查看远端名
git remote -v查看远端地址
git remote add <remote> <url>添加远端
git remote set-url <remote> <url>修改远端地址
git remote rename <old> <new>重命名远端
git remote remove <remote>删除远端
git remote show <remote>查看远端详情
git fetch获取默认远端更新
git fetch <remote>获取指定远端更新
git fetch --all获取所有远端更新
git fetch --prune获取更新并清理失效引用
git pull拉取并合并当前分支
git pull --rebase拉取并变基当前分支
git push推送当前分支
git push <remote> <branch>推送指定分支
git push -u <remote> <branch>推送并设置上游分支
git push --force-with-lease安全强制推送
git push <remote> --delete <branch>删除远端分支

查看历史

命令说明
git log查看提交历史
git log --oneline单行查看提交历史
git log --oneline --graph --decorate图形化查看历史
git log -p查看提交补丁
git log --stat查看提交文件统计
git log --author="Name"按作者查看历史
git log --since="2026-01-01"按开始日期查看历史
git log --until="2026-12-31"按结束日期查看历史
git log -- <file>查看文件历史
git show <commit>查看提交详情
git show --stat <commit>查看提交统计
git reflog查看引用变更历史
git shortlog按作者汇总提交
git blame <file>查看文件逐行作者

差异命令

命令说明
git diff查看工作区未暂存差异
git diff --staged查看已暂存差异
git diff --cached查看已暂存差异
git diff HEAD查看相对 HEAD 的差异
git diff <commit>查看相对指定提交的差异
git diff <commit1> <commit2>查看两个提交差异
git diff <branch1> <branch2>查看两个分支差异
git diff -- <file>查看指定文件差异
git diff --name-only只显示变化文件名
git diff --stat显示差异统计
git diff --check检查空白字符问题

撤销和恢复

命令说明
git restore <file>丢弃文件工作区修改
git restore .丢弃当前目录工作区修改
git restore --source=<commit> <file>从指定提交恢复文件
git checkout -- <file>丢弃文件工作区修改
git reset <commit>回退提交并保留工作区
git reset --soft <commit>回退提交并保留暂存区
git reset --mixed <commit>回退提交并取消暂存
git reset --hard <commit>回退提交并丢弃修改
git reset --hard HEAD丢弃所有未提交修改
git revert <commit>创建反向提交
git revert --no-commit <commit>反向修改但不提交
git clean -n预览未跟踪文件清理
git clean -fd删除未跟踪文件和目录
git clean -fdx删除未跟踪和忽略文件

Stash 命令

命令说明
git stash暂存当前修改
git stash push -m "message"带说明暂存修改
git stash -u暂存包含未跟踪文件的修改
git stash -a暂存包含忽略文件的修改
git stash list查看 stash 列表
git stash show查看最近 stash 摘要
git stash show -p查看最近 stash 补丁
git stash apply应用最近 stash
git stash apply stash@{0}应用指定 stash
git stash pop应用并删除最近 stash
git stash drop stash@{0}删除指定 stash
git stash clear清空所有 stash
git stash branch <branch>从 stash 创建分支

Tag 命令

命令说明
git tag查看本地标签
git tag -l "v1.*"按模式查看标签
git tag <tag>创建轻量标签
git tag -a <tag> -m "message"创建附注标签
git tag <tag> <commit>给指定提交打标签
git show <tag>查看标签详情
git tag -d <tag>删除本地标签
git push <remote> <tag>推送指定标签
git push <remote> --tags推送所有标签
git push <remote> --delete <tag>删除远端标签
git fetch --tags拉取标签

文件和索引

命令说明
git ls-files查看已跟踪文件
git ls-files --others --exclude-standard查看未跟踪文件
git rm <file>删除文件并暂存
git rm --cached <file>停止跟踪文件
git mv <old> <new>移动或重命名文件
git grep <keyword>搜索仓库内容
git grep -n <keyword>搜索并显示行号
git archive -o repo.zip HEAD导出当前版本压缩包

Submodule 命令

命令说明
git submodule add <url> <path>添加子模块
git submodule init初始化子模块
git submodule update更新子模块
git submodule update --init --recursive初始化并递归更新子模块
git submodule status查看子模块状态
git submodule foreach git status查看所有子模块状态
git submodule deinit <path>取消初始化子模块

Worktree 命令

命令说明
git worktree list查看 worktree 列表
git worktree add <path> <branch>添加 worktree
git worktree add -b <branch> <path>新建分支并添加 worktree
git worktree remove <path>删除 worktree
git worktree prune清理无效 worktree 记录

Bisect 命令

命令说明
git bisect start开始二分排查
git bisect bad标记当前版本有问题
git bisect good <commit>标记指定版本正常
git bisect reset结束二分排查
git bisect run <command>自动运行命令排查

常用组合

命令说明
git fetch --all --prune同步所有远端并清理引用
git checkout -b <branch> origin/<branch>基于远端分支创建本地分支
git switch -c <branch> origin/<branch>基于远端分支创建本地分支
git branch --merged查看已合并分支
git branch --no-merged查看未合并分支
git log --oneline -10查看最近 10 个提交
git diff --staged --check检查暂存区空白字符问题
git commit --amend --no-edit修改上次提交但不改信息
git reset --soft HEAD~1撤销上次提交并保留暂存
git reset --mixed HEAD~1撤销上次提交并取消暂存
git revert HEAD反向撤销最新提交
git push -u origin HEAD推送当前分支并设置上游
Wyman · 长空

前端与全栈工程师,写关于系统、边界与 AI 工程的手记。相信把复杂的事讲清楚,本身就是一种工程能力。