git
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Operations
Git 操作
Advanced git workflows and conflict resolution.
高级Git工作流与冲突解决。
Decision Trees
决策树
Conflict Resolution Strategy
冲突解决策略
| Situation | Strategy |
|---|---|
| Lock file conflict (pnpm-lock, Cargo.lock, etc.) | Never merge manually. Checkout theirs, regenerate. |
| SOPS encrypted file | Checkout theirs, run |
| Simple content conflict | Resolve manually, prefer smallest diff. |
| Large structural conflict | Consider |
| 场景 | 策略 |
|---|---|
| 锁文件冲突(pnpm-lock、Cargo.lock等) | 切勿手动合并。拉取对方版本,重新生成。 |
| SOPS加密文件 | 拉取对方版本,运行 |
| 简单内容冲突 | 手动解决,优先选择最小差异。 |
| 大型结构性冲突 | 考虑使用 |
Rebase vs Merge
Rebase 与 Merge 选择
| Situation | Use |
|---|---|
| Feature branch behind main | |
| Shared branch (others have it checked out) | Never rebase. Merge only. |
| Cleaning up messy commits before PR | |
| Already pushed and others pulled | Never rebase. Use |
| 场景 | 操作建议 |
|---|---|
| 功能分支落后于主分支 | |
| 共享分支(其他人已检出) | 切勿变基。仅使用Merge。 |
| 提交PR前清理杂乱提交 | 结合squash/fixup使用 |
| 已推送且其他人已拉取的分支 | 切勿变基。改用 |
Undo Operations
撤销操作
| What happened | Fix |
|---|---|
| Wrong commit message (not pushed) | |
| Last commit was wrong (keep changes staged) | |
| Last commit was wrong (keep changes unstaged) | |
| Already pushed bad commit | |
| Need to recover something lost | |
| 问题场景 | 修复方案 |
|---|---|
| 提交信息错误(未推送) | |
| 最后一次提交错误(保留变更暂存) | |
| 最后一次提交错误(保留变更未暂存) | |
| 已推送错误提交 | |
| 需要恢复丢失的内容 | |
Lock File Conflicts
锁文件冲突
Always regenerate, never manually merge:
bash
undefined始终重新生成,切勿手动合并:
bash
undefinedpnpm
pnpm
git checkout --theirs pnpm-lock.yaml && pnpm install && git add pnpm-lock.yaml
git checkout --theirs pnpm-lock.yaml && pnpm install && git add pnpm-lock.yaml
npm
npm
git checkout --theirs package-lock.json && npm install && git add package-lock.json
git checkout --theirs package-lock.json && npm install && git add package-lock.json
Cargo
Cargo
git checkout --theirs Cargo.lock && cargo generate-lockfile && git add Cargo.lock
git checkout --theirs Cargo.lock && cargo generate-lockfile && git add Cargo.lock
SOPS encrypted files
SOPS encrypted files
git checkout --theirs secrets.yaml && sops updatekeys secrets.yaml && git add secrets.yaml
undefinedgit checkout --theirs secrets.yaml && sops updatekeys secrets.yaml && git add secrets.yaml
undefinedArchaeology
仓库溯源
bash
undefinedbash
undefinedFind when a string was added/removed
查找字符串添加/移除的提交记录
git log -S "search string" --oneline
git log -S "search string" --oneline
Blame specific lines
查看特定行的修改历史
git blame -L 10,20 <file>
git blame -L 10,20 <file>
Find commits touching a function
查找修改某个函数的提交
git log -L :functionName:file.js
git log -L :functionName:file.js
Binary search for a bug introduction
二分查找引入bug的提交
git bisect start && git bisect bad HEAD && git bisect good v1.0.0
undefinedgit bisect start && git bisect bad HEAD && git bisect good v1.0.0
undefinedSafety Rules
安全规则
- Never rebase shared branches
- not
--force-with-lease(prevents overwriting others' work)--force - Regenerate lock files -- never merge them
- Backup branch before destructive ops:
git branch backup-$(date +%Y%m%d-%H%M%S) - Never commit large binaries -- use Git LFS
- 切勿对共享分支执行变基
- **使用**而非
--force-with-lease(防止覆盖他人工作)--force - 重新生成锁文件——切勿合并锁文件
- 破坏性操作前创建备份分支:
git branch backup-$(date +%Y%m%d-%H%M%S) - 切勿提交大型二进制文件——使用Git LFS