Git 命令速查

适用平台:Windows / macOS / Linux。覆盖安装后常用到的配置 / 新建与克隆 / 提交与撤销 / 分支与合并 / Rebase / 远端 / 标签 / 记录 / 暂存 / 子模块 / 工作树 / 稀疏检出 / 清理 / 排错等。


1. 基础配置(一次性/全局)

1
2
3
4
5
6
7
8
9
10
11
12
13
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main # 默认主分支名
git config --global core.autocrlf input # macOS/Linux 推荐(或 false)
git config --global core.autocrlf true # Windows 推荐
git config --global pull.rebase false # pull 默认合并;或 true/merges
git config --global fetch.prune true # 拉取时清理已删除远端分支
git config --global rerere.enabled true # 记忆冲突解决(提高效率)
git config --global core.editor "code --wait" # 你喜欢的编辑器
git config --global alias.st "status -sb" # 常用别名示例
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --list --show-origin # 查看配置来源

SSH 与签名(可选)

1
2
3
4
5
ssh-keygen -t ed25519 -C "you@example.com"          # 生成 SSH Key
# 将公钥添加到 Git 托管平台(GitHub/GitLab)

git config --global commit.gpgsign true # 提交签名(需安装 GPG)
git config --global user.signingkey <KEYID>

2. 新建 / 克隆仓库

1
2
3
4
5
git init                                             # 在当前目录初始化
git init myrepo # 新建目录并初始化
git clone <url> # 克隆(HTTP/SSH)
git clone --depth 1 <url> # 浅克隆
git clone -b develop <url> # 指定分支

3. 查看状态 / 差异 / 忽略

1
2
3
4
5
git status -sb                                       # 简洁状态
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs HEAD
git add -N <file> # 仅追踪,不加入内容(intent to add)
git check-ignore -v <path> # 排查 .gitignore 生效原因

.gitignore 快速示例

1
2
3
4
# 忽略所有 .log 和 temp/ 下内容
*.log
temp/
!temp/keep.me

4. 暂存与提交

1
2
3
4
5
6
git add <file>                                       # 加入暂存区
git add . # 全部变更
git restore --staged <file> # 从暂存区移除(不丢文件)
git commit -m "feat: message" # 普通提交
git commit --amend # 修改最近一次提交(不改内容则只改信息)
git commit --amend --no-edit # 仅重写父指针(合并暂存内容)

多行提交信息

1
git commit                                           # 进入编辑器,填写 subject/body

5. 撤销与回滚(工作区/暂存区/提交)

1
2
3
4
5
6
7
8
git restore <file>                                   # 丢弃工作区修改(对比暂存区)
git restore --source=HEAD --staged <file> # 丢弃暂存区到 HEAD
git reset --soft HEAD~1 # 回退 1 次提交到暂存(保留修改)
git reset --mixed HEAD~1 # 默认:回退到工作区(保留修改)
git reset --hard HEAD~1 # 丢弃这次提交及改动(危险)
git revert <commit> # 生成“反向提交”(公共分支安全)
git reflog # 查看 HEAD 历史移动记录(救命绳)
git restore -S <commit> # 将某提交的内容还原到工作区(2.23+)

删除误追踪的大文件

1
2
git rm --cached <path>                               # 停止追踪但保留文件
echo "<path>" >> .gitignore

6. 分支(创建/切换/删除/重命名)

1
2
3
4
5
6
7
git branch                                           # 查看本地分支
git branch -vv # 含上游跟踪信息
git switch -c feature/x # 新建并切换(2.23+)
git switch main # 切换
git branch -m oldname newname # 本地分支重命名
git branch -d feature/x # 删除(已合并)
git branch -D feature/x # 强删(未合并)

跟踪远端分支

1
2
3
git fetch                                            # 更新远端引用
git switch -c feature/x origin/feature/x # 新建跟踪分支
git branch -u origin/feature/x # 绑定上游

7. 合并与冲突解决

1
2
3
4
5
git merge feature/x                                  # 将 feature/x 合并到当前分支
git merge --no-ff feature/x # 保留合并提交
# 解决冲突文件后:
git add <conflicted-files>
git commit

图形化合并工具(示例:VS Code)

1
2
3
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"
git mergetool

8. Rebase(整理提交历史)

1
2
3
4
5
git rebase main                                      # 将当前分支基于 main 重新播放
git rebase -i HEAD~5 # 交互式压缩/重排/合并提交
git rebase --abort # 放弃 rebase
git rebase --continue # 解决冲突后继续
git pull --rebase # 拉取时避免额外合并提交

注意:不要对已经公开推送、他人基于其工作的提交做 rebase/改历史。

9. 远端(Remote)/ 拉取与推送

1
2
3
4
5
6
7
8
9
git remote -v                                        # 查看远端
git remote add origin <url>
git remote set-url origin <new-url> # 替换地址(HTTP<->SSH)
git fetch --all --prune # 拉取所有并清理已删除远端分支
git pull # 当前分支拉取并合并
git pull --rebase # 拉取并 rebase
git push -u origin main # 首次推送并建立上游
git push # 普通推送
git push --force-with-lease # 安全强推(避免覆盖他人)

