Git 命令参考:综合指南
本报告旨在为 Git 用户提供一份详尽且实用的参考资料,专为集成到 VitePress 文档站点而设计。报告深入阐述了各项 Git 命令,提供了多层次的观察和详细建议,旨在为 Git 的初学者和经验丰富的实践者提供全面的基础理解。
1. Git 设置与配置
本节涵盖了设置 Git 环境和配置用户特定及仓库特定设置的基本命令。正确的配置是实现高效且一致的 Git 工作流程的第一步。
git config
git config
命令是自定义 Git 行为的基础。它允许用户查询、设置、替换和取消设置各种选项,这些选项控制着从用户身份到差异算法和合并策略的一切。Git 从多个配置文件中按特定顺序读取配置:系统范围、全局(用户特定)和局部(仓库特定),其中后读取的文件会覆盖先前的设置。
核心语法
git config --list
git config --global <key> <value>
git config --local <key> <value>
git config --system <key> <value>
git config --edit [--global|--local|--system]
关键选项与变体
- 用户身份sh
git config --global user.name "[firstname lastname]" git config --global user.email "[valid-email]"
- 编辑器配置sh
git config --global core.editor "vim"
- 输出分页器sh
git config --global core.pager ""
- 别名sh
git config --global alias.st "status -sb" git config --global alias.cm "commit -m" git config --global alias.ll "log --oneline" git config --global alias.last "log -1 HEAD --stat" git config --global alias.se '!git rev-list --all | xargs git grep -F'
- 排除文件sh
git config --global core.excludesfile ~/.gitignore_global
- 签名密钥sh
git config --global user.signingkey <gpg-key-id>
- 显示来源/范围sh
git config --list --show-origin git config --list --show-scope
- 类型指定sh
git config --type=bool core.filemode true git config --type=path core.editor "C:/Program Files/Sublime Text/subl.exe --wait"
配置层次结构说明
Git 配置的层次结构及其覆盖机制,是理解其行为一致性的关键。局部设置会覆盖全局设置,全局设置又会覆盖系统设置。
别名的意义
别名不仅是快捷方式,还能标准化团队命令,降低认知负担,提升协作一致性。
git init
git init
命令用于创建新的空 Git 仓库或重新初始化现有仓库。
核心语法
git init
git init <project-name>
关键选项与变体
--bare
:创建裸仓库-b <branch-name>
/--initial-branch=<branch-name>
:指定初始分支--template=<template-directory>
:自定义模板--object-format=<format>
:指定哈希算法--ref-format=<format>
:指定引用格式--separate-git-dir=<git-dir>
:分离工作树与仓库数据--shared[=权限]
:多用户权限-q
/--quiet
:安静模式
git init -b
选项支持现代包容性分支命名。
git clone
git clone
命令用于复制现有 Git 仓库,包括其所有历史、分支和标签。
核心语法
git clone <repo-url>
git clone <repo-url> <directory>
关键选项与变体
--depth=<depth>
:浅克隆-b <name>
/--branch <name>
:指定分支-o <name>
/--origin <name>
:自定义远程名--bare
、--mirror
、--local
、--no-hardlinks
、--shared
、--reference
、--dissociate
--recurse-submodules
、--filter
、--sparse
、-c <key>=<value>
、--bundle-uri=<uri>
注意:
--shared
等本地克隆选项有潜在风险,可能导致仓库损坏。
2. 基本工作流程:暂存、提交与状态
git status
显示工作目录和暂存区的状态。
核心语法
git status
关键选项
-s
/--short
-b
/--branch
--porcelain[=<version>]
-u[<mode>]
/--untracked-files[=<mode>]
--ignore-submodules[=<when>]
--ignored[=<mode>]
-z
--show-stash
--ahead-behind
/--no-ahead-behind
状态码表
X (索引状态) | Y (工作树状态) | 描述 |
---|---|---|
' ' | ' ' | 无更改 |
M | ' ' | 已暂存(已修改) |
' ' | M | 已修改但未暂存 |
M | M | 已暂存且工作树也已修改 |
A | ' ' | 已暂存(新文件) |
D | ' ' | 已暂存(删除文件) |
R | ' ' | 已暂存(重命名) |
C | ' ' | 已暂存(复制) |
U | U | 未合并(合并冲突) |
?? | ?? | 未跟踪文件 |
!! | !! | 已忽略文件 |
git add
将更改暂存,为下一次提交做准备。
核心语法
git add <file>
git add .
关键选项
-p
/--patch
-u
/--update
-A
/--all
--dry-run
/-n
--force
git commit
将暂存的更改作为新的提交快照记录到本地仓库。
核心语法
git commit -m "[message]"
git commit
关键选项
-a
/--all
-p
/--patch
-F [file]
/--file=[file]
-C [commit]
/--reuse-message=[commit]
-c [commit]
/--reedit-message=[commit]
--amend
--allow-empty
--allow-empty-message
--author="[author]"
--date="[date]"
-s
/--signoff
-S
/--gpg-sign[=<key>]
-v
/--verbose
-q
/--quiet
--dry-run
--status
/--no-status
git rm
从工作目录和索引中删除文件。
核心语法
git rm <file>
关键选项
-r
-f
/--force
-n
/--dry-run
--cached
--ignore-unmatch
--
-q
/--quiet
git mv
移动或重命名文件、目录或符号链接,并自动暂存更改。
核心语法
git mv <source> <destination>
git mv <source>... <destination-directory>
关键选项
-f
/--force
-k
-n
/--dry-run
-v
/--verbose
3. 分支与合并
git branch
用于列出、创建、删除和重命名分支。
核心语法
git branch
git branch <branch-name>
git branch -d <branch-name>
git branch -D <branch-name>
git branch -m <old-name> <new-name>
git branch -r
git branch -a
git branch -vv
git branch -u <remote>/<branch>
git branch --unset-upstream
git branch --contains <commit>
git branch --no-contains <commit>
git branch --merged
git branch --no-merged
git checkout
切换分支或恢复文件。
核心语法
git checkout <branch-name>
git checkout -b <new-branch>
git checkout -
git checkout -- <file>
git checkout <commit-hash> -- <file>
git checkout --track <remote>/<branch>
git checkout --detach <commit-hash>
git checkout -f
git checkout -m
git checkout --conflict=<style>
git checkout --recurse-submodules
git switch
专注于切换分支。
核心语法
git switch <branch>
git switch -c <new-branch>
git switch -C <new-branch>
git switch -
git switch --detach <start-point>
git switch --guess / --no-guess
git switch --discard-changes / -f
git switch -m
git switch --conflict=<style>
git switch --track / --no-track
git switch --orphan <new-branch>
git switch --ignore-other-worktrees
git switch --recurse-submodules
git merge
将一个或多个分支的历史合并到当前分支。
核心语法
git merge <branch>
关键选项
--no-ff
--ff-only
--squash
--no-commit
--abort
--continue
-Xignore-space-change
-Xpatience
-Xours
/-Xtheirs
--allow-unrelated-histories
git rebase
通过将一系列提交从一个分支重新应用到另一个基础分支上来重写提交历史。
核心语法
git rebase <branch>
git rebase -i <branch>
交互式变基操作表
操作 | 描述 | 对历史的影响 | 常见用例 |
---|---|---|---|
pick | 按原样使用提交 | 保留提交 | 默认 |
reword | 编辑提交消息 | 保留提交,更改消息 | 修正消息 |
edit | 停止以修改内容 | 保留提交,允许更改 | 拆分/补充提交 |
squash | 合并到前一个提交,合并消息 | 合并提交 | 逻辑分组 |
fixup | 合并到前一个提交,丢弃消息 | 合并提交 | 小修补 |
drop | 删除提交 | 删除提交 | 移除不需要的更改 |
exec | 执行命令 | 执行命令 | 测试/lint |
4. 远程操作
git remote
管理远程仓库。
核心语法
git remote -v
git remote add <alias> <url>
git remote set-url <name> <new-url>
git remote set-url --push <name> <new-push-url>
git remote remove <name>
git remote rename <old-name> <new-name>
git remote show <name>
git remote prune <name>
git remote update [--prune]
git remote set-head <name> <branch>
git remote get-url [--push][--all] <name>
git remote set-url --add <name> <url>
git remote set-url --delete <name> <url>
git fetch
从远程仓库下载提交、文件和引用。
核心语法
git fetch <alias>
git fetch origin
git fetch <remote> <branch>
git fetch --all
关键选项
--dry-run
--tags
--no-tags
--prune
/-p
--recurse-submodules
--depth=<depth>
--unshallow
git pull
获取并合并远程更改。
核心语法
git pull
git pull origin <branch>
关键选项
--no-commit
--rebase
--ff-only
--no-ff
--autostash
--verbose
--recurse-submodules
git push
将本地内容上传到远程仓库。
核心语法
git push <alias> <branch>
关键选项
--force
--force-with-lease
--all
--tags
-u
/--set-upstream
--dry-run
--delete <remote> <branch>
--recurse-submodules=(check|on-demand)
5. 历史检查与分析
git log
显示提交历史。
核心语法
git log
关键选项
--oneline
--graph
--decorate
--all
-p
/--patch
--stat
--pretty=format:"..."
--abbrev-commit
--full-diff
--summary
--name-status
-<n>
/--max-count=<n>
--author="<pattern>"
--grep="<pattern>"
--since="<date>"
/--after="<date>"
--until="<date>"
/--before="<date>"
-- <file>
-S"<string>"
-G"<regex>"
<branchB>..<branchA>
--no-merges
--merges
--first-parent
--walk-reflogs
--relative-date
--diff-filter=
--find-copies[=<n>]
/--find-renames[=<n>]
git log 漂亮格式示例
格式字符串 | 描述 | 示例输出 |
---|---|---|
--oneline | 短提交哈希和主题 | a1b2c3d Fix: Bug in login flow |
--graph --oneline --decorate | 带分支图和引用 | * a1b2c3d (HEAD -> main, origin/main) Fix: Bug in login flow |
--pretty=format:"%h %an %ad %s" | 自定义格式 | a1b2c3d John Doe 2023-01-15 Fix: Bug in login flow |
--pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" | 彩色自定义格式 | a1b2c3d - (HEAD -> main, origin/main) Fix: Bug in login flow (2 days ago) <John Doe> |
--stat | 文件更改摘要 | a1b2c3d Fix: Bug in login flow file.py |
-p | 完整补丁 | (完整差异输出) |
git diff
显示仓库不同状态之间的差异。
核心语法
git diff
git diff --staged
git diff <commit1> <commit2>
git diff <branch1>..<branch2>
git diff <branch1>...<branch2>
关键选项
--color-words
--stat
--name-only
--binary
--check
--word-diff[=<mode>]
--diff-algorithm=(myers|minimal|patience|histogram)
--ignore-space-at-eol
--ignore-space-change
--ignore-all-space
--ignore-blank-lines
git show
以人类可读的格式显示 Git 对象信息。
核心语法
git show
git show <commit-hash>
git show <tag-name>
git show <branch-name>
git show --name-only
git show --stat
git show --pretty=format:"..."
git show <tree-ish>:<path/to/file>
git shortlog
以适合发布公告的格式汇总 git log 输出。
核心语法
git shortlog
关键选项
-n
/--numbered
-s
/--summary
-e
/--email
--format[=<format>]
--group=<type>
-w[<width>[,<indent1>[,<indent2>]]]
<revision-range>
--author=<pattern>
/--grep=<pattern>
--first-parent
git blame
注释文件中的每一行,显示最后修改该行的提交元数据。
核心语法
git blame <file>
关键选项
-L <start>,<end>
--date=(relative|iso|short|raw)
-C
或-M
-s
-w
--reverse
git describe
查找给定提交的最新可达标签。
核心语法
git describe [--all][--tags][--contains][--abbrev=<n>] <commit-ish>
关键选项
--all
--tags
--contains
--abbrev=<n>
--dirty[=<mark>]
--broken[=<mark>]
--long
--candidates=<n>
--exact-match
--match <pattern>
/--exclude <pattern>
--first-parent
--always