文章目录
- [Git 常用参数大全:通过目录快速掌握 Git 命令选项](#Git 常用参数大全:通过目录快速掌握 Git 命令选项)
- [一、Git 全局参数](#一、Git 全局参数)
-
- [`--help` ------ 查看命令帮助文档](#
--help—— 查看命令帮助文档) - [`--version` ------ 查看 Git 版本](#
--version—— 查看 Git 版本) - [`-C <path>` ------ 在指定目录执行 Git 命令](#
-C <path>—— 在指定目录执行 Git 命令) - [`-c key=value` ------ 临时覆盖配置项](#
-c key=value—— 临时覆盖配置项) - [`--git-dir=<path>` ------ 指定 Git 数据目录](#
--git-dir=<path>—— 指定 Git 数据目录) - [`--work-tree=<path>` ------ 指定工作目录](#
--work-tree=<path>—— 指定工作目录) - [`--bare` ------ 使用裸仓库模式](#
--bare—— 使用裸仓库模式) - [`--paginate` ------ 使用分页器显示结果](#
--paginate—— 使用分页器显示结果) - [`--no-pager` ------ 禁止分页输出](#
--no-pager—— 禁止分页输出)
- [`--help` ------ 查看命令帮助文档](#
- [二、git init 参数](#二、git init 参数)
-
- [`--bare` ------ 创建裸仓库](#
--bare—— 创建裸仓库) - [`--initial-branch=<name>` ------ 指定默认分支名称](#
--initial-branch=<name>—— 指定默认分支名称) - [`--shared` ------ 创建共享仓库](#
--shared—— 创建共享仓库)
- [`--bare` ------ 创建裸仓库](#
- [三、git clone 参数](#三、git clone 参数)
-
- [`--depth=<n>` ------ 浅克隆最近 n 次提交](#
--depth=<n>—— 浅克隆最近 n 次提交) - [`--single-branch` ------ 只克隆一个分支](#
--single-branch—— 只克隆一个分支) - [`-b <branch>` ------ 指定克隆分支](#
-b <branch>—— 指定克隆分支) - [`--recursive` ------ 克隆所有子模块](#
--recursive—— 克隆所有子模块) - [`--recurse-submodules` ------ 初始化子模块](#
--recurse-submodules—— 初始化子模块) - [`--mirror` ------ 镜像克隆](#
--mirror—— 镜像克隆)
- [`--depth=<n>` ------ 浅克隆最近 n 次提交](#
- [四、git status 参数](#四、git status 参数)
-
- [`-s` ------ 简洁状态输出](#
-s—— 简洁状态输出) - [`--short` ------ 简洁格式显示](#
--short—— 简洁格式显示) - [`-b` ------ 显示当前分支信息](#
-b—— 显示当前分支信息) - [`--ignored` ------ 显示被忽略文件](#
--ignored—— 显示被忽略文件) - [`--untracked-files=no` ------ 隐藏未跟踪文件](#
--untracked-files=no—— 隐藏未跟踪文件)
- [`-s` ------ 简洁状态输出](#
- [五、git add 参数](#五、git add 参数)
-
- [`.` ------ 添加当前目录所有修改](#
.—— 添加当前目录所有修改) - [`-A` ------ 添加所有变更](#
-A—— 添加所有变更) - [`-u` ------ 仅更新已跟踪文件](#
-u—— 仅更新已跟踪文件) - [`-p` ------ 交互式添加代码块](#
-p—— 交互式添加代码块) - [`-i` ------ 交互模式](#
-i—— 交互模式)
- [`.` ------ 添加当前目录所有修改](#
- [六、git commit 参数](#六、git commit 参数)
-
- [`-m` ------ 指定提交信息](#
-m—— 指定提交信息) - [`-a` ------ 自动添加已修改文件](#
-a—— 自动添加已修改文件) - [`--amend` ------ 修改最近一次提交](#
--amend—— 修改最近一次提交) - [`--no-edit` ------ 保持原提交信息](#
--no-edit—— 保持原提交信息) - [`--allow-empty` ------ 创建空提交](#
--allow-empty—— 创建空提交) - [`--signoff` ------ 添加 Signed-off-by 信息](#
--signoff—— 添加 Signed-off-by 信息)
- [`-m` ------ 指定提交信息](#
- [七、git log 参数](#七、git log 参数)
-
- [`--oneline` ------ 单行显示提交记录](#
--oneline—— 单行显示提交记录) - [`--graph` ------ 图形化显示提交关系](#
--graph—— 图形化显示提交关系) - [`--decorate` ------ 显示分支和标签](#
--decorate—— 显示分支和标签) - [`--all` ------ 显示所有分支历史](#
--all—— 显示所有分支历史) - [`--stat` ------ 显示文件统计信息](#
--stat—— 显示文件统计信息) - [`-p` ------ 显示完整 Diff](#
-p—— 显示完整 Diff) - [`--author=<name>` ------ 按作者筛选](#
--author=<name>—— 按作者筛选) - [`--since=<date>` ------ 查看指定时间之后提交](#
--since=<date>—— 查看指定时间之后提交) - [`--until=<date>` ------ 查看指定时间之前提交](#
--until=<date>—— 查看指定时间之前提交) - [`-n <count>` ------ 限制提交数量](#
-n <count>—— 限制提交数量)
- [`--oneline` ------ 单行显示提交记录](#
- [八、git diff 参数](#八、git diff 参数)
-
- [`--staged` ------ 查看暂存区改动](#
--staged—— 查看暂存区改动) - [`--cached` ------ 比较索引区与 HEAD](#
--cached—— 比较索引区与 HEAD) - [`--name-only` ------ 仅显示文件名](#
--name-only—— 仅显示文件名) - [`--stat` ------ 显示修改统计](#
--stat—— 显示修改统计) - [`--word-diff` ------ 单词级差异比较](#
--word-diff—— 单词级差异比较)
- [`--staged` ------ 查看暂存区改动](#
- [九、git branch 参数](#九、git branch 参数)
-
- [`-a` ------ 查看所有分支](#
-a—— 查看所有分支) - [`-r` ------ 查看远程分支](#
-r—— 查看远程分支) - [`-d` ------ 删除已合并分支](#
-d—— 删除已合并分支) - [`-D` ------ 强制删除分支](#
-D—— 强制删除分支) - [`-m` ------ 重命名分支](#
-m—— 重命名分支)
- [`-a` ------ 查看所有分支](#
- [十、git checkout / switch 参数](#十、git checkout / switch 参数)
-
- [`-b` ------ 创建并切换分支](#
-b—— 创建并切换分支) - [`-B` ------ 强制创建或重置分支](#
-B—— 强制创建或重置分支) - [`--detach` ------ 进入 Detached HEAD](#
--detach—— 进入 Detached HEAD)
- [`-b` ------ 创建并切换分支](#
- [十一、git push 参数](#十一、git push 参数)
-
- [`-u` ------ 设置上游分支](#
-u—— 设置上游分支) - [`--force` ------ 强制推送](#
--force—— 强制推送) - [`--force-with-lease` ------ 安全强推](#
--force-with-lease—— 安全强推) - [`--tags` ------ 推送所有标签](#
--tags—— 推送所有标签) - [`--delete` ------ 删除远程分支](#
--delete—— 删除远程分支)
- [`-u` ------ 设置上游分支](#
- [十二、git pull 参数](#十二、git pull 参数)
-
- [`--rebase` ------ 使用 Rebase 拉取更新](#
--rebase—— 使用 Rebase 拉取更新) - [`--ff-only` ------ 仅允许 Fast Forward](#
--ff-only—— 仅允许 Fast Forward) - [`--no-rebase` ------ 使用 Merge 更新](#
--no-rebase—— 使用 Merge 更新)
- [`--rebase` ------ 使用 Rebase 拉取更新](#
- [十三、git fetch 参数](#十三、git fetch 参数)
-
- [`--all` ------ 获取所有远程仓库](#
--all—— 获取所有远程仓库) - [`--prune` ------ 清理已删除远程分支](#
--prune—— 清理已删除远程分支) - [`--tags` ------ 同步标签](#
--tags—— 同步标签)
- [`--all` ------ 获取所有远程仓库](#
- [十四、git stash 参数](#十四、git stash 参数)
-
- [`push` ------ 保存当前修改](#
push—— 保存当前修改) - [`-u` ------ 包含未跟踪文件](#
-u—— 包含未跟踪文件) - [`list` ------ 查看暂存列表](#
list—— 查看暂存列表) - [`show` ------ 查看 stash 内容](#
show—— 查看 stash 内容) - [`pop` ------ 恢复并删除 stash](#
pop—— 恢复并删除 stash) - [`apply` ------ 恢复但保留 stash](#
apply—— 恢复但保留 stash) - [`drop` ------ 删除 stash](#
drop—— 删除 stash)
- [`push` ------ 保存当前修改](#
- [十五、最值得记住的 Git 参数](#十五、最值得记住的 Git 参数)
Git 常用参数大全:通过目录快速掌握 Git 命令选项
Git 本身并不是一个单独的命令,而是一套庞大的版本控制工具集。
无论是 git clone、git commit、git push,还是 git log、git diff,都拥有大量参数可供使用。
本文整理 Git 日常开发中最常见、最实用的参数,并将参数名称和功能简介直接写入标题,方便通过博客目录快速查阅。
一、Git 全局参数
这些参数适用于所有 Git 子命令。
--help ------ 查看命令帮助文档
bash
git --help
git commit --help
打开完整帮助文档。
等价于:
bash
man git-commit
也可以使用:
bash
git commit -h
查看简略帮助。
--version ------ 查看 Git 版本
bash
git --version
输出:
text
git version 2.49.0
常用于确认当前安装版本。
-C <path> ------ 在指定目录执行 Git 命令
无需切换目录即可操作仓库。
bash
git -C /home/user/project status
等价于:
bash
cd /home/user/project
git status
-c key=value ------ 临时覆盖配置项
只对当前命令有效。
bash
git -c core.editor=vim commit
例如:
bash
git -c color.ui=false diff
不会修改配置文件。
--git-dir=<path> ------ 指定 Git 数据目录
bash
git --git-dir=/tmp/repo/.git status
用于操作非当前目录仓库。
--work-tree=<path> ------ 指定工作目录
bash
git --git-dir=/repo/.git \
--work-tree=/repo status
通常与:
text
--git-dir
配合使用。
--bare ------ 使用裸仓库模式
bash
git --bare init
创建:
text
xxx.git
裸仓库。
特点:
没有工作区。
常用于:
- Git Server
- 中央仓库
- 远程仓库
--paginate ------ 使用分页器显示结果
bash
git --paginate log
默认调用:
text
less
浏览长输出。
--no-pager ------ 禁止分页输出
bash
git --no-pager log
适合脚本。
CI 环境也经常使用。
二、git init 参数
--bare ------ 创建裸仓库
bash
git init --bare
生成:
text
project.git
目录结构:
text
HEAD
objects/
refs/
config
没有源码文件。
--initial-branch=<name> ------ 指定默认分支名称
bash
git init --initial-branch=main
创建:
text
main
而不是:
text
master
例如:
bash
git init -b main
简写形式。
--shared ------ 创建共享仓库
bash
git init --shared
允许多人访问。
例如:
bash
git init --shared=group
支持:
text
false
true
group
all
world
三、git clone 参数
--depth=<n> ------ 浅克隆最近 n 次提交
bash
git clone --depth=1 repo.git
只下载最近一次提交。
优点:
- 克隆速度快
- 节省空间
缺点:
无法查看完整历史。
--single-branch ------ 只克隆一个分支
bash
git clone --single-branch
配合:
bash
git clone -b dev --single-branch repo.git
只获取:
text
dev
分支。
-b <branch> ------ 指定克隆分支
bash
git clone -b main repo.git
等价:
bash
git clone --branch main repo.git
--recursive ------ 克隆所有子模块
bash
git clone --recursive repo.git
自动执行:
bash
git submodule update --init
--recurse-submodules ------ 初始化子模块
新版推荐写法。
bash
git clone --recurse-submodules
--mirror ------ 镜像克隆
bash
git clone --mirror repo.git
会复制:
- refs
- tags
- branches
- remote refs
常用于:
备份仓库。
四、git status 参数
-s ------ 简洁状态输出
bash
git status -s
输出:
text
M file.txt
A app.js
?? temp.txt
--short ------ 简洁格式显示
bash
git status --short
等价:
bash
git status -s
-b ------ 显示当前分支信息
bash
git status -sb
输出:
text
## main...origin/main
--ignored ------ 显示被忽略文件
bash
git status --ignored
输出:
text
!! node_modules
--untracked-files=no ------ 隐藏未跟踪文件
bash
git status -uno
适合大型项目。
五、git add 参数
. ------ 添加当前目录所有修改
bash
git add .
包含:
- 修改文件
- 新文件
-A ------ 添加所有变更
bash
git add -A
包含:
- 修改
- 新增
- 删除
-u ------ 仅更新已跟踪文件
bash
git add -u
不会添加新文件。
-p ------ 交互式添加代码块
bash
git add -p
可以逐段提交代码。
非常实用。
-i ------ 交互模式
bash
git add -i
提供菜单式界面。
六、git commit 参数
-m ------ 指定提交信息
bash
git commit -m "fix login"
最常见参数。
-a ------ 自动添加已修改文件
bash
git commit -am "update"
等价:
bash
git add -u
git commit
--amend ------ 修改最近一次提交
bash
git commit --amend
修改:
- message
- 文件内容
--no-edit ------ 保持原提交信息
bash
git commit --amend --no-edit
--allow-empty ------ 创建空提交
bash
git commit --allow-empty -m "trigger ci"
CI/CD 中非常常见。
--signoff ------ 添加 Signed-off-by 信息
bash
git commit --signoff
生成:
text
Signed-off-by: user@example.com
七、git log 参数
--oneline ------ 单行显示提交记录
bash
git log --oneline
输出:
text
a52bc21 update api
dd85aa1 fix bug
--graph ------ 图形化显示提交关系
bash
git log --graph
显示:
text
* commit
|\
| *
*
--decorate ------ 显示分支和标签
bash
git log --decorate
输出:
text
HEAD -> main
origin/main
--all ------ 显示所有分支历史
bash
git log --all
--stat ------ 显示文件统计信息
bash
git log --stat
输出:
text
3 files changed
20 insertions
5 deletions
-p ------ 显示完整 Diff
bash
git log -p
查看每次提交改动。
--author=<name> ------ 按作者筛选
bash
git log --author="Tom"
--since=<date> ------ 查看指定时间之后提交
bash
git log --since="1 week ago"
例如:
bash
git log --since="2026-01-01"
--until=<date> ------ 查看指定时间之前提交
bash
git log --until="2026-06-01"
-n <count> ------ 限制提交数量
bash
git log -n 10
八、git diff 参数
--staged ------ 查看暂存区改动
bash
git diff --staged
等价:
bash
git diff --cached
--cached ------ 比较索引区与 HEAD
bash
git diff --cached
--name-only ------ 仅显示文件名
bash
git diff --name-only
--stat ------ 显示修改统计
bash
git diff --stat
--word-diff ------ 单词级差异比较
bash
git diff --word-diff
适合:
- Markdown
- 文档
- 配置文件
九、git branch 参数
-a ------ 查看所有分支
bash
git branch -a
包括:
- 本地分支
- 远程分支
-r ------ 查看远程分支
bash
git branch -r
-d ------ 删除已合并分支
bash
git branch -d feature
安全删除。
-D ------ 强制删除分支
bash
git branch -D feature
-m ------ 重命名分支
bash
git branch -m old new
十、git checkout / switch 参数
-b ------ 创建并切换分支
bash
git checkout -b dev
或:
bash
git switch -c dev
-B ------ 强制创建或重置分支
bash
git checkout -B dev
--detach ------ 进入 Detached HEAD
bash
git switch --detach
十一、git push 参数
-u ------ 设置上游分支
bash
git push -u origin main
以后可直接:
bash
git push
--force ------ 强制推送
bash
git push --force
危险操作。
可能覆盖历史。
--force-with-lease ------ 安全强推
bash
git push --force-with-lease
推荐替代:
bash
--force
--tags ------ 推送所有标签
bash
git push --tags
--delete ------ 删除远程分支
bash
git push origin --delete dev
十二、git pull 参数
--rebase ------ 使用 Rebase 拉取更新
bash
git pull --rebase
避免产生 Merge Commit。
--ff-only ------ 仅允许 Fast Forward
bash
git pull --ff-only
--no-rebase ------ 使用 Merge 更新
bash
git pull --no-rebase
十三、git fetch 参数
--all ------ 获取所有远程仓库
bash
git fetch --all
--prune ------ 清理已删除远程分支
bash
git fetch --prune
推荐配置:
bash
git config --global fetch.prune true
--tags ------ 同步标签
bash
git fetch --tags
十四、git stash 参数
push ------ 保存当前修改
bash
git stash push
-u ------ 包含未跟踪文件
bash
git stash -u
list ------ 查看暂存列表
bash
git stash list
show ------ 查看 stash 内容
bash
git stash show
pop ------ 恢复并删除 stash
bash
git stash pop
apply ------ 恢复但保留 stash
bash
git stash apply
drop ------ 删除 stash
bash
git stash drop
十五、最值得记住的 Git 参数
bash
git status -sb
git add -p
git commit -am
git commit --amend
git log --oneline --graph --decorate --all
git diff --staged
git clone --depth=1
git push --force-with-lease
git fetch --prune
git pull --rebase
如果只能记住十个 Git 参数,建议优先掌握以上这些,它们几乎覆盖了日常 90% 的 Git 使用场景。