git-worktrees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktrees

Git Worktrees

Second (and third, …) working directories linked to the same
.git
object store. One repo, many checkouts. Shared history, objects, refs, hooks, LFS — no re-clone.
第二个(以及第三个……)与同一个
.git
对象存储关联的工作目录。一个仓库,多个检出版本。共享历史记录、对象、引用、钩子、LFS——无需重新克隆。

Commands

Commands

bash
git worktree add ../hotfix hotfix-branch        # existing branch
git worktree add -b new-branch ../new main      # create from main
git worktree add --detach ../inspect <sha>      # for read-only review
git worktree list [--porcelain]
git worktree lock ../critical                   # prevent prune
git worktree move ../old ../new
git worktree remove ../hotfix                   # after cd out
git worktree prune                              # clean stale metadata
git worktree repair                             # fix links after moves
bash
git worktree add ../hotfix hotfix-branch        # 现有分支
git worktree add -b new-branch ../new main      # 基于main创建新分支
git worktree add --detach ../inspect <sha>      # 用于只读评审
git worktree list [--porcelain]
git worktree lock ../critical                   # 防止被清理
git worktree move ../old ../new
git worktree remove ../hotfix                   # 需先退出该目录
git worktree prune                              # 清理过期元数据
git worktree repair                             # 移动后修复链接

Rules

Rules

  • Same branch can't be checked out in two worktrees (use
    --detach
    or
    --force
    ).
  • Main worktree can't be
    remove
    d — it owns
    .git
    .
  • rm -rf
    on a worktree dir leaves
    .git/worktrees/<name>/
    — run
    prune
    .
  • 同一个分支不能在两个工作树中检出(使用
    --detach
    --force
    )。
  • 主工作树无法被
    remove
    ——它拥有
    .git
    目录。
  • 对工作树目录执行
    rm -rf
    会留下
    .git/worktrees/<name>/
    ——需运行
    prune
    清理。

Workflows

Workflows

Review a PR without losing WIP:
bash
git worktree add ../review-1234 --detach origin/pr/1234
Hotfix during a long build:
bash
git worktree add -b hotfix/urgent ../hotfix origin/main
Per-branch dedicated build caches: each worktree has its own
node_modules
,
target/
, etc. — no cross-branch invalidation.
Parallel bisect: keep main worktree for editing, run
git bisect run
in a second.
无需丢失进行中工作即可评审PR:
bash
git worktree add ../review-1234 --detach origin/pr/1234
长构建期间处理热修复:
bash
git worktree add -b hotfix/urgent ../hotfix origin/main
**按分支专用构建缓存:**每个工作树都有自己的
node_modules
target/
等——不会出现跨分支缓存失效的情况。
**并行二分查找:**主工作树用于编辑,在第二个工作树中运行
git bisect run

Per-Worktree Config

Per-Worktree Config

bash
git config extensions.worktreeConfig true
git config --worktree user.email alt@example.com
core.bare
,
core.worktree
are always worktree-scoped.
bash
git config extensions.worktreeConfig true
git config --worktree user.email alt@example.com
core.bare
core.worktree
始终是工作树作用域的。

Pitfalls

Pitfalls

  • Hooks run for every worktree —
    post-checkout
    firing
    yarn install
    per-worktree is correct but noisy.
  • Stashes are shared across worktrees (single
    refs/stash
    ). Label them.
  • IDE indexers can follow
    .git
    in a linked worktree back to the main repo — point the IDE at the worktree root.
  • Worktree on removable media that disappears →
    git worktree prune
    .
  • 钩子会为所有工作树运行——每个工作树触发
    post-checkout
    钩子执行
    yarn install
    是正确的,但会产生较多日志。
  • 暂存(Stashes)在所有工作树之间共享(单个
    refs/stash
    )。请为它们添加标签。
  • IDE索引器可能会跟随链接工作树中的
    .git
    回到主仓库——请将IDE指向工作树根目录。
  • 工作树所在的可移动介质消失后→运行
    git worktree prune