logo

Git Cheat Sheet


title: Git date: 2020-11-25 18:28:43 background: bg-[#d7593e] tags: - github - gitlab - version - VCS - 版本控制 - 协作开发 categories: - DevOps intro: | Git 版本控制速查表,涵盖日常开发所需的常用命令、分支管理、远程协作、冲突解决等内容。适合开发者日常查阅和学习。 plugins: - copyCode

入门指南 {.cols-2}

创建仓库

创建新的本地仓库 (Create new local repository)

$ git init [project-name]

克隆远程仓库 (Clone remote repository)

$ git clone <url>

# 克隆到指定目录
$ git clone <url> my-directory

# 浅克隆(只获取最新提交,适合大仓库)
$ git clone --depth 1 <url>

# 克隆指定分支
$ git clone -b <branch> <url>

基础配置

# 设置用户名(全局)
$ git config --global user.name "Your Name"

# 设置邮箱(全局)
$ git config --global user.email "you@example.com"

# 设置默认编辑器
$ git config --global core.editor "code --wait"

# 启用颜色输出
$ git config --global color.ui auto

# 设置默认分支名
$ git config --global init.defaultBranch main

# 查看配置
$ git config --list
$ git config --global --list

# 编辑全局配置
$ git config --global --edit

提交更改 {.row-span-2}

查看工作区状态 (Check status)

$ git status
$ git status -s    # 简洁模式

暂存文件 (Stage files)

# 暂存单个文件
$ git add <file>

# 暂存所有更改
$ git add .
$ git add -A

# 暂存部分更改(交互模式)
$ git add -p

提交更改 (Commit changes)

# 提交暂存的更改
$ git commit -m "commit message"

# 暂存并提交所有已跟踪文件
$ git commit -am "commit message"

# 修改上次提交
$ git commit --amend -m "new message"

# 修改上次提交但不改消息
$ git commit --amend --no-edit

# 空提交(用于触发 CI)
$ git commit --allow-empty -m "trigger CI"

撤销更改 (Undo changes)

# 撤销工作区更改(未暂存)
$ git restore <file>
$ git checkout -- <file>

# 取消暂存
$ git restore --staged <file>
$ git reset <file>

# 重置到上次提交
$ git reset --hard

# 撤销某次提交(创建新提交)
$ git revert <commit>

查看差异

# 工作区 vs 暂存区
$ git diff

# 暂存区 vs 最新提交
$ git diff --staged
$ git diff --cached

# 工作区 vs 最新提交
$ git diff HEAD

# 比较两个分支
$ git diff branch1..branch2

# 比较两次提交
$ git diff <commit1> <commit2>

# 只显示文件名
$ git diff --name-only

# 统计更改
$ git diff --stat

分支管理 {.cols-2}

分支操作

# 列出本地分支
$ git branch

# 列出所有分支(含远程)
$ git branch -a
$ git branch -av    # 显示详情

# 创建新分支
$ git branch <branch-name>

# 切换分支
$ git checkout <branch>
$ git switch <branch>      # Git 2.23+

# 创建并切换分支
$ git checkout -b <branch>
$ git switch -c <branch>

# 重命名分支
$ git branch -m <old> <new>

# 删除分支
$ git branch -d <branch>    # 安全删除
$ git branch -D <branch>    # 强制删除

# 查看分支合并状态
$ git branch --merged
$ git branch --no-merged

分支合并

# 合并分支到当前分支
$ git merge <branch>

# 非快进合并(保留分支历史)
$ git merge --no-ff <branch>

# 只快进合并
$ git merge --ff-only <branch>

# 合并时压缩提交
$ git merge --squash <branch>

# 中止合并
$ git merge --abort

变基操作

# 变基到目标分支
$ git rebase <branch>

# 交互式变基(修改最近 N 次提交)
$ git rebase -i HEAD~N

# 继续变基
$ git rebase --continue

# 跳过当前提交
$ git rebase --skip

# 中止变基
$ git rebase --abort

# 变基时保留合并提交
$ git rebase --preserve-merges <branch>

标签管理

# 列出标签
$ git tag
$ git tag -l "v1.*"    # 筛选

# 创建轻量标签
$ git tag <tag-name>

# 创建附注标签
$ git tag -a <tag-name> -m "message"

# 给指定提交打标签
$ git tag -a <tag-name> <commit>

# 推送标签到远程
$ git push origin <tag-name>
$ git push origin --tags    # 推送所有标签

# 删除标签
$ git tag -d <tag-name>
$ git push origin :refs/tags/<tag-name>    # 删除远程标签

远程协作 {.cols-2}

远程仓库

# 查看远程仓库
$ git remote
$ git remote -v

# 添加远程仓库
$ git remote add <name> <url>

# 修改远程仓库 URL
$ git remote set-url origin <new-url>

# 重命名远程仓库
$ git remote rename <old> <new>

# 删除远程仓库
$ git remote rm <name>

# 查看远程仓库详情
$ git remote show origin

拉取与推送

# 获取远程更新(不合并)
$ git fetch
$ git fetch origin
$ git fetch --all    # 所有远程

# 拉取并合并
$ git pull
$ git pull origin <branch>

# 拉取并变基
$ git pull --rebase

# 推送到远程
$ git push origin <branch>

# 推送并设置上游
$ git push -u origin <branch>

# 强制推送(谨慎使用)
$ git push --force
$ git push --force-with-lease    # 更安全

# 删除远程分支
$ git push origin --delete <branch>

同步分支

# 跟踪远程分支
$ git checkout --track origin/<branch>

# 设置上游分支
$ git branch -u origin/<branch>

# 查看跟踪关系
$ git branch -vv

# 清理已删除的远程分支
$ git fetch --prune
$ git remote prune origin

Cherry-pick

# 挑选单个提交
$ git cherry-pick <commit>

# 挑选多个提交
$ git cherry-pick <commit1> <commit2>

# 挑选范围
$ git cherry-pick <start>..<end>

# 只暂存不提交
$ git cherry-pick -n <commit>

# 继续/中止
$ git cherry-pick --continue
$ git cherry-pick --abort

查看历史 {.cols-2}

日志查看

# 查看提交历史
$ git log

# 简洁模式
$ git log --oneline

# 显示差异
$ git log -p

# 显示统计
$ git log --stat

# 限制数量
$ git log -n 10
$ git log -10

# 时间范围
$ git log --since="2024-01-01"
$ git log --until="2024-12-31"
$ git log --after="1 week ago"

# 按作者筛选
$ git log --author="name"

# 按提交信息筛选
$ git log --grep="keyword"

# 按内容搜索
$ git log -S "code pattern"

图形化日志

# 分支图
$ git log --graph --oneline --all

# 美化输出
$ git log --pretty=format:"%h %an %ar - %s"

# 常用格式
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

# 显示分支关系
$ git log --graph --decorate --all

文件历史

# 查看文件历史
$ git log -- <file>
$ git log -p -- <file>

# 跟踪文件重命名
$ git log --follow <file>

# 查看文件每行作者
$ git blame <file>
$ git blame -L 10,20 <file>    # 指定行范围

# 查看提交详情
$ git show <commit>
$ git show <commit>:<file>

比较分支

# A 有而 B 没有的提交
$ git log B..A
$ git log origin/main..HEAD

# 两边各自独有的提交
$ git log A...B

# 统计差异
$ git shortlog -sn

# 查看谁贡献了代码
$ git shortlog -sn --all

撤销与恢复 {.cols-2}

Reset 模式

# 软重置:只移动 HEAD,保留暂存和工作区
$ git reset --soft <commit>

# 混合重置(默认):移动 HEAD,重置暂存区
$ git reset --mixed <commit>
$ git reset <commit>

# 硬重置:完全重置到指定提交
$ git reset --hard <commit>

# 重置到上一个提交
$ git reset --hard HEAD~1

# 重置单个文件
$ git reset HEAD <file>

Revert 撤销

# 撤销提交(创建新提交)
$ git revert <commit>

# 撤销多个提交
$ git revert <commit1> <commit2>

# 撤销但不自动提交
$ git revert -n <commit>

# 撤销合并提交
$ git revert -m 1 <merge-commit>

找回丢失的提交

# 查看引用日志
$ git reflog
$ git reflog show <branch>

# 恢复到某个操作
$ git reset --hard HEAD@{2}

# 创建分支保存丢失的提交
$ git branch recover-branch <commit>

# 查看悬空对象
$ git fsck --lost-found

Stash 暂存

# 暂存当前更改
$ git stash
$ git stash save "message"

# 包含未跟踪文件
$ git stash -u
$ git stash --include-untracked

# 列出暂存
$ git stash list

# 恢复最近的暂存
$ git stash pop

# 恢复指定暂存
$ git stash pop stash@{n}

# 应用但不删除
$ git stash apply

# 删除暂存
$ git stash drop stash@{n}
$ git stash clear    # 删除所有

# 从暂存创建分支
$ git stash branch <branch-name>

高级操作 {.cols-2}

交互式变基

# 修改最近 5 次提交
$ git rebase -i HEAD~5

在编辑器中可用的命令:

pick   = 使用提交
reword = 使用提交,修改消息
edit   = 使用提交,停下来修改
squash = 与前一个提交合并
fixup  = 与前一个提交合并,丢弃消息
drop   = 删除提交

示例:合并提交

# 原始状态
pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit

# 修改为
pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit

Worktree 工作树

# 创建工作树
$ git worktree add <path> <branch>
$ git worktree add ../feature feature-branch

# 创建新分支的工作树
$ git worktree add -b <new-branch> <path>

# 列出工作树
$ git worktree list

# 删除工作树
$ git worktree remove <path>

# 清理过时的工作树
$ git worktree prune

Submodule 子模块

# 添加子模块
$ git submodule add <url> <path>

# 初始化子模块
$ git submodule init

# 更新子模块
$ git submodule update
$ git submodule update --init --recursive

# 克隆时包含子模块
$ git clone --recursive <url>

# 更新到最新
$ git submodule update --remote

# 删除子模块
$ git submodule deinit <path>
$ git rm <path>

Bisect 二分查找

# 开始二分查找
$ git bisect start

# 标记当前为坏
$ git bisect bad

# 标记某个提交为好
$ git bisect good <commit>

# Git 会自动检出中间版本
# 测试后标记
$ git bisect good    # 或 bad

# 结束查找
$ git bisect reset

# 自动查找(提供测试脚本)
$ git bisect run ./test.sh

文件操作 {.cols-2}

移动和删除

# 移动/重命名文件
$ git mv <old-path> <new-path>

# 删除文件
$ git rm <file>

# 从暂存区移除但保留文件
$ git rm --cached <file>

# 删除已忽略的缓存文件
$ git rm -r --cached .
$ git add .

.gitignore 忽略文件

# 注释
# 忽略所有 .log 文件
*.log

# 忽略 node_modules 目录
node_modules/

# 忽略根目录的文件
/config.local.js

# 不忽略特定文件
!important.log

# 忽略所有 .env 文件但保留示例
.env*
!.env.example

# 忽略构建目录
dist/
build/
*.min.js

# 忽略 IDE 配置
.idea/
.vscode/
*.swp

# 忽略系统文件
.DS_Store
Thumbs.db

清理工作区

# 显示将被删除的文件
$ git clean -n
$ git clean --dry-run

# 删除未跟踪文件
$ git clean -f

# 包含目录
$ git clean -fd

# 包含忽略的文件
$ git clean -fx

# 交互模式
$ git clean -i

大文件处理

# 查看仓库大小
$ git count-objects -vH

# 找出大文件
$ git rev-list --objects --all | \
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
  sed -n 's/^blob //p' | \
  sort -rnk2 | head -20

# 使用 Git LFS
$ git lfs install
$ git lfs track "*.psd"
$ git add .gitattributes

实用技巧 {.cols-2}

别名配置

# 常用别名
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

# 高级别名
$ git config --global alias.lg "log --graph --oneline --all"
$ git config --global alias.last "log -1 HEAD"
$ git config --global alias.unstage "reset HEAD --"
$ git config --global alias.undo "reset --soft HEAD~1"

# 查看别名
$ git config --get-regexp alias

快捷操作

# 快速切换到上一个分支
$ git checkout -

# 修改上次提交
$ git commit --amend

# 查看某文件的最后修改
$ git log -1 -- <file>

# 查看谁最后修改了文件
$ git log -1 --format="%an" -- <file>

# 统计代码行数
$ git diff --stat HEAD~10

# 导出指定版本的文件
$ git archive --format=zip HEAD > archive.zip

搜索

# 在文件内容中搜索
$ git grep "pattern"
$ git grep -n "pattern"    # 显示行号
$ git grep -c "pattern"    # 统计次数

# 在历史中搜索
$ git log -S "pattern"     # 内容变更
$ git log -G "regex"       # 正则匹配
$ git log --all --grep="keyword"

常见问题

修复错误的分支提交:

# 在错误分支上提交了代码
$ git stash
$ git checkout correct-branch
$ git stash pop

合并冲突解决:

# 查看冲突文件
$ git status

# 解决冲突后
$ git add <resolved-file>
$ git commit

# 使用工具解决
$ git mergetool

# 保留某一方的版本
$ git checkout --ours <file>
$ git checkout --theirs <file>

撤销已推送的提交:

# 方法1: revert(推荐)
$ git revert <commit>
$ git push

# 方法2: reset + force push(谨慎)
$ git reset --hard <commit>
$ git push --force-with-lease

工作流程 {.cols-2}

Git Flow

# 主要分支
main/master  - 生产环境
develop      - 开发环境

# 辅助分支
feature/*    - 功能开发
release/*    - 发布准备
hotfix/*     - 紧急修复

# 功能分支流程
$ git checkout -b feature/new-feature develop
# ... 开发 ...
$ git checkout develop
$ git merge --no-ff feature/new-feature
$ git branch -d feature/new-feature

GitHub Flow

# 简化流程
main  - 永远可部署

# 流程
$ git checkout -b feature-branch
# ... 开发和提交 ...
$ git push -u origin feature-branch
# 创建 Pull Request
# Code Review
# 合并到 main
# 部署

常用流程命令

# 开始新功能
$ git checkout main
$ git pull
$ git checkout -b feature/xxx

# 保持同步
$ git fetch origin
$ git rebase origin/main

# 准备提交 PR
$ git push -u origin feature/xxx

# 清理完成的分支
$ git checkout main
$ git branch -d feature/xxx
$ git push origin --delete feature/xxx

Commit 规范

<type>(<scope>): <subject>

<body>

<footer>

类型 (type):

feat     - 新功能
fix      - Bug 修复
docs     - 文档更新
style    - 代码格式(不影响功能)
refactor - 重构
perf     - 性能优化
test     - 测试相关
chore    - 构建/工具变更

示例:

feat(auth): add login with OAuth2

- Support Google OAuth2
- Add refresh token handling
- Update user model

Closes #123
🛠️ 开发工具

Git

Git Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 开发工具🧭 Markdown 速查🏷️ 2 个标签
#git#version-control
向下滚动查看内容
返回全部 Cheat Sheets

入门指南

创建仓库

创建新的本地仓库 (Create new local repository)

SHELL
滚动查看更多
$ git init [project-name]

克隆远程仓库 (Clone remote repository)

SHELL
滚动查看更多
$ git clone <url>

# 克隆到指定目录
$ git clone <url> my-directory

# 浅克隆(只获取最新提交,适合大仓库)
$ git clone --depth 1 <url>

# 克隆指定分支
$ git clone -b <branch> <url>
基础配置
SHELL
滚动查看更多
# 设置用户名(全局)
$ git config --global user.name "Your Name"

# 设置邮箱(全局)
$ git config --global user.email "you@example.com"

# 设置默认编辑器
$ git config --global core.editor "code --wait"

# 启用颜色输出
$ git config --global color.ui auto

# 设置默认分支名
$ git config --global init.defaultBranch main

# 查看配置
$ git config --list
$ git config --global --list

# 编辑全局配置
$ git config --global --edit
提交更改

查看工作区状态 (Check status)

SHELL
滚动查看更多
$ git status
$ git status -s    # 简洁模式

暂存文件 (Stage files)

SHELL
滚动查看更多
# 暂存单个文件
$ git add <file>

# 暂存所有更改
$ git add .
$ git add -A

# 暂存部分更改(交互模式)
$ git add -p

提交更改 (Commit changes)

SHELL
滚动查看更多
# 提交暂存的更改
$ git commit -m "commit message"

# 暂存并提交所有已跟踪文件
$ git commit -am "commit message"

# 修改上次提交
$ git commit --amend -m "new message"

# 修改上次提交但不改消息
$ git commit --amend --no-edit

# 空提交(用于触发 CI)
$ git commit --allow-empty -m "trigger CI"

撤销更改 (Undo changes)

SHELL
滚动查看更多
# 撤销工作区更改(未暂存)
$ git restore <file>
$ git checkout -- <file>

# 取消暂存
$ git restore --staged <file>
$ git reset <file>

# 重置到上次提交
$ git reset --hard

# 撤销某次提交(创建新提交)
$ git revert <commit>
查看差异
SHELL
滚动查看更多
# 工作区 vs 暂存区
$ git diff

# 暂存区 vs 最新提交
$ git diff --staged
$ git diff --cached

# 工作区 vs 最新提交
$ git diff HEAD

# 比较两个分支
$ git diff branch1..branch2

# 比较两次提交
$ git diff <commit1> <commit2>

# 只显示文件名
$ git diff --name-only

# 统计更改
$ git diff --stat

分支管理

分支操作
SHELL
滚动查看更多
# 列出本地分支
$ git branch

# 列出所有分支(含远程)
$ git branch -a
$ git branch -av    # 显示详情

# 创建新分支
$ git branch <branch-name>

# 切换分支
$ git checkout <branch>
$ git switch <branch>      # Git 2.23+

# 创建并切换分支
$ git checkout -b <branch>
$ git switch -c <branch>

# 重命名分支
$ git branch -m <old> <new>

# 删除分支
$ git branch -d <branch>    # 安全删除
$ git branch -D <branch>    # 强制删除

# 查看分支合并状态
$ git branch --merged
$ git branch --no-merged
分支合并
SHELL
滚动查看更多
# 合并分支到当前分支
$ git merge <branch>

# 非快进合并(保留分支历史)
$ git merge --no-ff <branch>

# 只快进合并
$ git merge --ff-only <branch>

# 合并时压缩提交
$ git merge --squash <branch>

# 中止合并
$ git merge --abort
变基操作
SHELL
滚动查看更多
# 变基到目标分支
$ git rebase <branch>

# 交互式变基(修改最近 N 次提交)
$ git rebase -i HEAD~N

# 继续变基
$ git rebase --continue

# 跳过当前提交
$ git rebase --skip

# 中止变基
$ git rebase --abort

# 变基时保留合并提交
$ git rebase --preserve-merges <branch>
标签管理
SHELL
滚动查看更多
# 列出标签
$ git tag
$ git tag -l "v1.*"    # 筛选

# 创建轻量标签
$ git tag <tag-name>

# 创建附注标签
$ git tag -a <tag-name> -m "message"

# 给指定提交打标签
$ git tag -a <tag-name> <commit>

# 推送标签到远程
$ git push origin <tag-name>
$ git push origin --tags    # 推送所有标签

# 删除标签
$ git tag -d <tag-name>
$ git push origin :refs/tags/<tag-name>    # 删除远程标签

远程协作

远程仓库
SHELL
滚动查看更多
# 查看远程仓库
$ git remote
$ git remote -v

# 添加远程仓库
$ git remote add <name> <url>

# 修改远程仓库 URL
$ git remote set-url origin <new-url>

# 重命名远程仓库
$ git remote rename <old> <new>

# 删除远程仓库
$ git remote rm <name>

# 查看远程仓库详情
$ git remote show origin
拉取与推送
SHELL
滚动查看更多
# 获取远程更新(不合并)
$ git fetch
$ git fetch origin
$ git fetch --all    # 所有远程

# 拉取并合并
$ git pull
$ git pull origin <branch>

# 拉取并变基
$ git pull --rebase

# 推送到远程
$ git push origin <branch>

# 推送并设置上游
$ git push -u origin <branch>

# 强制推送(谨慎使用)
$ git push --force
$ git push --force-with-lease    # 更安全

# 删除远程分支
$ git push origin --delete <branch>
同步分支
SHELL
滚动查看更多
# 跟踪远程分支
$ git checkout --track origin/<branch>

# 设置上游分支
$ git branch -u origin/<branch>

# 查看跟踪关系
$ git branch -vv

# 清理已删除的远程分支
$ git fetch --prune
$ git remote prune origin
Cherry-pick
SHELL
滚动查看更多
# 挑选单个提交
$ git cherry-pick <commit>

# 挑选多个提交
$ git cherry-pick <commit1> <commit2>

# 挑选范围
$ git cherry-pick <start>..<end>

# 只暂存不提交
$ git cherry-pick -n <commit>

# 继续/中止
$ git cherry-pick --continue
$ git cherry-pick --abort

查看历史

日志查看
SHELL
滚动查看更多
# 查看提交历史
$ git log

# 简洁模式
$ git log --oneline

# 显示差异
$ git log -p

# 显示统计
$ git log --stat

# 限制数量
$ git log -n 10
$ git log -10

# 时间范围
$ git log --since="2024-01-01"
$ git log --until="2024-12-31"
$ git log --after="1 week ago"

# 按作者筛选
$ git log --author="name"

# 按提交信息筛选
$ git log --grep="keyword"

# 按内容搜索
$ git log -S "code pattern"
图形化日志
SHELL
滚动查看更多
# 分支图
$ git log --graph --oneline --all

# 美化输出
$ git log --pretty=format:"%h %an %ar - %s"

# 常用格式
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

# 显示分支关系
$ git log --graph --decorate --all
文件历史
SHELL
滚动查看更多
# 查看文件历史
$ git log -- <file>
$ git log -p -- <file>

# 跟踪文件重命名
$ git log --follow <file>

# 查看文件每行作者
$ git blame <file>
$ git blame -L 10,20 <file>    # 指定行范围

# 查看提交详情
$ git show <commit>
$ git show <commit>:<file>
比较分支
SHELL
滚动查看更多
# A 有而 B 没有的提交
$ git log B..A
$ git log origin/main..HEAD

# 两边各自独有的提交
$ git log A...B

# 统计差异
$ git shortlog -sn

# 查看谁贡献了代码
$ git shortlog -sn --all

撤销与恢复

Reset 模式
SHELL
滚动查看更多
# 软重置:只移动 HEAD,保留暂存和工作区
$ git reset --soft <commit>

# 混合重置(默认):移动 HEAD,重置暂存区
$ git reset --mixed <commit>
$ git reset <commit>

# 硬重置:完全重置到指定提交
$ git reset --hard <commit>

# 重置到上一个提交
$ git reset --hard HEAD~1

# 重置单个文件
$ git reset HEAD <file>
Revert 撤销
SHELL
滚动查看更多
# 撤销提交(创建新提交)
$ git revert <commit>

# 撤销多个提交
$ git revert <commit1> <commit2>

# 撤销但不自动提交
$ git revert -n <commit>

# 撤销合并提交
$ git revert -m 1 <merge-commit>
找回丢失的提交
SHELL
滚动查看更多
# 查看引用日志
$ git reflog
$ git reflog show <branch>

# 恢复到某个操作
$ git reset --hard HEAD@{2}

# 创建分支保存丢失的提交
$ git branch recover-branch <commit>

# 查看悬空对象
$ git fsck --lost-found
Stash 暂存
SHELL
滚动查看更多
# 暂存当前更改
$ git stash
$ git stash save "message"

# 包含未跟踪文件
$ git stash -u
$ git stash --include-untracked

# 列出暂存
$ git stash list

# 恢复最近的暂存
$ git stash pop

# 恢复指定暂存
$ git stash pop stash@{n}

# 应用但不删除
$ git stash apply

# 删除暂存
$ git stash drop stash@{n}
$ git stash clear    # 删除所有

# 从暂存创建分支
$ git stash branch <branch-name>

高级操作

交互式变基
SHELL
滚动查看更多
# 修改最近 5 次提交
$ git rebase -i HEAD~5

在编辑器中可用的命令:

CODE
滚动查看更多
pick   = 使用提交
reword = 使用提交,修改消息
edit   = 使用提交,停下来修改
squash = 与前一个提交合并
fixup  = 与前一个提交合并,丢弃消息
drop   = 删除提交

示例:合并提交

SHELL
滚动查看更多
# 原始状态
pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit

# 修改为
pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit
Worktree 工作树
SHELL
滚动查看更多
# 创建工作树
$ git worktree add <path> <branch>
$ git worktree add ../feature feature-branch

# 创建新分支的工作树
$ git worktree add -b <new-branch> <path>

# 列出工作树
$ git worktree list

# 删除工作树
$ git worktree remove <path>

# 清理过时的工作树
$ git worktree prune
Submodule 子模块
SHELL
滚动查看更多
# 添加子模块
$ git submodule add <url> <path>

# 初始化子模块
$ git submodule init

# 更新子模块
$ git submodule update
$ git submodule update --init --recursive

# 克隆时包含子模块
$ git clone --recursive <url>

# 更新到最新
$ git submodule update --remote

# 删除子模块
$ git submodule deinit <path>
$ git rm <path>
Bisect 二分查找
SHELL
滚动查看更多
# 开始二分查找
$ git bisect start

# 标记当前为坏
$ git bisect bad

# 标记某个提交为好
$ git bisect good <commit>

# Git 会自动检出中间版本
# 测试后标记
$ git bisect good    # 或 bad

# 结束查找
$ git bisect reset

# 自动查找(提供测试脚本)
$ git bisect run ./test.sh

文件操作

移动和删除
SHELL
滚动查看更多
# 移动/重命名文件
$ git mv <old-path> <new-path>

# 删除文件
$ git rm <file>

# 从暂存区移除但保留文件
$ git rm --cached <file>

# 删除已忽略的缓存文件
$ git rm -r --cached .
$ git add .
.gitignore 忽略文件
GITIGNORE
滚动查看更多
# 注释
# 忽略所有 .log 文件
*.log

# 忽略 node_modules 目录
node_modules/

# 忽略根目录的文件
/config.local.js

# 不忽略特定文件
!important.log

# 忽略所有 .env 文件但保留示例
.env*
!.env.example

# 忽略构建目录
dist/
build/
*.min.js

# 忽略 IDE 配置
.idea/
.vscode/
*.swp

# 忽略系统文件
.DS_Store
Thumbs.db
清理工作区
SHELL
滚动查看更多
# 显示将被删除的文件
$ git clean -n
$ git clean --dry-run

# 删除未跟踪文件
$ git clean -f

# 包含目录
$ git clean -fd

# 包含忽略的文件
$ git clean -fx

# 交互模式
$ git clean -i
大文件处理
SHELL
滚动查看更多
# 查看仓库大小
$ git count-objects -vH

# 找出大文件
$ git rev-list --objects --all | \
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
  sed -n 's/^blob //p' | \
  sort -rnk2 | head -20

# 使用 Git LFS
$ git lfs install
$ git lfs track "*.psd"
$ git add .gitattributes

实用技巧

别名配置
SHELL
滚动查看更多
# 常用别名
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

# 高级别名
$ git config --global alias.lg "log --graph --oneline --all"
$ git config --global alias.last "log -1 HEAD"
$ git config --global alias.unstage "reset HEAD --"
$ git config --global alias.undo "reset --soft HEAD~1"

# 查看别名
$ git config --get-regexp alias
快捷操作
SHELL
滚动查看更多
# 快速切换到上一个分支
$ git checkout -

# 修改上次提交
$ git commit --amend

# 查看某文件的最后修改
$ git log -1 -- <file>

# 查看谁最后修改了文件
$ git log -1 --format="%an" -- <file>

# 统计代码行数
$ git diff --stat HEAD~10

# 导出指定版本的文件
$ git archive --format=zip HEAD > archive.zip
搜索
SHELL
滚动查看更多
# 在文件内容中搜索
$ git grep "pattern"
$ git grep -n "pattern"    # 显示行号
$ git grep -c "pattern"    # 统计次数

# 在历史中搜索
$ git log -S "pattern"     # 内容变更
$ git log -G "regex"       # 正则匹配
$ git log --all --grep="keyword"
常见问题

修复错误的分支提交:

SHELL
滚动查看更多
# 在错误分支上提交了代码
$ git stash
$ git checkout correct-branch
$ git stash pop

合并冲突解决:

SHELL
滚动查看更多
# 查看冲突文件
$ git status

# 解决冲突后
$ git add <resolved-file>
$ git commit

# 使用工具解决
$ git mergetool

# 保留某一方的版本
$ git checkout --ours <file>
$ git checkout --theirs <file>

撤销已推送的提交:

SHELL
滚动查看更多
# 方法1: revert(推荐)
$ git revert <commit>
$ git push

# 方法2: reset + force push(谨慎)
$ git reset --hard <commit>
$ git push --force-with-lease

工作流程

Git Flow
SHELL
滚动查看更多
# 主要分支
main/master  - 生产环境
develop      - 开发环境

# 辅助分支
feature/*    - 功能开发
release/*    - 发布准备
hotfix/*     - 紧急修复

# 功能分支流程
$ git checkout -b feature/new-feature develop
# ... 开发 ...
$ git checkout develop
$ git merge --no-ff feature/new-feature
$ git branch -d feature/new-feature
GitHub Flow
SHELL
滚动查看更多
# 简化流程
main  - 永远可部署

# 流程
$ git checkout -b feature-branch
# ... 开发和提交 ...
$ git push -u origin feature-branch
# 创建 Pull Request
# Code Review
# 合并到 main
# 部署
常用流程命令
SHELL
滚动查看更多
# 开始新功能
$ git checkout main
$ git pull
$ git checkout -b feature/xxx

# 保持同步
$ git fetch origin
$ git rebase origin/main

# 准备提交 PR
$ git push -u origin feature/xxx

# 清理完成的分支
$ git checkout main
$ git branch -d feature/xxx
$ git push origin --delete feature/xxx
Commit 规范
CODE
滚动查看更多
<type>(<scope>): <subject>

<body>

<footer>

类型 (type):

CODE
滚动查看更多
feat     - 新功能
fix      - Bug 修复
docs     - 文档更新
style    - 代码格式(不影响功能)
refactor - 重构
perf     - 性能优化
test     - 测试相关
chore    - 构建/工具变更

示例:

CODE
滚动查看更多
feat(auth): add login with OAuth2

- Support Google OAuth2
- Add refresh token handling
- Update user model

Closes #123

相关 Cheat Sheets