git

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Operations

Git 操作

Advanced git workflows and conflict resolution.
高级Git工作流与冲突解决。

Decision Trees

决策树

Conflict Resolution Strategy

冲突解决策略

SituationStrategy
Lock file conflict (pnpm-lock, Cargo.lock, etc.)Never merge manually. Checkout theirs, regenerate.
SOPS encrypted fileCheckout theirs, run
sops updatekeys
, re-add.
Simple content conflictResolve manually, prefer smallest diff.
Large structural conflictConsider
--ours
/
--theirs
+ manual reapply of the smaller side.
场景策略
锁文件冲突(pnpm-lock、Cargo.lock等)切勿手动合并。拉取对方版本,重新生成。
SOPS加密文件拉取对方版本,运行
sops updatekeys
,重新添加。
简单内容冲突手动解决,优先选择最小差异。
大型结构性冲突考虑使用
--ours
/
--theirs
+ 手动重新应用变更较少的一方。

Rebase vs Merge

Rebase 与 Merge 选择

SituationUse
Feature branch behind main
git rebase origin/main
Shared branch (others have it checked out)Never rebase. Merge only.
Cleaning up messy commits before PR
git rebase -i
with squash/fixup
Already pushed and others pulledNever rebase. Use
git revert
instead.
场景操作建议
功能分支落后于主分支
git rebase origin/main
共享分支(其他人已检出)切勿变基。仅使用Merge。
提交PR前清理杂乱提交结合squash/fixup使用
git rebase -i
已推送且其他人已拉取的分支切勿变基。改用
git revert

Undo Operations

撤销操作

What happenedFix
Wrong commit message (not pushed)
git commit --amend
Last commit was wrong (keep changes staged)
git reset --soft HEAD~1
Last commit was wrong (keep changes unstaged)
git reset HEAD~1
Already pushed bad commit
git revert <hash>
(creates new commit)
Need to recover something lost
git reflog
then
git checkout HEAD@{N}
问题场景修复方案
提交信息错误(未推送)
git commit --amend
最后一次提交错误(保留变更暂存)
git reset --soft HEAD~1
最后一次提交错误(保留变更未暂存)
git reset HEAD~1
已推送错误提交
git revert <hash>
(创建新提交)
需要恢复丢失的内容
git reflog
然后执行
git checkout HEAD@{N}

Lock File Conflicts

锁文件冲突

Always regenerate, never manually merge:
bash
undefined
始终重新生成,切勿手动合并:
bash
undefined

pnpm

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
undefined
git checkout --theirs secrets.yaml && sops updatekeys secrets.yaml && git add secrets.yaml
undefined

Archaeology

仓库溯源

bash
undefined
bash
undefined

Find 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
undefined
git bisect start && git bisect bad HEAD && git bisect good v1.0.0
undefined

Safety Rules

安全规则

  1. Never rebase shared branches
  2. --force-with-lease
    not
    --force
    (prevents overwriting others' work)
  3. Regenerate lock files -- never merge them
  4. Backup branch before destructive ops:
    git branch backup-$(date +%Y%m%d-%H%M%S)
  5. Never commit large binaries -- use Git LFS
  1. 切勿对共享分支执行变基
  2. **使用
    --force-with-lease
    **而非
    --force
    (防止覆盖他人工作)
  3. 重新生成锁文件——切勿合并锁文件
  4. 破坏性操作前创建备份分支:
    git branch backup-$(date +%Y%m%d-%H%M%S)
  5. 切勿提交大型二进制文件——使用Git LFS