git config — 配置管理设置和读取 Git 配置项。
格式:
git config [<scope>] [options] <key> [<value>]
作用域参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--global |
global(全局) | 当前用户的全局配置,存储在 ~/.gitconfig |
--system |
system(系统) | 系统级配置,存储在 /etc/gitconfig |
--local |
local(本地) | 当前仓库配置,存储在 .git/config(默认) |
--worktree |
worktree(工作树) | 当前工作树配置 |
--file <file> |
file(文件) | 指定配置文件路径 |
--blob <blob> |
blob(二进制大对象) | 从指定 blob 读取配置 |
操作参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--list |
list(列表) | 列出所有配置项 |
-l |
同 --list |
列出所有配置项 |
--get <key> |
get(获取) | 获取指定配置项的值 |
--get-all <key> |
get all(获取全部) | 获取多值配置项的所有值 |
--get-regexp <regex> |
get regexp(正则获取) | 用正则匹配配置项名称 |
--get-urlmatch <name> <url> |
get URL match | 获取匹配 URL 的配置 |
--unset <key> |
unset(取消设置) | 删除配置项 |
--unset-all <key> |
unset all | 删除多值配置项的所有值 |
--replace-all |
replace all(全部替换) | 替换多值配置项的所有值 |
--add |
add(添加) | 向多值配置项添加新值 |
--rename-section <old> <new> |
rename section(重命名节) | 重命名配置节 |
--remove-section <name> |
remove section(删除节) | 删除整个配置节 |
-e, --edit |
edit(编辑) | 用编辑器打开配置文件 |
--includes |
includes(包含) | 解析时考虑 include 指令 |
--no-includes |
no includes | 解析时忽略 include 指令 |
--show-origin |
show origin(显示来源) | 显示配置项来自哪个文件 |
--show-scope |
show scope(显示作用域) | 显示配置项的作用域级别 |
--type <type> |
type(类型) | 指定值类型(bool, int, bool-or-int, path, expiry-date, color) |
--bool |
bool(布尔) | 值类型为布尔 |
--int |
int(整数) | 值类型为整数 |
--bool-or-int |
bool or int | 值类型为布尔或整数 |
--path |
path(路径) | 值类型为路径(展开 ~) |
--expiry-date |
expiry date(过期日期) | 值类型为过期日期 |
--null |
null(空) | 用 NUL 字符分隔输出 |
-z |
同 --null |
用 NUL 字符分隔输出 |
--name-only |
name only(仅名称) | 只输出配置项名称 |
--default <value> |
default(默认值) | 当 --get 找不到时使用的默认值 |
--fixed-value |
fixed value(固定值) | 精确匹配值(不作为正则) |
示例:
# 设置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 查看所有配置及来源
git config --list --show-origin
# 设置默认编辑器
git config --global core.editor "vim"
# 设置默认分支名
git config --global init.defaultBranch main
# 配置别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
# 删除配置项
git config --global --unset user.name
# 编辑全局配置文件
git config --global -e
# 设置换行符处理
git config --global core.autocrlf input
# 设置凭证缓存
git config --global credential.helper cache
git help — 帮助显示 Git 命令的帮助信息。
格式:
git help [options] [<command>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-a, --all |
all(全部) | 列出所有可用命令 |
-g, --guides |
guides(指南) | 列出所有概念指南 |
-c, --config |
config(配置) | 列出所有配置变量 |
-i, --info |
info(信息) | 用 info 格式显示 |
-m, --man |
man(手册) | 用 man 格式显示 |
-w, --web |
web(网页) | 用浏览器打开帮助页面 |
--verbose |
verbose(详细) | 与 --all 配合显示详细描述 |
示例:
# 查看 commit 命令帮助
git help commit
# 列出所有命令
git help -a
# 在浏览器中打开帮助
git help -w merge
# 简短帮助
git commit -h
git clone — 克隆仓库从远程仓库创建本地副本。
格式:
git clone [options] <repository> [<directory>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-l, --local |
local(本地) | 从本地仓库克隆时使用硬链接 |
--no-hardlinks |
no hardlinks(无硬链接) | 不使用硬链接 |
-s, --shared |
shared(共享) | 使用共享对象存储 |
--reference <repo> |
reference(引用) | 使用指定仓库作为引用(节省空间) |
--dissociate |
dissociate(解除关联) | 克隆后断开与引用仓库的关联 |
-q, --quiet |
quiet(安静) | 减少输出信息 |
-v, --verbose |
verbose(详细) | 增加输出信息 |
--progress |
progress(进度) | 强制显示进度 |
--server-option=<opt> |
server option | 传递选项给服务端 |
-n, --no-checkout |
no checkout(不检出) | 克隆后不自动检出 HEAD |
--bare |
bare(裸) | 创建裸仓库(无工作目录) |
--mirror |
mirror(镜像) | 镜像克隆(包含所有引用) |
-o <name>, --origin <name> |
origin(源) | 指定远程名称(默认 origin) |
-b <name>, --branch <name> |
branch(分支) | 克隆指定分支 |
-u <upload-pack>, --upload-pack <upload-pack> |
upload pack | 指定远程 upload-pack 路径 |
--template=<dir> |
template(模板) | 指定模板目录 |
-c <key>=<value>, --config <key>=<value> |
config(配置) | 设置克隆仓库的配置项 |
--depth <n> |
depth(深度) | 浅克隆,只获取最近 n 次提交 |
--shallow-since=<date> |
shallow since(浅克隆起始日期) | 获取指定日期之后的提交 |
--shallow-exclude=<rev> |
shallow exclude(浅克隆排除) | 排除指定引用可达的提交 |
--single-branch |
single branch(单分支) | 只克隆单个分支 |
--no-single-branch |
no single branch | 克隆所有分支(与 --depth 配合时) |
--no-tags |
no tags(无标签) | 不克隆标签 |
--recurse-submodules[=<pathspec>] |
recurse submodules(递归子模块) | 同时克隆子模块 |
--shallow-submodules |
shallow submodules(浅子模块) | 浅克隆子模块 |
--no-shallow-submodules |
no shallow submodules | 不浅克隆子模块 |
-j <n>, --jobs <n> |
jobs(并行任务数) | 并行获取子模块的数量 |
--filter=<filter-spec> |
filter(过滤) | 部分克隆过滤器(如 blob:none) |
--also-filter-submodules |
also filter submodules | 对子模块也应用过滤器 |
--remote-submodules |
remote submodules | 子模块使用远程跟踪分支 |
--no-remote-submodules |
no remote submodules | 子模块不使用远程跟踪分支 |
--sparse |
sparse(稀疏) | 启用稀疏检出 |
--bundle-uri=<uri> |
bundle URI | 从 bundle URI 获取数据 |
--separate-git-dir=<dir> |
separate git dir(分离 git 目录) | 将 .git 目录放在指定位置 |
示例:
# 基本克隆
git clone https://github.com/user/repo.git
# 克隆到指定目录
git clone https://github.com/user/repo.git my-folder
# 浅克隆(只获取最近1次提交,加速下载)
git clone --depth 1 https://github.com/user/repo.git
# 克隆指定分支
git clone --branch develop https://github.com/user/repo.git
# 克隆并初始化子模块
git clone --recurse-submodules https://github.com/user/repo.git
# 使用 SSH 克隆
git clone git@github.com:user/repo.git
# 部分克隆(不下载 blob,按需获取)
git clone --filter=blob:none https://github.com/user/repo.git
# 稀疏检出克隆
git clone --sparse https://github.com/user/repo.git
# 镜像克隆
git clone --mirror https://github.com/user/repo.git
git init — 初始化仓库在当前目录或指定目录创建新的 Git 仓库。
格式:
git init [options] [<directory>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-q, --quiet |
quiet(安静) | 只输出错误和警告信息 |
--bare |
bare(裸) | 创建裸仓库(无工作目录,用于服务器) |
--template=<dir> |
template(模板) | 指定模板目录 |
--separate-git-dir=<dir> |
separate git dir(分离 git 目录) | 将 .git 目录放在指定位置 |
-b <name>, --initial-branch=<name> |
branch / initial branch(初始分支) | 指定初始分支名称 |
--shared[=<permissions>] |
shared(共享) | 设置仓库的共享权限(group, all, umask, 0xxx) |
--object-format=<format> |
object format(对象格式) | 指定哈希算法(sha1 或 sha256) |
示例:
# 在当前目录初始化
git init
# 在指定目录初始化
git init my-project
# 创建裸仓库
git init --bare my-project.git
# 指定初始分支名为 main
git init -b main
# 使用 SHA-256
git init --object-format=sha256
git add — 添加到暂存区将工作区的更改添加到暂存区(索引)。
格式:
git add [options] [<pathspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-A, --all |
All(全部) | 添加所有更改(新增、修改、删除) |
. |
(当前目录) | 添加当前目录及子目录下的所有更改 |
-u, --update |
update(更新) | 只添加已跟踪文件的修改和删除,不添加新文件 |
-p, --patch |
patch(补丁) | 交互式选择要暂存的代码块(hunk) |
-i, --interactive |
interactive(交互) | 进入交互模式 |
-n, --dry-run |
no action / dry run(模拟运行) | 不实际添加,只显示将会添加的文件 |
-f, --force |
force(强制) | 强制添加被 .gitignore 忽略的文件 |
-e, --edit |
edit(编辑) | 在编辑器中手动编辑差异后暂存 |
-N, --intent-to-add |
New / intent to add(意图添加) | 标记文件将被添加(但暂不暂存内容) |
--refresh |
refresh(刷新) | 只刷新索引中的 stat 信息 |
--ignore-errors |
ignore errors(忽略错误) | 遇到错误时继续添加其他文件 |
--ignore-missing |
ignore missing(忽略缺失) | 与 --dry-run 配合,忽略缺失文件 |
--no-warn-embedded-repo |
no warn embedded repo | 不警告嵌入的仓库 |
--renormalize |
renormalize(重新规范化) | 对已跟踪文件重新应用 clean 过滤器 |
--chmod=(+\|-)x |
chmod(修改权限) | 设置文件的可执行权限 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
--pathspec-file-nul |
pathspec file NUL | 路径规范文件使用 NUL 分隔 |
--sparse |
sparse(稀疏) | 允许更新稀疏检出范围外的索引条目 |
--verbose |
verbose(详细) | 显示详细信息 |
示例:
# 添加单个文件
git add file.txt
# 添加所有更改
git add -A
# 添加当前目录下所有更改
git add .
# 交互式选择暂存内容
git add -p
# 强制添加被忽略的文件
git add -f build/output.log
# 标记新文件(稍后再暂存内容)
git add -N new_file.txt
# 模拟运行
git add -n .
git commit — 提交将暂存区的内容记录到仓库历史中。
格式:
git commit [options] [--] [<pathspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-m <msg>, --message=<msg> |
message(消息) | 指定提交信息 |
-a, --all |
all(全部) | 自动暂存所有已跟踪文件的修改和删除后提交 |
--amend |
amend(修改) | 修改最近一次提交 |
-v, --verbose |
verbose(详细) | 在编辑器中显示差异信息 |
-q, --quiet |
quiet(安静) | 减少输出 |
--no-edit |
no edit(不编辑) | 使用已有提交信息(常与 --amend 配合) |
--allow-empty |
allow empty(允许空) | 允许空提交(无更改) |
--allow-empty-message |
allow empty message | 允许空提交信息 |
-e, --edit |
edit(编辑) | 强制打开编辑器编辑提交信息 |
-C <commit>, --reuse-message=<commit> |
Copy / reuse message(复用消息) | 复用指定提交的信息(作者、日期、消息) |
-c <commit>, --reedit-message=<commit> |
copy edit / reedit message | 复用并编辑指定提交的信息 |
--fixup=<commit> |
fixup(修正) | 创建 fixup 提交(用于 rebase --autosquash) |
--squash=<commit> |
squash(压缩) | 创建 squash 提交 |
-F <file>, --file=<file> |
File(文件) | 从文件读取提交信息 |
-t <file>, --template=<file> |
template(模板) | 使用模板文件 |
-s, --signoff |
signoff(签署) | 添加 Signed-off-by 行 |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名提交 |
--no-gpg-sign |
no GPG sign | 不签名 |
-n, --no-verify |
no verify(不验证) | 跳过 pre-commit 和 commit-msg 钩子 |
--author=<author> |
author(作者) | 指定提交作者 |
--date=<date> |
date(日期) | 指定提交日期 |
--cleanup=<mode> |
cleanup(清理) | 提交信息清理模式(strip, whitespace, verbatim, scissors, default) |
-p, --patch |
patch(补丁) | 交互式选择要提交的内容 |
-o, --only |
only(仅) | 只提交命令行指定的文件 |
-i, --include |
include(包含) | 在提交前额外暂存命令行指定的文件 |
--reset-author |
reset author(重置作者) | 重置作者为当前用户(常与 --amend 配合) |
--short |
short(简短) | 简短输出 |
--branch |
branch(分支) | 显示分支信息 |
--porcelain |
porcelain(瓷器/高层) | 机器可读的输出格式 |
--long |
long(长) | 长格式输出 |
--dry-run |
dry run(模拟运行) | 不实际提交,只显示将要提交的内容 |
--status |
status(状态) | 在编辑器中包含状态信息 |
--no-status |
no status | 不包含状态信息 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
--trailer <token>=<value> |
trailer(尾部标记) | 添加 trailer 到提交信息 |
--untracked-files[=<mode>] |
untracked files(未跟踪文件) | 显示未跟踪文件(no, normal, all) |
示例:
# 基本提交
git commit -m "feat: add login feature"
# 暂存并提交所有已跟踪文件
git commit -am "fix: resolve crash issue"
# 修改最近一次提交
git commit --amend -m "updated commit message"
# 修改最近一次提交但不改信息
git commit --amend --no-edit
# 空提交(常用于触发 CI)
git commit --allow-empty -m "trigger build"
# 跳过钩子
git commit -n -m "wip: quick save"
# 创建 fixup 提交
git commit --fixup=abc1234
# 签名提交
git commit -S -m "signed commit"
# 指定作者
git commit --author="Name <email>" -m "message"
git status — 查看状态显示工作区和暂存区的状态。
格式:
git status [options] [--] [<pathspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-s, --short |
short(简短) | 简洁输出格式 |
-b, --branch |
branch(分支) | 显示分支和跟踪信息 |
--show-stash |
show stash(显示暂存) | 显示 stash 条目数量 |
--porcelain[=<version>] |
porcelain(瓷器/高层) | 机器可读的稳定输出格式 |
--long |
long(长) | 长格式输出(默认) |
-v, --verbose |
verbose(详细) | 显示已暂存的差异;-vv 同时显示未暂存的差异 |
-u[<mode>], --untracked-files[=<mode>] |
untracked(未跟踪) | 显示未跟踪文件(no, normal, all) |
--ignored[=<mode>] |
ignored(已忽略) | 显示被忽略的文件(traditional, no, matching) |
--ignore-submodules[=<when>] |
ignore submodules | 忽略子模块变更(none, untracked, dirty, all) |
-z |
zero(零) | 用 NUL 字符分隔输出 |
--column[=<options>] |
column(列) | 以列格式显示 |
--no-column |
no column | 不以列格式显示 |
--ahead-behind |
ahead behind(领先/落后) | 显示与上游的领先/落后提交数 |
--no-ahead-behind |
no ahead behind | 不显示领先/落后信息 |
--renames |
renames(重命名) | 检测重命名 |
--no-renames |
no renames | 不检测重命名 |
--find-renames[=<n>] |
find renames(查找重命名) | 设置重命名检测阈值 |
示例:
# 查看状态
git status
# 简洁模式
git status -s
# 显示分支信息的简洁模式
git status -sb
# 显示被忽略的文件
git status --ignored
# 显示 stash 数量
git status --show-stash
# 详细模式(显示差异)
git status -v
git rm — 删除文件从工作区和索引中删除文件。
格式:
git rm [options] [--] <file>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--cached |
cached(缓存/索引) | 只从索引(暂存区)删除,保留工作区文件 |
-f, --force |
force(强制) | 强制删除(即使文件有未暂存的修改) |
-r |
recursive(递归) | 递归删除目录 |
-n, --dry-run |
no action / dry run | 模拟运行 |
-q, --quiet |
quiet(安静) | 减少输出 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
--sparse |
sparse(稀疏) | 允许更新稀疏检出范围外的条目 |
示例:
# 删除文件
git rm file.txt
# 只从 Git 跟踪中移除,保留本地文件
git rm --cached file.txt
# 递归删除目录
git rm -r folder/
# 停止跟踪某类文件
git rm --cached "*.log"
git mv — 移动/重命名文件移动或重命名文件、目录。
格式:
git mv [options] <source> <destination>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-f, --force |
force(强制) | 强制移动(即使目标已存在) |
-k |
keep going(继续) | 跳过会导致错误的移动操作 |
-n, --dry-run |
no action / dry run | 模拟运行 |
-v, --verbose |
verbose(详细) | 显示详细信息 |
示例:
# 重命名文件
git mv old_name.txt new_name.txt
# 移动文件到子目录
git mv file.txt src/file.txt
# 强制移动
git mv -f source.txt destination.txt
git notes — 提交注释为对象添加或查看注释(不修改对象本身)。
格式:
git notes [<subcommand>] [options]
子命令:
| 子命令 | 含义 |
|---|---|
list |
列出注释 |
add |
添加注释 |
copy |
复制注释 |
append |
追加注释 |
edit |
编辑注释 |
show |
显示注释 |
merge |
合并注释 |
remove |
删除注释 |
prune |
清理无效注释 |
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-m <msg>, --message=<msg> |
message(消息) | 指定注释内容 |
-F <file>, --file=<file> |
File(文件) | 从文件读取注释 |
-f, --force |
force(强制) | 覆盖已有注释 |
--ref=<ref> |
reference(引用) | 指定注释引用命名空间 |
示例:
# 给当前提交添加注释
git notes add -m "This needs review"
# 给指定提交添加注释
git notes add -m "Bug introduced here" abc1234
# 查看注释
git notes show
# 删除注释
git notes remove abc1234
git branch — 分支管理列出、创建、删除、重命名分支。
格式:
git branch [options] [<branch-name>] [<start-point>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-a, --all |
all(全部) | 列出所有分支(本地 + 远程) |
-r, --remotes |
remotes(远程) | 只列出远程跟踪分支 |
-l, --list |
list(列表) | 列出分支(可配合模式匹配) |
-d <branch> |
delete(删除) | 删除已合并的分支 |
-D <branch> |
Delete force(强制删除) | 强制删除分支(等同 -d --force) |
-m [<old>] <new> |
move(移动/重命名) | 重命名分支 |
-M [<old>] <new> |
Move force(强制重命名) | 强制重命名分支 |
-c [<old>] <new> |
copy(复制) | 复制分支 |
-C [<old>] <new> |
Copy force(强制复制) | 强制复制分支 |
-v, --verbose |
verbose(详细) | 显示每个分支的最后一次提交 |
-vv |
verbose × 2 | 显示分支及其上游跟踪信息 |
-q, --quiet |
quiet(安静) | 减少输出 |
-f, --force |
force(强制) | 强制创建/重置分支 |
-t, --track[=<mode>] |
track(跟踪) | 设置上游跟踪(direct, inherit) |
--no-track |
no track | 不设置上游跟踪 |
--set-upstream-to=<upstream> |
set upstream to | 设置上游分支 |
-u <upstream> |
upstream(上游) | 同 --set-upstream-to |
--unset-upstream |
unset upstream | 取消上游分支设置 |
--merged[=<commit>] |
merged(已合并) | 列出已合并到指定提交的分支 |
--no-merged[=<commit>] |
no merged(未合并) | 列出未合并到指定提交的分支 |
--contains[=<commit>] |
contains(包含) | 列出包含指定提交的分支 |
--no-contains[=<commit>] |
no contains | 列出不包含指定提交的分支 |
--points-at <object> |
points at(指向) | 列出指向指定对象的分支 |
--sort=<key> |
sort(排序) | 排序方式(如 -committerdate) |
--format=<format> |
format(格式) | 自定义输出格式 |
--color[=<when>] |
color(颜色) | 彩色输出 |
--no-color |
no color | 不使用颜色 |
--column[=<options>] |
column(列) | 以列格式显示 |
--no-column |
no column | 不以列格式显示 |
--abbrev=<n> |
abbreviation(缩写) | 设置 SHA-1 缩写长度 |
--no-abbrev |
no abbreviation | 不缩写 SHA-1 |
-i, --ignore-case |
ignore case(忽略大小写) | 排序和过滤时忽略大小写 |
--create-reflog |
create reflog | 创建分支的引用日志 |
--edit-description |
edit description(编辑描述) | 编辑分支描述 |
--show-current |
show current(显示当前) | 显示当前分支名 |
示例:
# 列出本地分支
git branch
# 列出所有分支
git branch -a
# 显示当前分支名
git branch --show-current
# 创建新分支
git branch feature/login
# 删除分支
git branch -d feature/login
# 强制删除未合并的分支
git branch -D feature/login
# 重命名当前分支
git branch -m new-name
# 查看分支详细信息(含上游)
git branch -vv
# 查看已合并的分支
git branch --merged
# 设置上游分支
git branch -u origin/main
# 按提交日期排序
git branch --sort=-committerdate
git switch — 切换分支(推荐)Git 2.23+ 引入,专门用于切换分支。
格式:
git switch [options] <branch>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-c <branch>, --create <branch> |
create(创建) | 创建并切换到新分支 |
-C <branch>, --force-create <branch> |
Create force(强制创建) | 创建(或重置)并切换 |
-d, --detach |
detach(分离) | 分离 HEAD 到指定提交 |
--guess |
guess(猜测) | 如果本地没有匹配分支,猜测远程分支(默认开启) |
--no-guess |
no guess | 不猜测远程分支 |
-f, --force |
force(强制) | 强制切换(丢弃本地修改) |
--discard-changes |
discard changes(丢弃更改) | 同 --force |
-m, --merge |
merge(合并) | 切换时三方合并本地修改 |
--conflict=<style> |
conflict(冲突) | 冲突显示样式(merge, diff3, zdiff3) |
-q, --quiet |
quiet(安静) | 减少输出 |
--progress |
progress(进度) | 显示进度 |
--no-progress |
no progress | 不显示进度 |
-t, --track[=<mode>] |
track(跟踪) | 设置上游跟踪 |
--no-track |
no track | 不设置上游跟踪 |
--orphan <branch> |
orphan(孤立) | 创建孤立分支(无历史记录) |
--ignore-other-worktrees |
ignore other worktrees | 忽略其他工作树中的检出 |
--recurse-submodules |
recurse submodules | 递归更新子模块 |
--no-recurse-submodules |
no recurse submodules | 不递归更新子模块 |
示例:
# 切换分支
git switch main
# 创建并切换
git switch -c feature/new-feature
# 切换到上一个分支
git switch -
# 分离 HEAD
git switch --detach v1.0.0
# 创建孤立分支
git switch --orphan gh-pages
git checkout — 切换分支/恢复文件切换分支或恢复工作区文件(Git 2.23 前的多功能命令)。
格式:
git checkout [options] <branch>
git checkout [options] [<tree-ish>] -- <pathspec>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-b <branch> |
branch(分支) | 创建并切换到新分支 |
-B <branch> |
Branch force(强制分支) | 创建(或重置)并切换 |
-l |
log(日志) | 创建分支时创建 reflog |
-d, --detach |
detach(分离) | 分离 HEAD |
-t, --track[=<mode>] |
track(跟踪) | 设置上游跟踪 |
--no-track |
no track | 不设置上游跟踪 |
--guess |
guess(猜测) | 猜测远程分支 |
--no-guess |
no guess | 不猜测 |
-f, --force |
force(强制) | 强制切换(丢弃本地修改) |
--orphan <branch> |
orphan(孤立) | 创建孤立分支 |
-m, --merge |
merge(合并) | 切换时三方合并 |
--conflict=<style> |
conflict(冲突) | 冲突显示样式 |
-p, --patch |
patch(补丁) | 交互式恢复文件 |
--ours |
ours(我们的) | 冲突时选择当前分支版本 |
--theirs |
theirs(他们的) | 冲突时选择目标分支版本 |
-q, --quiet |
quiet(安静) | 减少输出 |
--progress |
progress(进度) | 显示进度 |
--no-progress |
no progress | 不显示进度 |
--recurse-submodules |
recurse submodules | 递归更新子模块 |
--no-recurse-submodules |
no recurse submodules | 不递归更新子模块 |
--overlay |
overlay(覆盖) | 覆盖模式(不删除索引中多余的文件) |
--no-overlay |
no overlay | 非覆盖模式 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
示例:
# 切换分支
git checkout develop
# 创建并切换到新分支
git checkout -b feature/new-feature
# 从指定分支创建新分支
git checkout -b feature/new origin/develop
# 恢复单个文件到最近提交的状态
git checkout -- file.txt
# 从指定提交恢复文件
git checkout abc1234 -- file.txt
# 创建孤立分支
git checkout --orphan gh-pages
git restore — 恢复文件(推荐)Git 2.23+ 引入,专门用于恢复工作区/暂存区文件。
格式:
git restore [options] [--] <pathspec>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-s <tree>, --source=<tree> |
source(来源) | 指定恢复来源(提交、分支等) |
-W, --worktree |
Worktree(工作树) | 恢复工作区文件(默认) |
-S, --staged |
Staged(暂存区) | 恢复暂存区(取消 git add) |
-p, --patch |
patch(补丁) | 交互式选择恢复内容 |
--ours |
ours(我们的) | 冲突时选择当前分支版本 |
--theirs |
theirs(他们的) | 冲突时选择目标分支版本 |
-m, --merge |
merge(合并) | 重新创建冲突合并 |
--conflict=<style> |
conflict(冲突) | 冲突显示样式 |
--ignore-unmerged |
ignore unmerged(忽略未合并) | 恢复时忽略未合并的条目 |
--ignore-skip-worktree-bits |
ignore skip worktree bits | 忽略 skip-worktree 位 |
--recurse-submodules |
recurse submodules | 递归恢复子模块 |
--no-recurse-submodules |
no recurse submodules | 不递归恢复子模块 |
--overlay |
overlay(覆盖) | 覆盖模式 |
--no-overlay |
no overlay | 非覆盖模式(删除来源中不存在的文件) |
--progress |
progress(进度) | 显示进度 |
--no-progress |
no progress | 不显示进度 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
--pathspec-file-nul |
pathspec file NUL | 路径规范文件使用 NUL 分隔 |
示例:
# 丢弃工作区的修改
git restore file.txt
# 取消暂存(撤销 git add)
git restore --staged file.txt
# 从指定提交恢复文件
git restore --source=HEAD~2 file.txt
# 同时恢复工作区和暂存区
git restore --staged --worktree file.txt
# 交互式恢复
git restore -p file.txt
git push — 推送将本地提交推送到远程仓库。
格式:
git push [options] [<remote>] [<refspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-u, --set-upstream |
upstream / set upstream(设置上游) | 推送并设置上游跟踪分支 |
-f, --force |
force(强制) | 强制推送(覆盖远程历史,危险) |
--force-with-lease[=<ref>] |
force with lease(带租约的强制) | 安全的强制推送(检查远程是否被他人更新) |
--force-if-includes |
force if includes | 确保本地 reflog 包含远程的最新提交 |
--all |
all(全部) | 推送所有分支 |
--mirror |
mirror(镜像) | 镜像推送所有引用 |
--tags |
tags(标签) | 推送所有标签 |
--follow-tags |
follow tags(跟随标签) | 推送提交可达的附注标签 |
--no-tags |
no tags | 不推送标签 |
-d, --delete |
delete(删除) | 删除远程分支或标签 |
--prune |
prune(修剪) | 删除远程上没有对应本地分支的远程分支 |
-n, --dry-run |
no action / dry run | 模拟运行 |
--porcelain |
porcelain | 机器可读的输出 |
-v, --verbose |
verbose(详细) | 详细输出 |
-q, --quiet |
quiet(安静) | 减少输出 |
--progress |
progress(进度) | 强制显示进度 |
--no-verify |
no verify(不验证) | 跳过 pre-push 钩子 |
--signed=<mode> |
signed(签名) | GPG 签名推送(true, false, if-asked) |
--no-signed |
no signed | 不签名 |
--atomic |
atomic(原子) | 原子推送(全部成功或全部失败) |
-o <option>, --push-option=<option> |
option / push option | 传递选项给服务端 |
--repo=<repo> |
repository(仓库) | 指定远程仓库 |
--recurse-submodules=<check> |
recurse submodules | 子模块推送检查(check, on-demand, only, no) |
--thin |
thin(精简) | 精简传输 |
--no-thin |
no thin | 不精简传输 |
--receive-pack=<path> |
receive pack | 指定远程 receive-pack 路径 |
--exec=<path> |
execute(执行) | 同 --receive-pack |
--ipv4 |
IPv4 | 只使用 IPv4 |
--ipv6 |
IPv6 | 只使用 IPv6 |
--[no-]auto-gc |
auto gc(自动垃圾回收) | 控制是否触发远程的自动 gc |
示例:
# 推送当前分支
git push
# 首次推送并设置上游
git push -u origin main
# 安全的强制推送(推荐)
git push --force-with-lease origin main
# 删除远程分支
git push origin --delete feature/old-branch
git push origin :feature/old-branch # 简写
# 推送所有标签
git push --tags
# 原子推送
git push --atomic origin main feature
git pull — 拉取从远程获取并合并到当前分支(= fetch + merge 或 fetch + rebase)。
格式:
git pull [options] [<remote>] [<refspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--rebase[=<mode>] |
rebase(变基) | 使用变基代替合并(false, true, merges, interactive) |
--no-rebase |
no rebase | 使用合并(默认) |
--ff |
fast forward(快进) | 允许快进合并(默认) |
--ff-only |
fast forward only | 只允许快进合并 |
--no-ff |
no fast forward | 禁止快进合并,总是创建合并提交 |
--squash |
squash(压缩) | 压缩合并 |
--no-squash |
no squash | 不压缩合并 |
--commit |
commit(提交) | 合并后自动提交(默认) |
--no-commit |
no commit | 合并但不自动提交 |
--edit |
edit(编辑) | 编辑合并提交信息 |
--no-edit |
no edit | 不编辑合并提交信息 |
--autostash |
auto stash(自动暂存) | 自动暂存本地修改,操作后恢复 |
--no-autostash |
no auto stash | 不自动暂存 |
-s <strategy>, --strategy=<strategy> |
strategy(策略) | 指定合并策略 |
-X <option>, --strategy-option=<option> |
eXtra option(额外选项) | 传递合并策略选项 |
--all |
all(全部) | 获取所有远程仓库 |
--depth <n> |
depth(深度) | 限制获取深度 |
--deepen=<n> |
deepen(加深) | 加深浅克隆 |
--shallow-since=<date> |
shallow since | 浅获取起始日期 |
--shallow-exclude=<rev> |
shallow exclude | 浅获取排除引用 |
--unshallow |
unshallow(取消浅) | 转换为完整仓库 |
--tags |
tags(标签) | 获取标签 |
--no-tags |
no tags | 不获取标签 |
-v, --verbose |
verbose(详细) | 详细输出 |
-q, --quiet |
quiet(安静) | 减少输出 |
--progress |
progress(进度) | 显示进度 |
--allow-unrelated-histories |
allow unrelated histories | 允许合并无关历史 |
--recurse-submodules[=<mode>] |
recurse submodules | 递归更新子模块 |
--verify-signatures |
verify signatures(验证签名) | 验证合并提交的 GPG 签名 |
--signoff |
signoff(签署) | 添加 Signed-off-by |
--no-signoff |
no signoff | 不添加 Signed-off-by |
--stat |
statistics(统计) | 显示合并统计 |
-n, --no-stat |
no stat | 不显示合并统计 |
--log[=<n>] |
log(日志) | 在合并提交信息中包含日志 |
--no-log |
no log | 不包含日志 |
--set-upstream |
set upstream | 设置上游跟踪 |
--ipv4 |
IPv4 | 只使用 IPv4 |
--ipv6 |
IPv6 | 只使用 IPv6 |
示例:
# 拉取并合并
git pull
# 拉取并变基
git pull --rebase
# 拉取指定远程分支
git pull origin develop
# 只允许快进合并
git pull --ff-only
# 自动暂存本地修改后拉取
git pull --autostash --rebase
# 允许合并无关历史
git pull --allow-unrelated-histories origin main
git fetch — 获取从远程仓库获取最新数据(不合并)。
格式:
git fetch [options] [<remote>] [<refspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--all |
all(全部) | 获取所有远程仓库的数据 |
-p, --prune |
prune(修剪) | 删除本地已不存在于远程的分支引用 |
-P, --prune-tags |
Prune tags(修剪标签) | 删除本地已不存在于远程的标签 |
--tags |
tags(标签) | 获取所有标签 |
-t |
tags(标签) | 同 --tags |
--no-tags |
no tags | 不获取标签 |
-f, --force |
force(强制) | 强制更新本地引用 |
--depth <n> |
depth(深度) | 限制获取的提交深度 |
--deepen=<n> |
deepen(加深) | 加深浅克隆 n 个提交 |
--shallow-since=<date> |
shallow since | 浅获取起始日期 |
--shallow-exclude=<rev> |
shallow exclude | 浅获取排除引用 |
--unshallow |
unshallow(取消浅) | 转换为完整仓库 |
--update-shallow |
update shallow | 更新浅克隆边界 |
--negotiate-only |
negotiate only | 只协商不获取 |
--negotiation-tip=<ref> |
negotiation tip | 协商时使用的提示引用 |
--dry-run |
dry run | 模拟运行 |
-k, --keep |
keep(保留) | 保留下载的包文件 |
--multiple |
multiple(多个) | 允许指定多个远程 |
--auto-maintenance |
auto maintenance | 获取后运行自动维护 |
--no-auto-maintenance |
no auto maintenance | 不运行自动维护 |
--auto-gc |
auto gc | 获取后运行自动垃圾回收 |
--no-auto-gc |
no auto gc | 不运行自动垃圾回收 |
--write-fetch-head |
write fetch head | 写入 FETCH_HEAD(默认) |
--no-write-fetch-head |
no write fetch head | 不写入 FETCH_HEAD |
--prefetch |
prefetch(预获取) | 修改 refspec 以放入 refs/prefetch/ |
--recurse-submodules[=<mode>] |
recurse submodules | 递归获取子模块(yes, on-demand, no) |
--no-recurse-submodules |
no recurse submodules | 不递归获取子模块 |
-j <n>, --jobs=<n> |
jobs(并行任务数) | 并行获取子模块的数量 |
--submodule-prefix=<path> |
submodule prefix | 子模块路径前缀 |
--set-upstream |
set upstream | 设置上游跟踪 |
--upload-pack <path> |
upload pack | 指定远程 upload-pack 路径 |
-v, --verbose |
verbose(详细) | 详细输出 |
-q, --quiet |
quiet(安静) | 减少输出 |
--progress |
progress(进度) | 显示进度 |
--server-option=<opt> |
server option | 传递选项给服务端 |
--show-forced-updates |
show forced updates | 显示强制更新 |
--no-show-forced-updates |
no show forced updates | 不显示强制更新 |
--filter=<filter-spec> |
filter(过滤) | 部分克隆过滤器 |
--ipv4 |
IPv4 | 只使用 IPv4 |
--ipv6 |
IPv6 | 只使用 IPv6 |
--stdin |
stdin(标准输入) | 从标准输入读取 refspec |
-o <option> |
option(选项) | 传递选项给服务端 |
-4 |
同 --ipv4 |
只使用 IPv4 |
-6 |
同 --ipv6 |
只使用 IPv6 |
示例:
# 获取默认远程仓库的更新
git fetch
# 获取所有远程仓库
git fetch --all
# 获取并清理无效引用
git fetch --prune
# 获取指定远程仓库
git fetch upstream
# 获取指定分支
git fetch origin main
git remote — 远程仓库管理管理远程仓库的配置。
格式:
git remote [options]
git remote <subcommand> [options]
子命令与参数:
| 子命令/参数 | 缩写含义 | 说明 |
|---|---|---|
add <name> <url> |
add(添加) | 添加远程仓库 |
rename <old> <new> |
rename(重命名) | 重命名远程仓库 |
remove <name> / rm <name> |
remove(删除) | 删除远程仓库 |
set-head <name> <branch> |
set head(设置 HEAD) | 设置远程的默认分支 |
set-branches <name> <branch>... |
set branches | 设置跟踪的远程分支 |
get-url <name> |
get URL | 获取远程仓库 URL |
set-url <name> <newurl> |
set URL | 修改远程仓库 URL |
set-url --add <name> <url> |
set URL add | 添加额外的 URL |
set-url --delete <name> <url> |
set URL delete | 删除指定 URL |
show <name> |
show(显示) | 显示远程仓库详细信息 |
prune <name> |
prune(修剪) | 清理已删除的远程分支引用 |
update [<group>] |
update(更新) | 获取远程仓库组的更新 |
-v, --verbose |
verbose(详细) | 显示远程仓库的 URL |
add 子命令参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-f |
fetch(获取) | 添加后立即 fetch |
--tags |
tags(标签) | 导入所有标签 |
--no-tags |
no tags | 不导入标签 |
-t <branch> |
track(跟踪) | 只跟踪指定分支 |
-m <master> |
master(主分支) | 设置远程 HEAD 指向的分支 |
--mirror=<type> |
mirror(镜像) | 镜像类型(fetch, push) |
示例:
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
# 修改远程 URL
git remote set-url origin git@github.com:user/repo.git
# 查看远程仓库详情
git remote show origin
# 删除远程仓库
git remote remove upstream
# 重命名远程仓库
git remote rename origin main-remote
# 清理无效的远程分支引用
git remote prune origin
git merge — 合并将指定分支合并到当前分支。
格式:
git merge [options] [<commit>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--no-ff |
no fast forward(禁止快进) | 始终创建合并提交 |
--ff |
fast forward(快进) | 允许快进合并(默认) |
--ff-only |
fast forward only | 只允许快进合并 |
--squash |
squash(压缩) | 压缩合并(将所有提交合为一个暂存) |
--no-squash |
no squash | 不压缩合并 |
--commit |
commit(提交) | 合并后自动提交(默认) |
--no-commit |
no commit | 合并但不自动提交 |
-e, --edit |
edit(编辑) | 编辑合并提交信息 |
--no-edit |
no edit | 不编辑合并提交信息 |
-m <msg> |
message(消息) | 指定合并提交信息 |
-s <strategy>, --strategy=<strategy> |
strategy(策略) | 指定合并策略(ort, recursive, resolve, octopus, ours, subtree) |
-X <option>, --strategy-option=<option> |
eXtra option(额外选项) | 传递合并策略选项(ours, theirs, patience, diff-algorithm=<algo>, renormalize, no-renormalize, no-renames, find-renames=<n>, subtree=<path>) |
--abort |
abort(中止) | 中止合并(冲突时使用) |
--continue |
continue(继续) | 解决冲突后继续合并 |
--quit |
quit(退出) | 退出合并但保留当前状态 |
--stat |
statistics(统计) | 显示合并统计 |
-n, --no-stat |
no stat | 不显示合并统计 |
--log[=<n>] |
log(日志) | 在合并提交信息中包含被合并分支的日志 |
--no-log |
no log | 不包含日志 |
--signoff |
signoff(签署) | 添加 Signed-off-by |
--no-signoff |
no signoff | 不添加 Signed-off-by |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名合并提交 |
--no-gpg-sign |
no GPG sign | 不签名 |
--verify-signatures |
verify signatures | 验证被合并提交的签名 |
--no-verify-signatures |
no verify signatures | 不验证签名 |
--allow-unrelated-histories |
allow unrelated histories | 允许合并无关历史 |
--progress |
progress(进度) | 显示进度 |
--no-progress |
no progress | 不显示进度 |
--autostash |
auto stash | 自动暂存本地修改 |
--no-autostash |
no auto stash | 不自动暂存 |
--overwrite-ignore |
overwrite ignore | 覆盖被忽略的文件 |
--no-overwrite-ignore |
no overwrite ignore | 不覆盖被忽略的文件 |
-q, --quiet |
quiet(安静) | 减少输出 |
-v, --verbose |
verbose(详细) | 详细输出 |
--into-name <branch> |
into name | 为合并信息指定目标分支名 |
--rerere-autoupdate |
rerere auto update | 允许 rerere 自动更新索引 |
--no-rerere-autoupdate |
no rerere auto update | 不允许 rerere 自动更新 |
示例:
# 合并分支
git merge feature/login
# 禁止快进合并
git merge --no-ff feature/login
# 压缩合并
git merge --squash feature/login
# 中止合并
git merge --abort
# 使用 theirs 策略解决冲突
git merge -X theirs feature/login
# 合并但不自动提交
git merge --no-commit feature/login
# 允许合并无关历史
git merge --allow-unrelated-histories other-repo/main
git rebase — 变基将当前分支的提交变基到另一个分支上。
格式:
git rebase [options] [<upstream> [<branch>]]
git rebase [options] --onto <newbase> [<upstream> [<branch>]]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-i, --interactive |
interactive(交互式) | 交互式变基 |
--onto <newbase> |
onto(到…上) | 指定新的基底 |
--abort |
abort(中止) | 中止变基 |
--continue |
continue(继续) | 解决冲突后继续变基 |
--skip |
skip(跳过) | 跳过当前冲突的提交 |
--quit |
quit(退出) | 退出变基但保留当前状态 |
--edit-todo |
edit todo(编辑待办) | 编辑交互式变基的待办列表 |
--autosquash |
auto squash(自动压缩) | 自动处理 fixup/squash 提交 |
--no-autosquash |
no auto squash | 不自动处理 |
--autostash |
auto stash(自动暂存) | 自动暂存本地修改 |
--no-autostash |
no auto stash | 不自动暂存 |
--rebase-merges[=<mode>] |
rebase merges(变基合并) | 保留合并提交(rebase-cousins, no-rebase-cousins) |
--no-rebase-merges |
no rebase merges | 不保留合并提交 |
-x <cmd>, --exec <cmd> |
exec(执行) | 每个提交后执行 shell 命令 |
--root |
root(根) | 从根提交开始变基 |
--keep-empty |
keep empty(保留空) | 保留空提交 |
--no-keep-empty |
no keep empty | 不保留空提交 |
--empty=<action> |
empty(空) | 处理变为空的提交(drop, keep, ask) |
-f, --force-rebase |
force rebase(强制变基) | 即使是快进也强制变基 |
--no-ff |
no fast forward | 同 --force-rebase |
--fork-point |
fork point(分叉点) | 使用 reflog 查找更好的分叉点 |
--no-fork-point |
no fork point | 不使用 reflog 查找分叉点 |
-s <strategy>, --strategy=<strategy> |
strategy(策略) | 指定合并策略 |
-X <option>, --strategy-option=<option> |
eXtra option | 传递合并策略选项 |
--stat |
statistics(统计) | 显示统计 |
-n, --no-stat |
no stat | 不显示统计 |
-v, --verbose |
verbose(详细) | 详细输出 |
-q, --quiet |
quiet(安静) | 减少输出 |
--committer-date-is-author-date |
committer date is author date | 使用作者日期作为提交者日期 |
--ignore-date |
ignore date(忽略日期) | 使用当前时间作为作者日期 |
--reset-author-date |
reset author date | 同 --ignore-date |
--signoff |
signoff(签署) | 添加 Signed-off-by |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名提交 |
--no-gpg-sign |
no GPG sign | 不签名 |
--ignore-whitespace |
ignore whitespace | 忽略空白变化 |
--whitespace=<option> |
whitespace(空白) | 空白处理选项 |
-C <n> |
Context(上下文) | 确保至少 n 行上下文匹配 |
--reschedule-failed-exec |
reschedule failed exec | 失败的 exec 命令重新调度 |
--no-reschedule-failed-exec |
no reschedule failed exec | 不重新调度 |
--update-refs |
update refs(更新引用) | 自动更新指向被变基提交的分支 |
--no-update-refs |
no update refs | 不自动更新引用 |
--apply |
apply(应用) | 使用 apply 后端(旧方式) |
-m, --merge |
merge(合并) | 使用 merge 后端 |
交互式变基操作:
| 操作 | 缩写 | 含义 |
|---|---|---|
pick |
p |
保留提交 |
reword |
r |
保留提交但修改提交信息 |
edit |
e |
保留提交但暂停以修改 |
squash |
s |
与前一个提交合并,保留信息 |
fixup |
f |
与前一个提交合并,丢弃信息 |
exec |
x |
执行 shell 命令 |
break |
b |
在此处暂停 |
drop |
d |
删除提交 |
label |
l |
给当前 HEAD 打标签 |
reset |
t |
重置 HEAD 到标签 |
merge |
m |
创建合并提交 |
update-ref |
u |
更新分支引用到当前位置 |
示例:
# 变基到 main 分支
git rebase main
# 交互式变基最近 3 个提交
git rebase -i HEAD~3
# 变基到指定提交
git rebase --onto main feature/base feature/current
# 中止变基
git rebase --abort
# 解决冲突后继续
git rebase --continue
# 自动暂存后变基
git rebase --autostash main
# 每个提交后运行测试
git rebase -i --exec "npm test" HEAD~5
# 自动更新相关分支引用
git rebase --update-refs main
git cherry-pick — 拣选提交将指定提交应用到当前分支。
格式:
git cherry-pick [options] <commit>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n, --no-commit |
no commit(不提交) | 应用但不自动提交 |
-e, --edit |
edit(编辑) | 编辑提交信息 |
-x |
explain(说明) | 在提交信息中记录原始 commit hash |
-m <parent-number>, --mainline <parent-number> |
mainline(主线) | 指定合并提交的主线(1 或 2) |
--abort |
abort(中止) | 中止 cherry-pick |
--continue |
continue(继续) | 解决冲突后继续 |
--skip |
skip(跳过) | 跳过当前提交 |
--quit |
quit(退出) | 退出但保留当前状态 |
-s, --signoff |
signoff(签署) | 添加 Signed-off-by |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名提交 |
--no-gpg-sign |
no GPG sign | 不签名 |
--ff |
fast forward(快进) | 如果可能则快进 |
--allow-empty |
allow empty | 允许空提交 |
--allow-empty-message |
allow empty message | 允许空提交信息 |
--keep-redundant-commits |
keep redundant commits | 保留冗余提交 |
--strategy=<strategy> |
strategy(策略) | 指定合并策略 |
-X <option> |
eXtra option | 传递合并策略选项 |
--rerere-autoupdate |
rerere auto update | 允许 rerere 自动更新 |
--no-rerere-autoupdate |
no rerere auto update | 不允许 rerere 自动更新 |
示例:
# 应用单个提交
git cherry-pick abc1234
# 应用多个提交
git cherry-pick abc1234 def5678
# 应用一个范围的提交(不含起始提交)
git cherry-pick abc1234..def5678
# 应用但不自动提交
git cherry-pick -n abc1234
# 记录原始 hash
git cherry-pick -x abc1234
# 中止 cherry-pick
git cherry-pick --abort
git log — 查看提交历史显示提交日志。
格式:
git log [options] [<revision-range>] [-- <path>...]
常用显示参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--oneline |
one line(一行) | 每个提交显示一行(缩短 hash + 标题) |
--graph |
graph(图形) | 图形化显示分支结构 |
--all |
all(全部) | 显示所有分支的历史 |
--decorate[=<type>] |
decorate(装饰) | 显示引用名称(short, full, auto, no) |
--no-decorate |
no decorate | 不显示引用名称 |
-n <number>, --max-count=<number> |
number / max count | 限制显示的提交数量 |
--skip=<number> |
skip(跳过) | 跳过前 n 个提交 |
--stat |
statistics(统计) | 显示文件修改统计 |
--shortstat |
short stat | 只显示总计统计 |
--numstat |
numeric stat | 显示数字统计(增/删行数) |
--name-only |
name only(仅名称) | 只显示修改的文件名 |
--name-status |
name status(名称和状态) | 显示文件名和修改状态 |
-p, --patch |
patch(补丁) | 显示每次提交的差异 |
-u |
同 --patch |
显示差异 |
--raw |
raw(原始) | 原始格式输出 |
--format=<format> |
format(格式) | 自定义输出格式 |
--pretty=<format> |
pretty(美化) | 同 --format(oneline, short, medium, full, fuller, email, raw, format:<string>) |
--abbrev-commit |
abbreviate commit(缩写提交) | 显示缩短的 commit hash |
--no-abbrev-commit |
no abbreviate commit | 不缩短 hash |
--date=<format> |
date(日期) | 日期格式(relative, local, iso, iso-strict, rfc, short, raw, human, unix, format:<string>) |
--relative-date |
relative date | 相对日期(如 "2 hours ago") |
--parents |
parents(父提交) | 显示父提交 hash |
--children |
children(子提交) | 显示子提交 hash |
--left-right |
left right(左右) | 标记提交来自哪一侧 |
--show-signature |
show signature | 显示 GPG 签名信息 |
--show-notes[=<ref>] |
show notes | 显示注释 |
--no-notes |
no notes | 不显示注释 |
过滤参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--author=<pattern> |
author(作者) | 按作者过滤 |
--committer=<pattern> |
committer(提交者) | 按提交者过滤 |
--grep=<pattern> |
grep(搜索) | 按提交信息过滤 |
--all-match |
all match | 所有 --grep 条件都匹配 |
--invert-grep |
invert grep | 反转 grep 匹配 |
-i, --regexp-ignore-case |
ignore case(忽略大小写) | 正则忽略大小写 |
-E, --extended-regexp |
Extended regexp(扩展正则) | 使用扩展正则表达式 |
-F, --fixed-strings |
Fixed strings(固定字符串) | 按固定字符串匹配 |
-P, --perl-regexp |
Perl regexp(Perl 正则) | 使用 Perl 正则表达式 |
--since=<date>, --after=<date> |
since / after | 显示指定日期之后的提交 |
--until=<date>, --before=<date> |
until / before | 显示指定日期之前的提交 |
-S <string> |
Search(搜索) | 搜索添加或删除了指定字符串的提交(pickaxe) |
-G <regex> |
Grep diff(差异搜索) | 搜索差异中匹配正则的提交 |
-L <range>:<file> |
Line(行) | 查看文件指定行范围的历史 |
--follow |
follow(跟踪) | 跟踪文件重命名 |
--diff-filter=<filter> |
diff filter(差异过滤) | 按修改类型过滤(A=Added, C=Copied, D=Deleted, M=Modified, R=Renamed, T=Type changed) |
--no-merges |
no merges | 不显示合并提交 |
--merges |
merges | 只显示合并提交 |
--min-parents=<n> |
min parents | 最少父提交数 |
--max-parents=<n> |
max parents | 最多父提交数 |
--first-parent |
first parent | 只跟踪第一个父提交 |
--ancestry-path |
ancestry path | 只显示祖先路径上的提交 |
--simplify-by-decoration |
simplify by decoration | 只显示被引用的提交 |
--full-history |
full history | 完整历史 |
--dense |
dense(密集) | 密集模式 |
--sparse |
sparse(稀疏) | 稀疏模式 |
--simplify-merges |
simplify merges | 简化合并 |
--reverse |
reverse(反转) | 反转输出顺序 |
--walk-reflogs |
walk reflogs | 遍历 reflog |
-g |
同 --walk-reflogs |
遍历 reflog |
--boundary |
boundary(边界) | 显示边界提交 |
--source |
source(来源) | 显示提交来自哪个引用 |
--remotes[=<pattern>] |
remotes | 只显示远程分支的提交 |
--branches[=<pattern>] |
branches | 只显示本地分支的提交 |
--tags[=<pattern>] |
tags | 只显示标签的提交 |
--glob=<pattern> |
glob | 匹配引用模式 |
--exclude=<pattern> |
exclude | 排除引用模式 |
--bisect |
bisect | 只显示二分查找的中间提交 |
--stdin |
stdin | 从标准输入读取提交 |
--cherry-mark |
cherry mark | 标记等价提交 |
--cherry-pick |
cherry pick | 省略等价提交 |
--cherry |
cherry | 同 --right-only --cherry-mark --no-merges |
--topo-order |
topological order(拓扑排序) | 拓扑排序 |
--date-order |
date order | 日期排序 |
--author-date-order |
author date order | 作者日期排序 |
常用 pretty 格式占位符:
| 占位符 | 含义 |
|---|---|
%H |
完整 commit hash |
%h |
缩短的 commit hash |
%T |
完整 tree hash |
%t |
缩短的 tree hash |
%P |
完整父提交 hash |
%p |
缩短的父提交 hash |
%an |
作者名 |
%ae |
作者邮箱 |
%aI |
作者日期(ISO 8601) |
%ad |
作者日期(受 --date 影响) |
%ar |
作者日期(相对) |
%cn |
提交者名 |
%ce |
提交者邮箱 |
%cd |
提交者日期 |
%cr |
提交者日期(相对) |
%s |
提交信息标题 |
%b |
提交信息正文 |
%B |
完整提交信息 |
%d |
引用名称(分支、标签) |
%D |
引用名称(无括号) |
%N |
注释 |
%n |
换行 |
%Cred |
红色 |
%Cgreen |
绿色 |
%Cblue |
蓝色 |
%Creset |
重置颜色 |
%C(...) |
自定义颜色 |
示例:
# 简洁日志
git log --oneline
# 图形化显示所有分支
git log --oneline --graph --all
# 最近 5 次提交
git log -5
# 显示文件修改统计
git log --stat
# 按作者过滤
git log --author="John"
# 按日期范围
git log --since="2024-01-01" --until="2024-12-31"
# 按提交信息搜索
git log --grep="fix"
# 查看某个文件的历史(跟踪重命名)
git log --follow -- file.txt
# 自定义格式
git log --pretty=format:"%h %Cgreen%an%Creset %s" --date=short
# 搜索添加了某字符串的提交
git log -S "function_name"
# 查看文件指定行范围的历史
git log -L 10,20:file.txt
# 反转输出顺序
git log --reverse --oneline
git diff — 查看差异显示工作区、暂存区、提交之间的差异。
格式:
git diff [options] [<commit>] [--] [<path>...]
git diff [options] <commit> <commit> [--] [<path>...]
git diff [options] --cached [<commit>] [--] [<path>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--staged, --cached |
staged / cached(暂存/缓存) | 查看暂存区与最近提交的差异 |
--stat |
statistics(统计) | 只显示统计信息 |
--shortstat |
short stat | 只显示总计统计 |
--numstat |
numeric stat | 数字统计 |
--name-only |
name only | 只显示文件名 |
--name-status |
name status | 显示文件名和状态 |
--word-diff[=<mode>] |
word diff(单词差异) | 按单词显示差异(color, plain, porcelain, none) |
--word-diff-regex=<regex> |
word diff regex | 自定义单词边界正则 |
--color-words[=<regex>] |
color words(彩色单词) | 彩色单词差异 |
--color-moved[=<mode>] |
color moved | 彩色显示移动的行 |
--color-moved-ws=<mode> |
color moved whitespace | 移动行的空白处理 |
-w, --ignore-all-space |
whitespace / ignore all space | 忽略所有空白变化 |
-b, --ignore-space-change |
blank / ignore space change | 忽略空白数量变化 |
--ignore-space-at-eol |
ignore space at EOL | 忽略行尾空白 |
--ignore-cr-at-eol |
ignore CR at EOL | 忽略行尾回车符 |
--ignore-blank-lines |
ignore blank lines | 忽略空行变化 |
-I <regex>, --ignore-matching-lines=<regex> |
Ignore matching | 忽略匹配正则的行 |
--no-index |
no index(无索引) | 比较两个非 Git 文件 |
--check |
check(检查) | 检查空白错误 |
-U <n>, --unified=<n> |
Unified(统一格式) | 显示 n 行上下文(默认 3) |
--inter-hunk-context=<n> |
inter hunk context | 合并相距 n 行内的 hunk |
--function-context |
function context | 显示整个函数上下文 |
-W |
同 --function-context |
显示整个函数上下文 |
--output=<file> |
output(输出) | 输出到文件 |
--output-indicator-new=<char> |
output indicator new | 新行指示符 |
--output-indicator-old=<char> |
output indicator old | 旧行指示符 |
--output-indicator-context=<char> |
output indicator context | 上下文行指示符 |
-p |
patch(补丁) | 生成补丁格式(默认) |
-s, --no-patch |
suppress / no patch | 不显示差异 |
--raw |
raw(原始) | 原始格式 |
--patch-with-raw |
patch with raw | 补丁格式 + 原始格式 |
--patch-with-stat |
patch with stat | 补丁格式 + 统计 |
--compact-summary |
compact summary | 紧凑摘要 |
--summary |
summary(摘要) | 显示创建/删除/重命名摘要 |
-z |
zero(零) | 用 NUL 分隔输出 |
--diff-filter=<filter> |
diff filter | 按修改类型过滤 |
-R |
Reverse(反转) | 反转差异方向 |
--relative[=<path>] |
relative(相对) | 相对路径显示 |
-a, --text |
all text / text | 将二进制文件视为文本 |
--binary |
binary(二进制) | 输出二进制差异 |
--full-index |
full index | 显示完整 blob hash |
--abbrev[=<n>] |
abbreviate(缩写) | 缩写 hash 长度 |
-B[<n>][/<m>] |
Break(断开) | 检测完全重写 |
-M[<n>] |
Move(移动) | 检测重命名 |
-C[<n>] |
Copy(复制) | 检测复制 |
--find-copies-harder |
find copies harder | 更彻底地检测复制 |
-D, --irreversible-delete |
Delete / irreversible delete | 删除文件时省略差异内容 |
-l <n> |
limit(限制) | 限制重命名/复制检测的文件数 |
--diff-algorithm=<algo> |
diff algorithm | 差异算法(patience, minimal, histogram, myers) |
--patience |
patience | 使用 patience 算法 |
--histogram |
histogram | 使用 histogram 算法 |
--minimal |
minimal | 使用 minimal 算法 |
--anchored=<text> |
anchored | 锚定差异 |
--no-renames |
no renames | 不检测重命名 |
--rename-empty |
rename empty | 空文件参与重命名检测 |
--no-rename-empty |
no rename empty | 空文件不参与 |
--ws-error-highlight=<kind> |
whitespace error highlight | 高亮空白错误(context, old, new, all, none) |
--ext-diff |
external diff | 使用外部差异工具 |
--no-ext-diff |
no external diff | 不使用外部差异工具 |
--textconv |
text convert(文本转换) | 使用 textconv 过滤器 |
--no-textconv |
no text convert | 不使用 textconv |
--ignore-submodules[=<when>] |
ignore submodules | 忽略子模块变更 |
--submodule[=<format>] |
submodule | 子模块差异格式(short, log, diff) |
--src-prefix=<prefix> |
src prefix(源前缀) | 设置源文件前缀(默认 a/) |
--dst-prefix=<prefix> |
dst prefix(目标前缀) | 设置目标文件前缀(默认 b/) |
--no-prefix |
no prefix | 不显示前缀 |
--line-prefix=<prefix> |
line prefix | 每行添加前缀 |
--ita-invisible-in-index |
intent to add invisible in index | 将 intent-to-add 条目视为不在索引中 |
示例:
# 查看工作区与暂存区的差异
git diff
# 查看暂存区与最近提交的差异
git diff --staged
# 比较两个分支
git diff main..develop
# 比较两个提交
git diff abc1234..def5678
# 只看文件名
git diff --name-only
# 查看某个文件的差异
git diff -- file.txt
# 忽略空白变化
git diff -w
# 查看统计信息
git diff --stat
# 使用 patience 算法
git diff --patience
# 按单词显示差异
git diff --word-diff
git show — 显示对象显示提交、标签、树等 Git 对象的详细信息。
格式:
git show [options] [<object>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--stat |
statistics(统计) | 只显示统计信息 |
--name-only |
name only | 只显示文件名 |
--name-status |
name status | 显示文件名和状态 |
--pretty=<format> |
pretty(美化) | 自定义输出格式 |
--format=<format> |
format(格式) | 同 --pretty |
--abbrev-commit |
abbreviate commit | 缩短 hash |
-s, --no-patch |
suppress / no patch | 不显示差异 |
--show-signature |
show signature | 显示签名信息 |
--show-notes[=<ref>] |
show notes | 显示注释 |
--no-notes |
no notes | 不显示注释 |
--date=<format> |
date(日期) | 日期格式 |
--expand-tabs[=<n>] |
expand tabs | 展开 tab 为空格 |
--no-expand-tabs |
no expand tabs | 不展开 tab |
(继承 git diff 的大部分参数) |
示例:
# 查看最近一次提交
git show
# 查看指定提交
git show abc1234
# 查看某个提交中的某个文件
git show abc1234:file.txt
# 查看标签信息
git show v1.0.0
# 只看统计信息
git show --stat
# 不显示差异
git show -s abc1234
git blame — 逐行追溯逐行显示文件的最后修改信息。
格式:
git blame [options] [<rev>] [--] <file>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-L <start>,<end> |
Line(行) | 只显示指定行范围 |
-L :<funcname> |
Line function | 只显示指定函数 |
-e, --show-email |
email / show email | 显示作者邮箱 |
-w |
whitespace(空白) | 忽略空白变化 |
-M[<num>] |
Move(移动) | 检测文件内的行移动 |
-C[<num>] |
Copy(复制) | 检测跨文件的行复制/移动 |
--since=<date> |
since(自从) | 只显示指定日期之后的修改 |
-l |
long(长) | 显示完整 hash |
-t |
timestamp(时间戳) | 显示原始时间戳 |
-s |
suppress(抑制) | 不显示作者名和时间戳 |
-f, --show-name |
file / show name | 显示文件名 |
-n, --show-number |
number / show number | 显示原始行号 |
-p, --porcelain |
porcelain(瓷器/高层) | 机器可读格式 |
--line-porcelain |
line porcelain | 每行都输出完整信息 |
--root |
root(根) | 不将根提交视为边界 |
--show-stats |
show stats | 显示统计信息 |
--progress |
progress(进度) | 显示进度 |
--score-debug |
score debug | 显示移动检测分数 |
--reverse <rev>..<rev> |
reverse(反转) | 反向追溯 |
--first-parent |
first parent | 只跟踪第一个父提交 |
--incremental |
incremental(增量) | 增量输出 |
--encoding=<encoding> |
encoding(编码) | 指定输出编码 |
--contents <file> |
contents(内容) | 使用指定文件内容 |
--date <format> |
date(日期) | 日期格式 |
--color-lines |
color lines | 彩色显示行 |
--color-by-age |
color by age | 按修改时间着色 |
--abbrev=<n> |
abbreviate(缩写) | hash 缩写长度 |
示例:
# 查看文件每行的修改信息
git blame file.txt
# 查看指定行范围
git blame -L 10,20 file.txt
# 显示邮箱
git blame -e file.txt
# 忽略空白变化
git blame -w file.txt
# 检测代码移动和复制
git blame -M -C file.txt
# 查看指定函数
git blame -L :myFunction file.txt
git shortlog — 汇总日志按作者汇总提交日志。
格式:
git shortlog [options] [<revision-range>] [-- <path>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-s, --summary |
summary(摘要) | 只显示提交数量 |
-n, --numbered |
numbered(编号) | 按提交数量排序 |
-e, --email |
email(邮箱) | 显示邮箱 |
-w[<width>[,<indent1>[,<indent2>]]] |
width(宽度) | 设置输出宽度和缩进 |
-c, --committer |
committer(提交者) | 按提交者分组(而非作者) |
--group=<type> |
group(分组) | 分组方式(author, committer, trailer:<field>) |
--format=<format> |
format(格式) | 自定义每条提交的格式 |
--date=<format> |
date(日期) | 日期格式 |
示例:
# 按作者汇总
git shortlog
# 按提交数量排序
git shortlog -sn
# 显示邮箱
git shortlog -sne
# 按 trailer 分组
git shortlog --group=trailer:co-authored-by
git describe — 描述提交用最近的标签来描述当前提交。
格式:
git describe [options] [<commit-ish>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--tags |
tags(标签) | 使用所有标签(不仅是附注标签) |
--all |
all(全部) | 使用所有引用 |
--long |
long(长) | 始终使用长格式 |
--first-parent |
first parent | 只跟踪第一个父提交 |
--abbrev=<n> |
abbreviate(缩写) | hash 缩写长度 |
--exact-match |
exact match(精确匹配) | 只输出精确匹配的标签 |
--candidates=<n> |
candidates(候选) | 考虑的候选标签数量 |
--match <pattern> |
match(匹配) | 只考虑匹配的标签 |
--exclude <pattern> |
exclude(排除) | 排除匹配的标签 |
--always |
always(总是) | 找不到标签时显示缩短的 hash |
--dirty[=<mark>] |
dirty(脏) | 工作区有修改时添加标记 |
--broken[=<mark>] |
broken(损坏) | HEAD 无效时添加标记 |
--contains |
contains(包含) | 查找包含指定提交的标签 |
示例:
# 描述当前提交
git describe
# 使用所有标签
git describe --tags
# 始终输出(即使没有标签)
git describe --always
# 检查是否有未提交的修改
git describe --dirty
git reset — 重置重置当前 HEAD 到指定状态。
格式:
git reset [<mode>] [<commit>]
git reset [options] [--] <pathspec>...
模式参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--soft |
soft(软) | 重置 HEAD,保留暂存区和工作区 |
--mixed |
mixed(混合) | 重置 HEAD 和暂存区,保留工作区(默认) |
--hard |
hard(硬) | 重置 HEAD、暂存区和工作区(丢弃所有修改) |
--merge |
merge(合并) | 重置但保留工作区中未暂存的修改 |
--keep |
keep(保留) | 重置但保留工作区修改(如有冲突则中止) |
其他参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-q, --quiet |
quiet(安静) | 减少输出 |
-p, --patch |
patch(补丁) | 交互式选择要重置的内容 |
-N, --intent-to-add |
New / intent to add | 重置后标记为 intent-to-add |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
--recurse-submodules[=<reset>] |
recurse submodules | 递归重置子模块 |
--no-recurse-submodules |
no recurse submodules | 不递归重置子模块 |
示例:
# 取消暂存(保留修改)
git reset HEAD file.txt
# 回退到上一个提交(保留修改在暂存区)
git reset --soft HEAD~1
# 回退到上一个提交(保留修改但取消暂存)
git reset HEAD~1
# 回退到上一个提交(丢弃所有修改,谨慎使用)
git reset --hard HEAD~1
# 回退到指定提交
git reset --hard abc1234
# 交互式重置
git reset -p
git revert — 撤销提交创建一个新提交来撤销指定提交的更改(安全的撤销方式)。
格式:
git revert [options] <commit>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n, --no-commit |
no commit(不提交) | 撤销但不自动提交 |
-e, --edit |
edit(编辑) | 编辑提交信息(默认) |
--no-edit |
no edit | 使用默认提交信息 |
-m <parent-number>, --mainline <parent-number> |
mainline(主线) | 撤销合并提交时指定主线 |
--abort |
abort(中止) | 中止 revert 操作 |
--continue |
continue(继续) | 解决冲突后继续 |
--skip |
skip(跳过) | 跳过当前提交 |
--quit |
quit(退出) | 退出但保留当前状态 |
-s, --signoff |
signoff(签署) | 添加 Signed-off-by |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名 |
--no-gpg-sign |
no GPG sign | 不签名 |
--strategy=<strategy> |
strategy(策略) | 指定合并策略 |
-X <option> |
eXtra option | 传递合并策略选项 |
--rerere-autoupdate |
rerere auto update | 允许 rerere 自动更新 |
--no-rerere-autoupdate |
no rerere auto update | 不允许 |
--reference |
reference(引用) | 在提交信息中使用引用格式 |
示例:
# 撤销指定提交
git revert abc1234
# 撤销但不自动提交
git revert --no-commit abc1234
# 撤销合并提交(保留主线 1)
git revert -m 1 abc1234
# 撤销多个提交
git revert abc1234..def5678
# 中止 revert
git revert --abort
git reflog — 引用日志查看 HEAD 和分支引用的变更历史(可用于恢复误操作)。
格式:
git reflog [show] [options] [<ref>]
git reflog expire [options]
git reflog delete [options] <ref>@{<specifier>}
show 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n <number> |
number(数量) | 限制显示数量 |
--date=<format> |
date(日期) | 日期格式 |
--all |
all(全部) | 显示所有引用的 reflog |
(继承 git log 的大部分显示参数) |
expire 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--expire=<time> |
expire(过期) | 过期时间(默认 90 天) |
--expire-unreachable=<time> |
expire unreachable | 不可达条目的过期时间(默认 30 天) |
--all |
all(全部) | 处理所有引用 |
--stale-fix |
stale fix(修复过期) | 修复过期条目 |
-n, --dry-run |
no action / dry run | 模拟运行 |
--rewrite |
rewrite(重写) | 重写 reflog |
--updateref |
update ref | 更新引用 |
--verbose |
verbose(详细) | 详细输出 |
示例:
# 查看 HEAD 的引用日志
git reflog
# 查看指定分支的引用日志
git reflog show main
# 限制显示数量
git reflog -10
# 恢复误删的提交
git reflog # 找到目标 commit hash
git reset --hard HEAD@{2} # 恢复到指定状态
# 清理过期的 reflog
git reflog expire --expire=30.days --all
git clean — 清理未跟踪文件删除工作区中未跟踪的文件。
格式:
git clean [options] [--] [<pathspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-f, --force |
force(强制) | 强制删除(必须指定,除非配置了 clean.requireForce=false) |
-d |
directory(目录) | 同时删除未跟踪的目录 |
-n, --dry-run |
no action / dry run | 模拟运行,显示将被删除的文件 |
-x |
exclude off(关闭排除) | 同时删除被 .gitignore 忽略的文件 |
-X |
eXclude only(仅排除) | 只删除被 .gitignore 忽略的文件 |
-i, --interactive |
interactive(交互) | 交互模式 |
-e <pattern>, --exclude=<pattern> |
exclude(排除) | 排除匹配的文件 |
-q, --quiet |
quiet(安静) | 减少输出 |
示例:
# 预览将被删除的文件
git clean -dn
# 删除未跟踪的文件和目录
git clean -df
# 删除所有未跟踪文件(包括被忽略的)
git clean -dfx
# 只删除被忽略的文件
git clean -dfX
# 交互式删除
git clean -di
# 排除某些文件
git clean -df -e "*.config"
git stash — 临时保存临时保存工作区和暂存区的修改。
格式:
git stash [push] [options] [--] [<pathspec>...]
git stash <subcommand> [options]
子命令:
| 子命令 | 含义 |
|---|---|
push |
保存修改(默认) |
pop [<stash>] |
恢复最近的 stash 并删除 |
apply [<stash>] |
恢复最近的 stash 但不删除 |
list |
列出所有 stash |
drop [<stash>] |
删除指定 stash |
clear |
清除所有 stash |
show [<stash>] |
显示 stash 的内容 |
branch <name> [<stash>] |
从 stash 创建新分支 |
create |
创建 stash 对象但不存储引用 |
store |
存储已创建的 stash 对象 |
push 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-m <message>, --message <message> |
message(消息) | 指定 stash 描述信息 |
-u, --include-untracked |
untracked / include untracked | 包含未跟踪的文件 |
-a, --all |
all(全部) | 包含所有文件(含忽略的) |
-k, --keep-index |
keep index(保留索引) | 保留暂存区的内容 |
--no-keep-index |
no keep index | 不保留暂存区 |
-p, --patch |
patch(补丁) | 交互式选择要暂存的内容 |
-S, --staged |
Staged(暂存区) | 只 stash 暂存区的内容 |
-q, --quiet |
quiet(安静) | 减少输出 |
--pathspec-from-file=<file> |
pathspec from file | 从文件读取路径规范 |
show 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-p, --patch |
patch(补丁) | 显示差异 |
--stat |
statistics(统计) | 显示统计 |
-u, --include-untracked |
untracked | 包含未跟踪文件的差异 |
--only-untracked |
only untracked | 只显示未跟踪文件 |
(继承 git diff 的大部分参数) |
pop/apply 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--index |
index(索引) | 同时恢复暂存区状态 |
-q, --quiet |
quiet(安静) | 减少输出 |
示例:
# 保存当前修改
git stash
# 保存并添加描述
git stash push -m "work in progress: login feature"
# 包含未跟踪文件
git stash -u
# 只 stash 暂存区的内容
git stash push --staged
# 查看所有 stash
git stash list
# 恢复最近的 stash
git stash pop
# 恢复指定的 stash
git stash apply stash@{2}
# 查看 stash 内容
git stash show -p stash@{0}
# 删除指定 stash
git stash drop stash@{1}
# 清除所有 stash
git stash clear
# 从 stash 创建分支
git stash branch new-branch stash@{0}
# 交互式选择要 stash 的内容
git stash push -p
git tag — 标签管理标签(通常用于版本发布标记)。
格式:
git tag [options] [<tagname>] [<commit>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-a |
annotated(附注) | 创建附注标签 |
-m <msg>, --message=<msg> |
message(消息) | 指定标签信息 |
-F <file>, --file=<file> |
File(文件) | 从文件读取标签信息 |
-d, --delete |
delete(删除) | 删除标签 |
-l, --list |
list(列表) | 列出标签(可配合模式匹配) |
-n[<num>] |
number(行数) | 显示标签信息的行数 |
-f, --force |
force(强制) | 强制覆盖已有标签 |
-s, --sign |
sign(签名) | GPG 签名标签 |
-u <keyid>, --local-user=<keyid> |
user / local user | 指定 GPG 密钥 |
-v, --verify |
verify(验证) | 验证标签签名 |
--sort=<key> |
sort(排序) | 排序方式(如 -v:refname 按版本号排序) |
--contains <commit> |
contains(包含) | 列出包含指定提交的标签 |
--no-contains <commit> |
no contains | 列出不包含指定提交的标签 |
--merged <commit> |
merged(已合并) | 列出已合并到指定提交的标签 |
--no-merged <commit> |
no merged | 列出未合并的标签 |
--points-at <object> |
points at(指向) | 列出指向指定对象的标签 |
--format=<format> |
format(格式) | 自定义输出格式 |
--color[=<when>] |
color(颜色) | 彩色输出 |
-i, --ignore-case |
ignore case(忽略大小写) | 忽略大小写 |
--column[=<options>] |
column(列) | 以列格式显示 |
--no-column |
no column | 不以列格式显示 |
--create-reflog |
create reflog | 创建标签的引用日志 |
-e, --edit |
edit(编辑) | 编辑标签信息 |
--cleanup=<mode> |
cleanup(清理) | 标签信息清理模式 |
示例:
# 列出所有标签
git tag
# 列出匹配的标签
git tag -l "v1.*"
# 创建轻量标签
git tag v1.0.0
# 创建附注标签
git tag -a v1.0.0 -m "Release version 1.0.0"
# 给历史提交打标签
git tag -a v0.9.0 abc1234 -m "Beta release"
# 删除本地标签
git tag -d v1.0.0
# 删除远程标签
git push origin --delete v1.0.0
# 推送标签到远程
git push origin v1.0.0
git push origin --tags
# 按版本号排序
git tag --sort=-v:refname
# 验证签名标签
git tag -v v1.0.0
git submodule — 子模块管理子模块(在一个 Git 仓库中嵌套另一个仓库)。
格式:
git submodule [<command>] [options]
子命令:
| 子命令 | 含义 |
|---|---|
add [options] <repository> [<path>] |
添加子模块 |
init [<path>...] |
初始化子模块配置 |
update [options] [<path>...] |
更新子模块 |
status [options] [<path>...] |
查看子模块状态 |
deinit [options] <path>... |
取消初始化子模块 |
sync [options] [<path>...] |
同步子模块 URL |
foreach [options] <command> |
对每个子模块执行命令 |
summary [options] [<path>...] |
显示子模块变更摘要 |
absorbgitdirs |
将子模块的 .git 目录移入父仓库 |
set-branch [options] <path> |
设置子模块的默认远程分支 |
set-url <path> <newurl> |
设置子模块的 URL |
add 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-b <branch>, --branch <branch> |
branch(分支) | 指定跟踪的分支 |
-f, --force |
force(强制) | 强制添加 |
--name <name> |
name(名称) | 指定子模块名称 |
--reference <repository> |
reference(引用) | 使用引用仓库 |
--depth <depth> |
depth(深度) | 浅克隆深度 |
update 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--init |
init(初始化) | 同时初始化未初始化的子模块 |
--recursive |
recursive(递归) | 递归更新嵌套子模块 |
--remote |
remote(远程) | 更新到远程最新提交 |
--merge |
merge(合并) | 合并子模块更新 |
--rebase |
rebase(变基) | 变基子模块更新 |
--checkout |
checkout(检出) | 检出子模块(默认) |
-N, --no-fetch |
No fetch(不获取) | 不 fetch 远程 |
-f, --force |
force(强制) | 强制更新 |
--depth <depth> |
depth(深度) | 浅克隆深度 |
-j <n>, --jobs <n> |
jobs(并行任务数) | 并行更新数量 |
--single-branch |
single branch | 只克隆单个分支 |
--no-single-branch |
no single branch | 克隆所有分支 |
--filter=<filter-spec> |
filter(过滤) | 部分克隆过滤器 |
foreach 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--recursive |
recursive(递归) | 递归执行 |
deinit 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-f, --force |
force(强制) | 强制取消初始化 |
--all |
all(全部) | 取消所有子模块 |
status 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--cached |
cached(缓存) | 使用索引中的提交 |
--recursive |
recursive(递归) | 递归显示 |
summary 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--cached |
cached(缓存) | 使用索引中的提交 |
--files |
files(文件) | 比较子模块 HEAD 与索引 |
-n <n>, --summary-limit <n> |
number / summary limit | 限制显示数量 |
set-branch 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-b <branch>, --branch <branch> |
branch(分支) | 设置分支 |
-d, --default |
default(默认) | 移除分支设置 |
示例:
# 添加子模块
git submodule add https://github.com/user/lib.git Libraries/lib
# 克隆后初始化并更新子模块
git submodule update --init --recursive
# 更新子模块到远程最新
git submodule update --remote
# 查看子模块状态
git submodule status
# 对所有子模块执行命令
git submodule foreach git pull origin main
# 对所有子模块递归执行
git submodule foreach --recursive git fetch
# 删除子模块
git submodule deinit Libraries/lib
git rm Libraries/lib
# 设置子模块跟踪分支
git submodule set-branch -b main Libraries/lib
git subtree — 子树将另一个仓库作为子目录合并(不需要 .gitmodules)。
格式:
git subtree <command> [options] <prefix>
子命令:
| 子命令 | 含义 |
|---|---|
add <prefix> <commit> |
添加子树 |
pull <prefix> <repository> <ref> |
拉取子树更新 |
push <prefix> <repository> <ref> |
推送子树更改 |
merge <prefix> <commit> |
合并子树 |
split <prefix> |
将子目录拆分为独立历史 |
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--squash |
squash(压缩) | 压缩合并 |
-P <prefix>, --prefix=<prefix> |
Prefix(前缀) | 指定子目录前缀 |
-m <message> |
message(消息) | 指定提交信息 |
--rejoin |
rejoin(重新加入) | split 后合并回主历史 |
--ignore-joins |
ignore joins | 忽略之前的 rejoin |
--onto=<onto> |
onto | 指定 split 的基底 |
-b <branch>, --branch=<branch> |
branch(分支) | split 时指定分支 |
--annotate=<annotation> |
annotate(注释) | split 时添加提交信息前缀 |
示例:
# 添加子树
git subtree add --prefix=lib https://github.com/user/lib.git main --squash
# 拉取子树更新
git subtree pull --prefix=lib https://github.com/user/lib.git main --squash
# 推送子树更改
git subtree push --prefix=lib https://github.com/user/lib.git main
# 拆分子目录
git subtree split --prefix=lib -b lib-branch
git grep — 搜索文件内容在 Git 跟踪的文件中搜索文本。
格式:
git grep [options] <pattern> [<tree>] [-- <pathspec>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n, --line-number |
number / line number(行号) | 显示行号 |
-c, --count |
count(计数) | 只显示每个文件的匹配数量 |
-i, --ignore-case |
ignore case(忽略大小写) | 忽略大小写 |
-w, --word-regexp |
word regexp(全词正则) | 全词匹配 |
-v, --invert-match |
invert match(反转匹配) | 反转匹配(显示不匹配的行) |
-h |
hide filename(隐藏文件名) | 不显示文件名 |
-H |
同 --show-filename |
显示文件名(默认) |
-l, --files-with-matches, --name-only |
list / files with matches | 只显示包含匹配的文件名 |
-L, --files-without-match |
List without / files without match | 显示不包含匹配的文件名 |
-e <pattern> |
expression(表达式) | 指定搜索模式 |
--and |
and(与) | 多条件与 |
--or |
or(或) | 多条件或 |
--not |
not(非) | 条件取反 |
-p, --show-function |
parent function / show function | 显示匹配所在的函数 |
-W, --function-context |
Whole function / function context | 显示整个函数上下文 |
-E, --extended-regexp |
Extended regexp(扩展正则) | 使用扩展正则 |
-G, --basic-regexp |
Grep basic(基本正则) | 使用基本正则(默认) |
-P, --perl-regexp |
Perl regexp(Perl 正则) | 使用 Perl 正则 |
-F, --fixed-strings |
Fixed strings(固定字符串) | 按固定字符串匹配 |
-f <file> |
file(文件) | 从文件读取搜索模式 |
--threads <n> |
threads(线程) | 使用的线程数 |
--max-depth <n> |
max depth(最大深度) | 目录搜索最大深度 |
-z, --null |
zero / null | 用 NUL 分隔输出 |
-o, --only-matching |
only matching(仅匹配) | 只显示匹配的部分 |
--break |
break(断开) | 文件之间添加空行 |
--heading |
heading(标题) | 文件名作为标题显示 |
--color[=<when>] |
color(颜色) | 彩色输出 |
-A <n>, --after-context <n> |
After context(后上下文) | 显示匹配后 n 行 |
-B <n>, --before-context <n> |
Before context(前上下文) | 显示匹配前 n 行 |
-C <n>, --context <n> |
Context(上下文) | 显示匹配前后 n 行 |
--recurse-submodules |
recurse submodules | 递归搜索子模块 |
--untracked |
untracked(未跟踪) | 搜索未跟踪的文件 |
--no-index |
no index | 搜索当前目录(不需要 Git 仓库) |
--cached |
cached(缓存) | 搜索索引中的文件 |
--all-match |
all match | 文件必须匹配所有模式 |
-q, --quiet |
quiet(安静) | 不输出,只返回退出码 |
示例:
# 搜索字符串
git grep "TODO"
# 显示行号
git grep -n "function"
# 忽略大小写
git grep -i "error"
# 在指定提交中搜索
git grep "pattern" HEAD~5
# 只显示文件名
git grep -l "import"
# 多条件搜索
git grep -e "class" --and -e "public"
# 在指定文件类型中搜索
git grep "pattern" -- "*.cpp"
# 显示函数上下文
git grep -p "TODO"
# 显示匹配前后各 2 行
git grep -C 2 "error"
git bisect — 二分查找使用二分查找定位引入 bug 的提交。
格式:
git bisect <subcommand> [options]
子命令:
| 子命令 | 含义 |
|---|---|
start [<bad> [<good>...]] [--] |
开始二分查找 |
bad [<rev>] |
标记当前或指定提交为有 bug |
new [<rev>] |
同 bad(语义更中性) |
good [<rev>] |
标记当前或指定提交为正常 |
old [<rev>] |
同 good(语义更中性) |
reset [<commit>] |
结束二分查找 |
skip [<rev>...] |
跳过当前或指定提交 |
log |
查看二分查找日志 |
replay <logfile> |
重放二分查找日志 |
run <command> |
自动化二分查找 |
visualize / view |
可视化当前范围 |
terms |
查看当前使用的术语 |
start 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--term-old=<term> |
term old | 自定义"好"的术语 |
--term-new=<term> |
term new | 自定义"坏"的术语 |
--no-checkout |
no checkout | 不检出,只更新 BISECT_HEAD |
--first-parent |
first parent | 只跟踪第一个父提交 |
示例:
# 开始二分查找
git bisect start
# 标记当前版本有 bug
git bisect bad
# 标记已知正常的版本
git bisect good v1.0.0
# Git 会自动切换到中间提交,测试后标记
git bisect good # 或 git bisect bad
# 结束查找
git bisect reset
# 自动化查找(用脚本测试)
git bisect start HEAD v1.0.0
git bisect run npm test
# 快速开始
git bisect start HEAD v1.0.0 -- src/
# 查看日志
git bisect log
git format-patch — 生成补丁将提交生成为邮件格式的补丁文件。
格式:
git format-patch [options] <since>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-<n> |
number(数量) | 生成最近 n 个提交的补丁 |
-o <dir>, --output-directory <dir> |
output(输出) | 指定输出目录 |
--stdout |
stdout(标准输出) | 输出到标准输出 |
--cover-letter |
cover letter(封面信) | 生成封面信 |
-s, --signoff |
signoff(签署) | 添加 Signed-off-by |
--subject-prefix=<prefix> |
subject prefix(主题前缀) | 设置主题前缀(默认 PATCH) |
-v <n>, --reroll-count=<n> |
version / reroll count | 版本号 |
--start-number <n> |
start number | 起始编号 |
--numbered |
numbered(编号) | 添加编号 |
--no-numbered |
no numbered | 不添加编号 |
--keep-subject |
keep subject | 保留原始主题 |
--rfc |
RFC | 使用 RFC 前缀 |
--attach[=<boundary>] |
attach(附件) | 作为附件 |
--inline[=<boundary>] |
inline(内联) | 内联附件 |
--thread[=<style>] |
thread(线程) | 邮件线程样式(shallow, deep) |
--no-thread |
no thread | 不使用线程 |
--in-reply-to=<msgid> |
in reply to | 回复指定邮件 |
--from[=<ident>] |
from(来自) | 指定发件人 |
--to=<email> |
to(收件人) | 指定收件人 |
--cc=<email> |
cc(抄送) | 指定抄送 |
--add-header=<header> |
add header | 添加邮件头 |
--base=<commit> |
base(基底) | 记录基底提交信息 |
--progress |
progress(进度) | 显示进度 |
--interdiff=<prev> |
interdiff(差异间差异) | 在封面信中包含与前一版本的差异 |
--range-diff=<prev> |
range diff | 在封面信中包含范围差异 |
--creation-factor=<n> |
creation factor | range-diff 的创建因子 |
--notes[=<ref>] |
notes(注释) | 包含注释 |
--no-notes |
no notes | 不包含注释 |
--signature=<sig> |
signature(签名) | 添加签名 |
--no-signature |
no signature | 不添加签名 |
--signature-file=<file> |
signature file | 从文件读取签名 |
--zero-commit |
zero commit | 使用全零 commit hash |
--no-binary |
no binary | 不包含二进制差异 |
(继承 git diff 的大部分参数) |
示例:
# 生成最近 3 个提交的补丁
git format-patch -3
# 生成某个提交之后的所有补丁
git format-patch abc1234
# 输出到指定目录
git format-patch -3 -o patches/
# 生成两个分支之间的补丁
git format-patch main..feature
# 带封面信
git format-patch --cover-letter -3
# 指定版本号
git format-patch -v2 -3
git apply — 应用补丁应用补丁文件到工作区。
格式:
git apply [options] [<patch>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--check |
check(检查) | 检查补丁是否可以应用 |
--stat |
statistics(统计) | 显示补丁统计信息 |
--numstat |
numeric stat | 数字统计 |
--summary |
summary(摘要) | 显示摘要 |
--index |
index(索引) | 同时更新索引 |
--cached |
cached(缓存) | 只更新索引 |
-R, --reverse |
Reverse(反转) | 反向应用补丁 |
-3, --3way |
3 way(三方) | 三方合并 |
--reject |
reject(拒绝) | 将无法应用的 hunk 保存为 .rej 文件 |
-p <n> |
path strip(路径剥离) | 剥离路径前缀的层数 |
-C <n> |
Context(上下文) | 确保至少 n 行上下文匹配 |
--unidiff-zero |
unidiff zero | 允许零行上下文 |
--apply |
apply(应用) | 实际应用补丁 |
-v, --verbose |
verbose(详细) | 详细输出 |
-q, --quiet |
quiet(安静) | 减少输出 |
--whitespace=<action> |
whitespace(空白) | 空白处理(nowarn, warn, fix, error, error-all) |
--directory=<root> |
directory(目录) | 指定应用目录 |
--include=<pattern> |
include(包含) | 只应用匹配的文件 |
--exclude=<pattern> |
exclude(排除) | 排除匹配的文件 |
--no-add |
no add | 忽略新增文件 |
--allow-binary-replacement |
allow binary replacement | 允许二进制替换 |
--binary |
binary(二进制) | 同 --allow-binary-replacement |
--unsafe-paths |
unsafe paths | 允许不安全的路径 |
示例:
# 应用补丁
git apply patch.patch
# 检查补丁是否可应用
git apply --check patch.patch
# 查看补丁统计
git apply --stat patch.patch
# 三方合并应用
git apply -3 patch.patch
# 反向应用
git apply -R patch.patch
git am — 应用邮件补丁应用邮件格式的补丁(由 format-patch 生成)。
格式:
git am [options] [<mbox>|<maildir>]...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--abort |
abort(中止) | 中止应用 |
--continue |
continue(继续) | 解决冲突后继续 |
--skip |
skip(跳过) | 跳过当前补丁 |
--quit |
quit(退出) | 退出但保留当前状态 |
-3, --3way |
3 way(三方) | 三方合并 |
-s, --signoff |
signoff(签署) | 添加 Signed-off-by |
-k, --keep |
keep(保留) | 保留原始主题 |
-c, --scissors |
cut / scissors(剪刀) | 使用剪刀线分割 |
--no-scissors |
no scissors | 不使用剪刀线 |
-q, --quiet |
quiet(安静) | 减少输出 |
-i, --interactive |
interactive(交互) | 交互模式 |
--directory=<dir> |
directory(目录) | 指定应用目录 |
--exclude=<pattern> |
exclude(排除) | 排除匹配的文件 |
--include=<pattern> |
include(包含) | 只包含匹配的文件 |
--patch-format=<format> |
patch format | 补丁格式(mbox, mboxrd, stgit, stgit-series, hg) |
-p <n> |
path strip(路径剥离) | 剥离路径前缀层数 |
-C <n> |
Context(上下文) | 上下文行数 |
--reject |
reject(拒绝) | 保存 .rej 文件 |
--whitespace=<action> |
whitespace(空白) | 空白处理 |
--ignore-space-change |
ignore space change | 忽略空白变化 |
--ignore-whitespace |
ignore whitespace | 忽略空白 |
-S[<keyid>], --gpg-sign[=<keyid>] |
Sign(签名) | GPG 签名 |
--committer-date-is-author-date |
committer date is author date | 使用作者日期 |
--ignore-date |
ignore date | 使用当前时间 |
--empty=<action> |
empty(空) | 处理空补丁(stop, drop, keep) |
--rerere-autoupdate |
rerere auto update | 允许 rerere 自动更新 |
示例:
# 应用补丁
git am patches/*.patch
# 三方合并应用
git am -3 patch.patch
# 中止应用
git am --abort
# 解决冲突后继续
git am --continue
git archive — 归档将 Git 仓库打包为归档文件。
格式:
git archive [options] <tree-ish> [<path>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--format=<fmt> |
format(格式) | 指定格式(tar, zip, tar.gz, tgz) |
--prefix=<prefix> |
prefix(前缀) | 添加路径前缀 |
-o <file>, --output=<file> |
output(输出) | 输出到文件 |
-l, --list |
list(列表) | 列出支持的格式 |
--worktree-attributes |
worktree attributes | 使用工作区的 .gitattributes |
--remote=<repo> |
remote(远程) | 从远程仓库归档 |
--exec=<path> |
execute(执行) | 指定远程 upload-archive 路径 |
-v, --verbose |
verbose(详细) | 详细输出 |
-0 到 -9 |
压缩级别 | 压缩级别(0=不压缩,9=最大压缩) |
示例:
# 打包为 tar.gz
git archive --format=tar.gz --prefix=project/ HEAD -o project.tar.gz
# 打包为 zip
git archive --format=zip HEAD -o project.zip
# 打包指定目录
git archive HEAD src/ -o src.tar
# 打包指定标签
git archive --prefix=v1.0/ v1.0.0 -o v1.0.tar.gz
git gc — 垃圾回收优化仓库,压缩文件历史。
格式:
git gc [options]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--aggressive |
aggressive(激进) | 更彻底的优化(耗时更长) |
--auto |
auto(自动) | 自动判断是否需要优化 |
--prune=<date> |
prune(修剪) | 清理指定日期之前的松散对象(默认 2 周) |
--no-prune |
no prune | 不清理松散对象 |
-q, --quiet |
quiet(安静) | 减少输出 |
--force |
force(强制) | 即使最近运行过也强制执行 |
--keep-largest-pack |
keep largest pack | 保留最大的包文件 |
--cruft |
cruft(碎片) | 将不可达对象打包为 cruft 包 |
--no-cruft |
no cruft | 不使用 cruft 包 |
示例:
# 基本垃圾回收
git gc
# 彻底优化
git gc --aggressive
# 自动优化
git gc --auto
# 清理所有松散对象
git gc --prune=now
git fsck — 完整性检查检查仓库对象的完整性和连通性。
格式:
git fsck [options] [<object>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--full |
full(完整) | 完整检查(检查所有对象目录) |
--unreachable |
unreachable(不可达) | 显示不可达对象 |
--dangling |
dangling(悬挂) | 显示悬挂对象(默认) |
--no-dangling |
no dangling | 不显示悬挂对象 |
--root |
root(根) | 显示根节点 |
--tags |
tags(标签) | 显示标签 |
--cache |
cache(缓存) | 检查索引中引用的对象 |
--no-reflogs |
no reflogs | 不考虑 reflog 中的引用 |
--connectivity-only |
connectivity only | 只检查连通性 |
--strict |
strict(严格) | 严格检查 |
--verbose |
verbose(详细) | 详细输出 |
--lost-found |
lost found(失物招领) | 将悬挂对象写入 .git/lost-found/ |
--name-objects |
name objects | 显示对象的可读名称 |
--progress |
progress(进度) | 显示进度 |
--no-progress |
no progress | 不显示进度 |
示例:
# 检查仓库完整性
git fsck
# 完整检查
git fsck --full
# 查找不可达对象
git fsck --unreachable
# 严格检查
git fsck --strict
# 恢复丢失的对象
git fsck --lost-found
git prune — 清理松散对象删除不可达的松散对象。
格式:
git prune [options] [--] [<head>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n, --dry-run |
no action / dry run | 模拟运行 |
-v, --verbose |
verbose(详细) | 详细输出 |
--progress |
progress(进度) | 显示进度 |
--expire <time> |
expire(过期) | 只清理指定时间之前的对象 |
示例:
# 清理不可达对象
git prune
# 模拟运行
git prune -n
# 清理所有松散对象
git prune --expire=now
git maintenance — 维护管理仓库的后台维护任务。
格式:
git maintenance <subcommand> [options]
子命令:
| 子命令 | 含义 |
|---|---|
run |
运行维护任务 |
start |
启动后台维护调度 |
stop |
停止后台维护调度 |
register |
注册当前仓库到维护列表 |
unregister |
从维护列表移除当前仓库 |
run 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--task=<task> |
task(任务) | 指定任务(gc, commit-graph, prefetch, loose-objects, incremental-repack, pack-refs) |
--auto |
auto(自动) | 自动判断是否需要 |
--schedule=<freq> |
schedule(调度) | 调度频率(hourly, daily, weekly) |
--quiet |
quiet(安静) | 减少输出 |
示例:
# 运行所有维护任务
git maintenance run
# 启动后台维护
git maintenance start
# 停止后台维护
git maintenance stop
# 运行指定任务
git maintenance run --task=gc
git rerere — 重用冲突解决记录和重用冲突解决方案。
格式:
git rerere [<subcommand>]
子命令:
| 子命令 | 含义 |
|---|---|
clear |
清除所有记录 |
forget <pathspec> |
忘记指定文件的记录 |
diff |
显示当前冲突的差异 |
status |
显示需要解决的文件 |
remaining |
显示尚未解决的文件 |
gc |
清理过期记录 |
示例:
# 启用 rerere
git config --global rerere.enabled true
# 查看状态
git rerere status
# 清除记录
git rerere clear
# 忘记指定文件
git rerere forget file.txt
git worktree — 多工作树管理多个工作树(允许同时检出多个分支到不同目录)。
格式:
git worktree <command> [options]
子命令与参数:
| 子命令 | 含义 |
|---|---|
add [options] <path> [<commit-ish>] |
添加新的工作树 |
list [options] |
列出所有工作树 |
remove [options] <worktree> |
删除工作树 |
prune [options] |
清理无效的工作树信息 |
lock [options] <worktree> |
锁定工作树(防止被清理) |
unlock <worktree> |
解锁工作树 |
move <worktree> <new-path> |
移动工作树 |
repair [<path>...] |
修复工作树链接 |
add 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-b <branch> |
branch(分支) | 创建新分支 |
-B <branch> |
Branch force(强制分支) | 创建或重置分支 |
-d, --detach |
detach(分离) | 分离 HEAD |
-f, --force |
force(强制) | 强制添加 |
--checkout |
checkout(检出) | 检出(默认) |
--no-checkout |
no checkout | 不检出 |
--lock |
lock(锁定) | 添加后锁定 |
--reason <string> |
reason(原因) | 锁定原因 |
-q, --quiet |
quiet(安静) | 减少输出 |
--track |
track(跟踪) | 设置上游跟踪 |
--no-track |
no track | 不设置跟踪 |
--guess-remote |
guess remote | 猜测远程分支 |
--no-guess-remote |
no guess remote | 不猜测 |
--orphan |
orphan(孤立) | 创建孤立分支 |
list 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--porcelain |
porcelain | 机器可读格式 |
-v, --verbose |
verbose(详细) | 详细输出 |
--expire <time> |
expire(过期) | 标记过期的工作树 |
-z |
zero(零) | NUL 分隔输出 |
remove 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-f, --force |
force(强制) | 强制删除 |
prune 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-n, --dry-run |
no action / dry run | 模拟运行 |
-v, --verbose |
verbose(详细) | 详细输出 |
--expire <time> |
expire(过期) | 过期时间 |
lock 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--reason <string> |
reason(原因) | 锁定原因 |
示例:
# 添加工作树
git worktree add ../hotfix hotfix-branch
# 添加并创建新分支
git worktree add -b new-feature ../feature
# 列出所有工作树
git worktree list
# 删除工作树
git worktree remove ../hotfix
# 清理无效工作树
git worktree prune
# 锁定工作树
git worktree lock --reason "long-running experiment" ../experiment
# 移动工作树
git worktree move ../old-path ../new-path
这些命令通常不直接使用,但在脚本和高级操作中很有用。
git cat-file — 查看对象显示 Git 对象的内容、类型或大小。
格式:
git cat-file [options] <object>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-t |
type(类型) | 显示对象类型 |
-s |
size(大小) | 显示对象大小 |
-p |
pretty print(美化打印) | 美化输出对象内容 |
-e |
exist(存在) | 检查对象是否存在 |
--batch |
batch(批量) | 批量模式 |
--batch-check |
batch check | 批量检查 |
--batch-all-objects |
batch all objects | 处理所有对象 |
--textconv |
text convert | 使用 textconv 过滤器 |
--filters |
filters(过滤器) | 使用过滤器 |
--follow-symlinks |
follow symlinks | 跟踪符号链接 |
示例:
# 查看对象类型
git cat-file -t abc1234
# 查看对象大小
git cat-file -s abc1234
# 查看对象内容
git cat-file -p abc1234
# 查看 HEAD 指向的树
git cat-file -p HEAD^{tree}
git hash-object — 计算对象哈希计算文件的 Git 对象哈希值。
格式:
git hash-object [options] [--] <file>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-t <type> |
type(类型) | 指定对象类型(默认 blob) |
-w |
write(写入) | 写入对象到数据库 |
--stdin |
stdin(标准输入) | 从标准输入读取 |
--stdin-paths |
stdin paths | 从标准输入读取文件路径 |
--no-filters |
no filters | 不使用过滤器 |
--literally |
literally(字面) | 允许任意类型 |
示例:
# 计算文件哈希
git hash-object file.txt
# 写入对象
git hash-object -w file.txt
# 从标准输入
echo "hello" | git hash-object --stdin
git rev-parse — 解析引用将各种 Git 引用解析为 SHA-1 哈希。
格式:
git rev-parse [options] <args>...
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--git-dir |
git dir(git 目录) | 显示 .git 目录路径 |
--git-common-dir |
git common dir | 显示共享 git 目录 |
--show-toplevel |
show toplevel(显示顶层) | 显示仓库根目录 |
--show-cdup |
show cdup | 显示到根目录的相对路径 |
--show-prefix |
show prefix | 显示当前目录相对于根的前缀 |
--show-superproject-working-tree |
show superproject working tree | 显示父项目工作树 |
--is-inside-git-dir |
is inside git dir | 是否在 .git 目录内 |
--is-inside-work-tree |
is inside work tree | 是否在工作树内 |
--is-bare-repository |
is bare repository | 是否是裸仓库 |
--is-shallow-repository |
is shallow repository | 是否是浅克隆 |
--abbrev-ref |
abbreviate reference | 缩写引用名 |
--short[=<n>] |
short(短) | 缩短 hash |
--symbolic |
symbolic(符号) | 显示符号引用 |
--symbolic-full-name |
symbolic full name | 显示完整符号引用 |
--verify |
verify(验证) | 验证引用 |
-q, --quiet |
quiet(安静) | 减少输出 |
--all |
all(全部) | 显示所有引用 |
--branches[=<pattern>] |
branches | 显示分支 |
--tags[=<pattern>] |
tags | 显示标签 |
--remotes[=<pattern>] |
remotes | 显示远程引用 |
--glob=<pattern> |
glob | 匹配引用模式 |
--exclude=<pattern> |
exclude | 排除引用模式 |
--parseopt |
parse options | 解析选项 |
--sq-quote |
sq quote(单引号引用) | Shell 引用 |
示例:
# 获取当前 HEAD 的 hash
git rev-parse HEAD
# 获取仓库根目录
git rev-parse --show-toplevel
# 获取 .git 目录路径
git rev-parse --git-dir
# 获取缩写的分支名
git rev-parse --abbrev-ref HEAD
# 验证引用
git rev-parse --verify main
# 检查是否在 git 仓库中
git rev-parse --is-inside-work-tree
git ls-tree — 列出树对象列出树对象的内容。
格式:
git ls-tree [options] <tree-ish> [<path>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-r |
recursive(递归) | 递归列出 |
-t |
tree(树) | 递归时也显示树对象 |
-d |
directory(目录) | 只显示树对象(目录) |
-l, --long |
long(长) | 显示对象大小 |
--name-only |
name only | 只显示名称 |
--name-status |
name status | 显示名称和状态 |
--full-name |
full name | 显示完整路径 |
--full-tree |
full tree | 从根开始显示 |
--abbrev[=<n>] |
abbreviate | 缩写 hash |
-z |
zero | NUL 分隔 |
--format=<format> |
format | 自定义格式 |
--object-only |
object only | 只显示对象 hash |
示例:
# 列出 HEAD 的文件
git ls-tree HEAD
# 递归列出
git ls-tree -r HEAD
# 只显示文件名
git ls-tree --name-only HEAD
# 显示大小
git ls-tree -l HEAD
git ls-files — 列出索引文件列出索引(暂存区)中的文件信息。
格式:
git ls-files [options] [<file>...]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-c, --cached |
cached(缓存) | 显示缓存的文件(默认) |
-d, --deleted |
deleted(已删除) | 显示已删除的文件 |
-m, --modified |
modified(已修改) | 显示已修改的文件 |
-o, --others |
others(其他) | 显示未跟踪的文件 |
-i, --ignored |
ignored(已忽略) | 显示被忽略的文件 |
-s, --stage |
stage(暂存) | 显示暂存信息 |
-u, --unmerged |
unmerged(未合并) | 显示未合并的文件 |
-k, --killed |
killed(被杀) | 显示需要删除的文件 |
-z |
zero | NUL 分隔 |
-t |
tag(标记) | 显示状态标记 |
-v |
verbose(详细) | 显示 assume-unchanged 位 |
--full-name |
full name | 显示完整路径 |
--recurse-submodules |
recurse submodules | 递归子模块 |
--abbrev[=<n>] |
abbreviate | 缩写 hash |
--debug |
debug(调试) | 显示调试信息 |
--eol |
eol(行尾) | 显示行尾信息 |
--error-unmatch |
error unmatch | 未匹配时报错 |
--with-tree=<tree> |
with tree | 使用指定树 |
--exclude=<pattern> |
exclude | 排除模式 |
-x <pattern> |
exclude | 同 --exclude |
--exclude-from=<file> |
exclude from | 从文件读取排除模式 |
-X <file> |
eXclude from | 同 --exclude-from |
--exclude-per-directory=<file> |
exclude per directory | 每个目录的排除文件 |
--exclude-standard |
exclude standard | 使用标准排除规则 |
--sparse |
sparse | 显示稀疏检出范围外的文件 |
--format=<format> |
format | 自定义格式 |
示例:
# 列出所有跟踪的文件
git ls-files
# 列出未跟踪的文件
git ls-files -o
# 列出被忽略的文件
git ls-files -i --exclude-standard
# 列出已修改的文件
git ls-files -m
# 列出未合并的文件
git ls-files -u
| 命令 | 说明 |
|---|---|
git update-index |
直接操作索引 |
git read-tree |
读取树对象到索引 |
git write-tree |
将索引写为树对象 |
git commit-tree |
从树对象创建提交 |
git update-ref |
更新引用 |
git symbolic-ref |
读取/设置符号引用 |
git for-each-ref |
遍历引用 |
git rev-list |
列出提交对象 |
git diff-tree |
比较两个树对象 |
git diff-index |
比较树与索引 |
git diff-files |
比较索引与工作区 |
git merge-base |
查找共同祖先 |
git merge-file |
三方合并单个文件 |
git merge-tree |
三方合并树对象 |
git mktag |
创建标签对象 |
git mktree |
从 ls-tree 格式创建树对象 |
git pack-objects |
创建包文件 |
git unpack-objects |
解包包文件 |
git pack-refs |
打包引用 |
git index-pack |
索引包文件 |
git verify-pack |
验证包文件 |
git count-objects |
统计松散对象 |
git check-ignore |
检查 .gitignore 规则 |
git check-attr |
检查 .gitattributes 属性 |
git check-mailmap |
检查 mailmap 映射 |
git check-ref-format |
检查引用名格式 |
git var |
显示 Git 逻辑变量 |
git stripspace |
清理空白 |
git credential |
凭证管理 |
git fmt-merge-msg |
生成合并提交信息 |
git interpret-trailers |
解析/添加 trailer |
git mailinfo |
从邮件提取补丁信息 |
git mailsplit |
分割邮箱文件 |
git column |
列格式化输出 |
git sparse-checkout — 稀疏检出只检出仓库的部分目录/文件。
格式:
git sparse-checkout <subcommand> [options]
子命令:
| 子命令 | 含义 |
|---|---|
init [--cone \| --no-cone] |
初始化稀疏检出 |
set <pattern>... |
设置稀疏检出模式 |
add <pattern>... |
添加稀疏检出模式 |
reapply |
重新应用稀疏检出规则 |
list |
列出当前模式 |
disable |
禁用稀疏检出 |
check-rules |
检查规则 |
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--cone |
cone(锥形) | 使用锥形模式(按目录匹配,更快) |
--no-cone |
no cone | 使用非锥形模式(按模式匹配) |
--sparse-index |
sparse index | 使用稀疏索引 |
--no-sparse-index |
no sparse index | 不使用稀疏索引 |
--stdin |
stdin | 从标准输入读取模式 |
示例:
# 初始化稀疏检出
git sparse-checkout init --cone
# 设置只检出指定目录
git sparse-checkout set src/ docs/
# 添加目录
git sparse-checkout add tests/
# 列出当前模式
git sparse-checkout list
# 禁用稀疏检出
git sparse-checkout disable
git bundle — 打包将 Git 对象打包为单个文件(用于离线传输)。
格式:
git bundle <subcommand> [options]
子命令:
| 子命令 | 含义 |
|---|---|
create <file> <git-rev-list-args> |
创建 bundle |
verify <file> |
验证 bundle |
list-heads <file> |
列出 bundle 中的引用 |
unbundle <file> |
解包 bundle |
create 参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--progress |
progress(进度) | 显示进度 |
--version=<n> |
version(版本) | bundle 格式版本 |
-q, --quiet |
quiet(安静) | 减少输出 |
--all |
all(全部) | 包含所有引用 |
示例:
# 创建包含所有历史的 bundle
git bundle create repo.bundle --all
# 创建增量 bundle
git bundle create update.bundle main ^origin/main
# 验证 bundle
git bundle verify repo.bundle
# 从 bundle 克隆
git clone repo.bundle my-repo
# 从 bundle 获取
git fetch repo.bundle main:other-main
git range-diff — 范围差异比较两个提交范围(常用于比较补丁系列的不同版本)。
格式:
git range-diff [options] <range1> <range2>
git range-diff [options] <rev1>...<rev2>
git range-diff [options] <base> <rev1> <rev2>
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--creation-factor=<n> |
creation factor | 创建因子(控制匹配灵敏度,默认 60) |
--no-dual-color |
no dual color | 不使用双色模式 |
--left-only |
left only | 只显示左侧范围 |
--right-only |
right only | 只显示右侧范围 |
(继承 git diff 的大部分参数) |
示例:
# 比较两个版本的补丁系列
git range-diff main..feature-v1 main..feature-v2
# 三参数形式
git range-diff main feature-v1 feature-v2
git request-pull — 生成拉取请求生成拉取请求的摘要信息。
格式:
git request-pull [options] <start> <url> [<end>]
参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
-p |
patch(补丁) | 包含补丁内容 |
示例:
# 生成拉取请求
git request-pull v1.0 https://github.com/user/repo.git main
git send-email — 发送邮件通过邮件发送补丁。
格式:
git send-email [options] <file|directory>...
常用参数:
| 参数 | 缩写含义 | 说明 |
|---|---|---|
--to=<address> |
to(收件人) | 收件人 |
--cc=<address> |
cc(抄送) | 抄送 |
--bcc=<address> |
bcc(密送) | 密送 |
--from=<address> |
from(发件人) | 发件人 |
--subject=<subject> |
subject(主题) | 主题 |
--in-reply-to=<msgid> |
in reply to | 回复指定邮件 |
--compose |
compose(撰写) | 撰写封面信 |
--annotate |
annotate(注释) | 发送前编辑 |
--dry-run |
dry run | 模拟运行 |
--confirm=<mode> |
confirm(确认) | 确认模式 |
--smtp-server=<host> |
SMTP server | SMTP 服务器 |
--smtp-server-port=<port> |
SMTP server port | SMTP 端口 |
--smtp-encryption=<enc> |
SMTP encryption | 加密方式(ssl, tls) |
--smtp-user=<user> |
SMTP user | SMTP 用户名 |
--smtp-pass=<pass> |
SMTP pass | SMTP 密码 |
--no-thread |
no thread | 不使用线程 |
--suppress-cc=<category> |
suppress cc | 抑制自动抄送 |
-v, --validate |
validate(验证) | 验证补丁 |
--no-validate |
no validate | 不验证 |
示例:
# 发送补丁
git send-email --to=maintainer@example.com patches/*.patch
# 模拟运行
git send-email --dry-run --to=test@example.com 0001-fix.patch
git lfs — 大文件存储Git Large File Storage,管理大文件(需要安装 git-lfs 扩展)。
格式:
git lfs <subcommand> [options]
常用子命令:
| 子命令 | 含义 |
|---|---|
install |
安装 Git LFS 钩子 |
track <pattern> |
跟踪大文件模式 |
untrack <pattern> |
取消跟踪 |
ls-files |
列出 LFS 跟踪的文件 |
status |
查看 LFS 状态 |
pull |
下载 LFS 文件 |
push |
上传 LFS 文件 |
fetch |
获取 LFS 文件 |
prune |
清理旧的 LFS 文件 |
migrate |
迁移历史中的大文件 |
env |
显示 LFS 环境信息 |
logs |
查看 LFS 日志 |
version |
显示版本 |
示例:
# 安装 LFS
git lfs install
# 跟踪大文件
git lfs track "*.psd"
git lfs track "*.zip"
# 查看跟踪的文件
git lfs ls-files
# 取消跟踪
git lfs untrack "*.psd"
# 迁移历史中的大文件
git lfs migrate import --include="*.psd"
# 编译输出
*.o
*.so
*.dylib
*.a
build/
bin/
out/
# IDE 文件
.vscode/
.idea/
*.xcworkspace
*.xcuserdata
*.swp
*.swo
*~
# 系统文件
.DS_Store
Thumbs.db
desktop.ini
# 依赖目录
node_modules/
vendor/
Pods/
# 日志和临时文件
*.log
*.tmp
*.bak
# 环境配置
.env
.env.local
.env.*.local
# 包文件
*.tar.gz
*.zip
*.rar
| 语法 | 含义 |
|---|---|
HEAD |
当前提交 |
HEAD~1 或 HEAD~ |
上一个提交 |
HEAD~n |
往前第 n 个提交 |
HEAD^ |
第一个父提交 |
HEAD^2 |
第二个父提交(合并提交) |
@{n} |
reflog 中第 n 个条目 |
@{upstream} 或 @{u} |
上游分支 |
@{push} |
推送目标分支 |
branch@{yesterday} |
分支昨天的状态 |
branch@{2.hours.ago} |
分支 2 小时前的状态 |
main..feature |
feature 有但 main 没有的提交 |
main...feature |
两个分支各自独有的提交 |
:/fix bug |
提交信息匹配 "fix bug" 的最近提交 |
<rev>:<path> |
指定提交中的文件 |
<rev>^{tree} |
提交对应的树对象 |
<rev>^{commit} |
解引用到提交对象 |
<rev>^{} |
解引用标签到底层对象 |
# 功能分支工作流
git switch -c feature/my-feature # 创建功能分支
# ... 开发 ...
git add -A # 暂存所有更改
git commit -m "feat: description" # 提交
git push -u origin feature/my-feature # 推送
# 创建 Pull Request / Merge Request
# 紧急修复
git switch -c hotfix/fix-crash main # 从 main 创建修复分支
# ... 修复 ...
git commit -am "fix: crash on startup"
git push -u origin hotfix/fix-crash
# 同步上游仓库(Fork 工作流)
git fetch upstream
git rebase upstream/main
git push --force-with-lease
# 撤销最近一次提交(保留修改)
git reset --soft HEAD~1
# 找回误删的分支
git reflog # 找到 commit hash
git switch -c recovered-branch abc1234
# 交互式变基整理提交
git rebase -i HEAD~5
# 暂存当前工作切换任务
git stash push -m "wip: current task"
git switch other-branch
# ... 处理其他事情 ...
git switch -
git stash pop