// version control

Git

Cheat Sheet

// 初始化 · 提交 · 分支 · 远程 · 撤销 · 高级操作

01 / 初始化 & 配置
初始化 & 克隆
# 初始化新仓库
git init
git init my-project

# 克隆
git clone https://github.com/user/repo.git
git clone repo.git my-dir       # 指定目录
git clone --depth 1 url           # 浅克隆
git clone --branch main url       # 指定分支
全局配置
# 用户信息
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# 默认编辑器
git config --global core.editor vim

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

# 换行符处理
git config --global core.autocrlf input  # macOS/Linux
git config --global core.autocrlf true   # Windows

# 别名
git config --global alias.st status
git config --global alias.lg "log --oneline --graph"

# 查看配置
git config --list
git config --global --list
02 / 基本操作
Status & Add & Commit
# 查看状态
git status
git status -s           # 简短格式

# 暂存文件
git add file.txt
git add .               # 所有变更
git add -p              # 交互式分块暂存
git add *.py            # 通配符

# 提交
git commit -m "feat: add login"
git commit -am "fix: typo"   # add+commit已跟踪
git commit --amend            # 修改上次提交
git commit --amend --no-edit  # 仅修改内容

# diff
git diff                # 未暂存的变更
git diff --staged       # 已暂存的变更
git diff HEAD~1 HEAD   # 两个commit之间
Push & Pull & Fetch
# 推送
git push
git push origin main
git push -u origin feature  # 设置上游
git push --force-with-lease  # 安全强推
git push --tags               # 推送标签
git push origin --delete branch  # 删远程分支

# 拉取
git pull
git pull origin main
git pull --rebase         # 变基方式拉取

# fetch(不自动合并)
git fetch
git fetch origin
git fetch --all           # 所有远程
git fetch --prune         # 删除已删除的远程分支
03 / Branch 分支操作
分支管理
# 查看分支
git branch              # 本地分支
git branch -r          # 远程分支
git branch -a          # 所有分支
git branch -v          # 带最新提交

# 创建分支
git branch feature/auth
git checkout -b feature/auth  # 创建并切换
git switch -c feature/auth   # 推荐(git 2.23+)

# 切换分支
git checkout main
git switch main

# 重命名分支
git branch -m old-name new-name

# 删除分支
git branch -d feature   # 已合并才能删
git branch -D feature   # 强制删除
Merge & Rebase
# Merge 合并
git merge feature
git merge --no-ff feature   # 强制保留合并记录
git merge --squash feature  # 压缩为单次提交
git merge --abort            # 放弃合并

# Rebase 变基
git rebase main              # 在main上重放
git rebase -i HEAD~3        # 交互式变基(整理提交)
git rebase --continue       # 解决冲突后继续
git rebase --abort          # 放弃变基

# Cherry-pick
git cherry-pick abc1234     # 应用某次提交
git cherry-pick a1..b2      # 范围
04 / 查看历史 & 撤销
查看历史 Log
# 基本日志
git log
git log --oneline
git log --oneline --graph --all
git log -n 10              # 最近10条
git log --since="2 weeks ago"
git log --author="Alice"
git log --grep="fix"        # 搜索提交信息
git log -p file.txt       # 文件变更历史
git log --stat             # 含文件统计

# 搜索代码变更
git log -S "search_term"   # 增删包含该字符串
git log -G "regex"

# 查看某次提交
git show abc1234
git show HEAD
git show HEAD:path/file.txt  # 查看历史版本文件
撤销操作
# 撤销工作区修改
git checkout -- file.txt
git restore file.txt         # 推荐方式
git restore .                # 所有文件

# 撤销暂存区
git reset HEAD file.txt
git restore --staged file.txt

# 撤销提交(保留变更)
git reset --soft HEAD~1      # 退回暂存区
git reset --mixed HEAD~1     # 退回工作区(默认)
git reset --hard HEAD~1      # 完全丢弃(慎用!)

# 创建反转提交(安全,推荐用于公共分支)
git revert abc1234
git revert --no-commit HEAD~3..HEAD

# 找回丢失的提交
git reflog
git checkout HEAD@{2}
05 / 远程仓库 & Tag & Stash
远程仓库
# 查看远程
git remote -v
git remote show origin

# 添加/删除远程
git remote add origin https://...
git remote remove origin
git remote rename origin upstream

# 修改远程URL
git remote set-url origin https://new-url

# 追踪远程分支
git branch --set-upstream-to=origin/main main
git branch -u origin/main
Tag 标签
# 创建标签
git tag v1.0.0                     # 轻量标签
git tag -a v1.0.0 -m "Release 1.0"  # 附注标签
git tag -a v1.0.0 abc1234          # 给历史提交打标

# 查看标签
git tag
git tag -l "v1.*"
git show v1.0.0

# 推送标签
git push origin v1.0.0
git push --tags

# 删除标签
git tag -d v1.0.0
git push origin --delete v1.0.0
Stash 暂存
# 保存工作现场
git stash
git stash push -m "WIP: feature"
git stash push -u    # 包含未跟踪文件
git stash push -p    # 交互式选择

# 查看stash列表
git stash list
git stash show stash@{0}
git stash show -p    # 查看diff

# 恢复
git stash pop         # 恢复并删除
git stash apply       # 恢复但保留
git stash apply stash@{1}

# 删除stash
git stash drop
git stash clear       # 删除所有
高级技巧
# bisect 二分查找bug
git bisect start
git bisect bad          # 当前版本有bug
git bisect good v1.0  # 已知好版本
git bisect reset        # 结束

# blame 逐行责任人
git blame file.txt
git blame -L 10,20 file.txt

# worktree 多工作树
git worktree add ../hotfix hotfix
git worktree list

# submodule
git submodule add url path
git submodule update --init

# 清理
git clean -fd    # 删除未跟踪的文件和目录
git gc            # 垃圾回收,压缩仓库
06 / Commit Message 规范
Conventional Commits
# 格式:
# <type>(<scope>): <subject>

feat: add user authentication
fix(login): resolve token expiry bug
docs: update README
style: format code
refactor(api): extract helper functions
test: add unit tests for parser
chore: update dependencies
perf: optimize database queries
ci: add GitHub Actions workflow
revert: revert feat: add dark mode

# BREAKING CHANGE
feat!: drop support for Node 12
# 或在 footer 添加:
# BREAKING CHANGE: description

// type: feat fix docs style refactor test chore perf ci

.gitignore 模式
# 忽略特定文件
.env
.env.local

# 忽略目录
node_modules/
dist/
__pycache__/
.venv/

# 忽略文件类型
*.log
*.pyc
*.class
*.DS_Store

# 例外(不忽略)
!important.log

# 任意深度目录中
**/temp

# 只在根目录
/config.local.js

# 查看哪些文件被忽略
git check-ignore -v filename