№ 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 | 推送当前分支并设置上游 |