10. Stash(临时保存工作现场)

1
2
3
4
5
6
7
git stash push -m "WIP: message"                     # 保存当前改动
git stash list # 列表
git stash show -p stash@{0} # 查看差异
git stash apply stash@{0} # 应用但保留记录
git stash pop # 应用并删除记录
git stash drop stash@{0} # 删除某条
git stash push -u # 包括未追踪文件

11. Tag(标签 / 版本)

1
2
3
4
5
6
git tag                                              # 列出标签
git tag v1.0.0 # 轻量标签
git tag -a v1.0.0 -m "release 1.0.0" # 附注标签(推荐)
git show v1.0.0
git push origin v1.0.0 # 推送单个标签
git push origin --tags # 推送全部标签

12. 日志与定位(log / blame / grep / shortlog)

1
2
3
4
5
6
git log --oneline --graph --decorate --all
git log --stat --patch # 带差异
git blame <file> # 查某行最后修改者
git grep -n "pattern" # 在版本库中搜索
git shortlog -sn --all # 统计作者贡献
git show <commit> # 查看提交内容/元数据

范围与选择器

1
2
3
git log <since>..<until>                             # 提交区间
git log -- <path> # 限定文件/目录
git diff <commit1> <commit2> -- <path>

13. Cherry-pick(挑拣提交)

1
2
3
git cherry-pick <commit>                             # 将该提交应用到当前分支
git cherry-pick <c1> <c2> # 多个
git cherry-pick --abort | --continue # 冲突处理

14. Submodule(子模块)

1
2
3
4
5
git submodule add <url> path/
git submodule update --init --recursive
git submodule sync --recursive # URL 同步
# 更新子模块到最新远端提交:
git submodule update --remote --merge

15. Worktree(多工作树并行开发)

1
2
3
git worktree list
git worktree add ../wt-feature feature/x # 在新目录检出分支
git worktree remove ../wt-feature

16. 稀疏检出(仅拉取需要的目录)

1
2
3
4
git sparse-checkout init --cone
git sparse-checkout set path/another-path/
git sparse-checkout add extras/
git sparse-checkout list

17. 清理与维护

1
2
3
git clean -fdX                                       # 强制删除未追踪+忽略文件(危险)
git clean -ndX # 先 dry-run 看看删什么
git gc --aggressive --prune=now # 垃圾回收(慎用在大型库)

18. 重写历史(谨慎)

1
2
3
4
5
6
7
8
# 修改作者信息(最近一次提交)
git commit --amend --author="New Name <new@example.com>"

# 全局重写作者/邮箱(使用 git filter-repo,需先安装)
# pip install git-filter-repo
git filter-repo --email-callback '
return b"new@example.com" if email==b"old@example.com" else email
'

如仓库已公开,重写历史需要强推并通知协作者重新同步。

19. 行结尾 / 大文件 / LFS / 属性

1
2
3
4
5
6
7
8
9
# 统一行结尾(推荐在仓库根创建 .gitattributes)
echo "* text=auto" >> .gitattributes

# Git LFS(Large File Storage)
git lfs install
git lfs track "*.bin"
git add .gitattributes
git add <large.bin>
git commit -m "track large files via LFS"

20. 排错套路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1) 看看我在哪、改了啥
git status -sb
git log --oneline -n 10
git reflog -n 20

# 2) 远端状况
git remote -v
git fetch --all --prune
git branch -vv

# 3) 解决“我只想回到昨天”
git reflog # 找到目标哈希 X
git reset --hard X # 回到该快照(若是公共分支三思)

# 4) 解决“本地乱了但不想丢”
git stash push -u -m "safety"
git switch -c rescue/$(date +%Y%m%d-%H%M%S)
git stash pop

21. 常用别名建议(.gitconfig)

1
2
3
4
5
6
7
8
9
10
11
12
13
[alias]
st = status -sb
lg = log --oneline --graph --decorate --all
co = checkout
sw = switch
br = branch -vv
ci = commit
dc = diff --cached
rb = rebase
cp = cherry-pick
aa = add -A
undo = reset --soft HEAD~1
last = show -1 --stat

附:常见工作流模板

A. 常规功能分支

1
2
3
4
5
6
git switch -c feature/x
# 开发 -> 提交
git push -u origin feature/x
# 发起 PR(代码平台上处理)
# 合并后:
git switch main && git pull --ff-only && git branch -d feature/x

B. Release 切分支

1
2
3
4
git switch -c release/1.2.0
# 冻结 & 修复 -> 打标签
git tag -a v1.2.0 -m "release 1.2.0"
git push origin release/1.2.0 v1.2.0

C. 从 main 同步更新到长期分支

1
2
3
git switch longrun
git fetch origin
git merge origin/main # 或者 git rebase origin/main

提示

  • 公共分支避免 --force,若必须请用 --force-with-lease
  • 习惯 reflog,关键时刻能救命;
  • 通过 PR / MR 平台做 Code Review 与发布管理;
  • 与团队约定:分支命名、提交信息规范(如 Conventional Commits)、发布流程。