<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Git Worktree · 仁才德</title>
    <link>https://jared.lynskey.co.nz/zh-cn/tags/git-worktree/</link>
    <description>关于软件工程与研发团队领导力的短文——绩效评估、招聘与团队扩张、CI/CD、DevOps 监控、智能体 AI。写于首尔。</description>
    <generator>Hugo</generator>
    <language>zh-CN</language>
    <copyright>© 2026 仁才德</copyright>
    <lastBuildDate>Thu, 19 Nov 2026 00:00:00 +1300</lastBuildDate>
    <atom:link href="https://jared.lynskey.co.nz/zh-cn/tags/git-worktree/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Claude Code worktree 如何使用：让多个智能体并行工作而不互相覆盖</title>
      <link>https://jared.lynskey.co.nz/zh-cn/posts/2026/claude-code-git-worktrees/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/zh-cn/posts/2026/claude-code-git-worktrees/</guid>
      <pubDate>Thu, 19 Nov 2026 00:00:00 +1300</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Claude Code</category>
      <category>AI Agents</category>
      <category>Git</category>
      <category>Git Worktree</category>
      <category>tmux</category>
      <category>Developer Tools</category>
      <category>Parallel Development</category>
      <description>Claude Code worktree 如何使用：claude -w 的用法，用 .worktreeinclude 带上 .env，node_modules 怎么处理，base ref 和清理，以及在 tmux 里并行跑多个智能体。</description>
      <content:encoded><![CDATA[<p>要让 Claude Code 在 git worktree 里运行，用 <code>claude -w &lt;名字&gt;</code> 启动就行（完整写法是 <code>--worktree</code>）。Claude Code 会在仓库根目录下创建 <code>.claude/worktrees/&lt;名字&gt;/</code>，并在里面检出一个从远程默认分支切出来的新分支 <code>worktree-&lt;名字&gt;</code>。从这以后，这个会话只能修改这个目录里的文件。在另一个终端里换个名字再跑一遍同样的命令，就有了两个碰不到彼此文件的智能体。坑在于 worktree 是一份全新的检出：<code>.env</code>、<code>node_modules</code> 以及其他被 git 忽略的东西，在你复制或安装之前都不存在。我在 worktree 上踩过的坑，几乎全是这一条。</p>
<p>我平时会同时跑很多个智能体，用上 worktree 是吃了几次亏之后的事。这篇文章讲 <code>claude -w</code> 实际怎么工作（我在 2.1.328 版本上验证过）、<code>.worktreeinclude</code> 是干什么的、<code>node_modules</code> 怎么办、我在 tmux 里怎么摆这些会话，以及什么时候我还是直接用 <code>git worktree add</code>。</p>
<figure><div class="film-pair">
  <div class="mc-film film-dark">
  <video
    class="my-0 rounded-md"
    controls playsinline preload="none"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film-poster.jpg" style="background:url('data:image/webp;base64,UklGRoQAAABXRUJQVlA4IHgAAABwBQCdASogABIAPw1wq0omIyGYDAQAYxDE9IBn0BuOmrQv+whr1JowkP2GvdEWiylHGHsAAP7r1B494gmKZxYarGSLJSBCQLucUwJnafSMMs2DcN8Xi7b7w/dBQJDESVxNVTjZ96FIOv3VtbbF3xp3++/ULAEhgAA=') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="用旁白介绍这篇文章的短视频">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film.webm" type="video/webm">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_en.vtt" srclang="en" label="English">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_ja.vtt" srclang="ja" label="日本語">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_ko.vtt" srclang="ko" label="한국어">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_vi.vtt" srclang="vi" label="Tiếng Việt">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_zh-cn.vtt" srclang="zh-cn" label="简体中文" default>
  </video>
  </div>
  <div class="mc-film film-light">
  <video
    class="my-0 rounded-md"
    controls playsinline preload="none"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film-light-poster.jpg" style="background:url('data:image/webp;base64,UklGRoIAAABXRUJQVlA4IHYAAADQBACdASogABIAPw1uqUemIyIYBABjEMT0gAc4DbW6V3D/YCrZgv8AsVgfEQtYAAD+77ncv/xfKWHnvcUR0S+hsPg7hFdUPAZo9hZKq5p8p/3q6SK+cmbz04KNDvyW45u0Ws/43dQYB5jO2XsnWh2nnV30wAAA') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="用旁白介绍这篇文章的短视频">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film-light.webm" type="video/webm">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_en.vtt" srclang="en" label="English">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_ja.vtt" srclang="ja" label="日本語">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_ko.vtt" srclang="ko" label="한국어">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_vi.vtt" srclang="vi" label="Tiếng Việt">
    <track kind="subtitles" src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktrees-film_zh-cn.vtt" srclang="zh-cn" label="简体中文" default>
  </video>
  </div>
  <script>(function(){if(window.mcFilm)return window.mcFilm();var a={play:'<path d="M8 5.6v12.8a1 1 0 0 0 1.52.85l10.2-6.4a1 1 0 0 0 0-1.7L9.52 4.75A1 1 0 0 0 8 5.6z"/>',pause:'<rect x="6.5" y="5" width="4" height="14" rx="1.2"/><rect x="13.5" y="5" width="4" height="14" rx="1.2"/>',volume:'<path d="M4 9.5v5a1 1 0 0 0 1 1h3l4.4 3.6a.6.6 0 0 0 1-.47V5.37a.6.6 0 0 0-1-.47L8 8.5H5a1 1 0 0 0-1 1z"/><path d="M16.5 8.5a5 5 0 0 1 0 7M19 6a8.5 8.5 0 0 1 0 12" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>',muted:'<path d="M4 9.5v5a1 1 0 0 0 1 1h3l4.4 3.6a.6.6 0 0 0 1-.47V5.37a.6.6 0 0 0-1-.47L8 8.5H5a1 1 0 0 0-1 1z"/><path d="M16.5 9.5l5 5m0-5l-5 5" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>',cc:'<rect x="3" y="5.5" width="18" height="13" rx="3" fill="none" stroke="currentColor" stroke-width="1.8"/><path d="M10.5 10.3a2.2 2.2 0 1 0 0 3.4M17 10.3a2.2 2.2 0 1 0 0 3.4" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/>',full:'<path d="M4.5 9V5.5a1 1 0 0 1 1-1H9M15 4.5h3.5a1 1 0 0 1 1 1V9M19.5 15v3.5a1 1 0 0 1-1 1H15M9 19.5H5.5a1 1 0 0 1-1-1V15" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round"/>',exit:'<path d="M9 4.5V8a1 1 0 0 1-1 1H4.5M19.5 9H16a1 1 0 0 1-1-1V4.5M15 19.5V16a1 1 0 0 1 1-1h3.5M4.5 15H8a1 1 0 0 1 1 1v3.5" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round"/>'},s={en:["Play","Pause","Mute","Unmute","Subtitles","Off","Full screen","Exit full screen","Seek"],ko:["재생","일시정지","음소거","음소거 해제","자막","끄기","전체 화면","전체 화면 종료","탐색"],ja:["再生","一時停止","ミュート","ミュート解除","字幕","オフ","全画面","全画面を終了","シーク"],zh:["播放","暂停","静音","取消静音","字幕","关闭","全屏","退出全屏","进度"],vi:["Phát","Tạm dừng","Tắt tiếng","Bật tiếng","Phụ đề","Tắt","Toàn màn hình","Thoát toàn màn hình","Tua"]},r=(document.documentElement.lang||"en").toLowerCase().slice(0,2),e=s[r]||s.en,t=function(e){return'<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">'+a[e]+"</svg>"},o={},i=function(e,t){if(o[e]||typeof window.gtag!="function")return;o[e]=!0;var n=(t.currentSrc||(t.querySelector("source")||{}).src||"").split("/").pop();window.gtag("event",e,{film:n.replace(/-light(?=\.)/,"")})},n=function(e){(!isFinite(e)||e<0)&&(e=0),e=Math.floor(e);var t=Math.floor(e/3600),n=Math.floor(e%3600/60),s=String(e%60).padStart(2,"0");return t?t+":"+String(n).padStart(2,"0")+":"+s:n+":"+s};function c(s){var a,r,c,d,u,m,p,g,v,b,j,_,w,O,C,E,k,A,o=s.querySelector("video");if(!o)return;s.classList.add("is-ready"),o.controls=!1,o.removeAttribute("controls"),s.insertAdjacentHTML("beforeend",'<button type="button" class="mc-film__big" aria-label="'+e[0]+'">'+t("play")+'</button><div class="mc-film__bar"><button type="button" class="mc-film__btn" data-act="play" aria-label="'+e[0]+'">'+t("play")+'</button><span class="mc-film__time"><span data-cur>0:00</span><span class="mc-film__of"> / </span><span data-dur>0:00</span></span><input class="mc-film__seek" type="range" min="0" max="1000" step="1" value="0" aria-label="'+e[8]+'"><span class="mc-film__cc"><button type="button" class="mc-film__btn" data-act="cc" aria-label="'+e[4]+'" aria-haspopup="true" aria-expanded="false">'+t("cc")+'</button><span class="mc-film__menu" role="menu" hidden></span></span><button type="button" class="mc-film__btn" data-act="mute" aria-label="'+e[2]+'">'+t("volume")+'</button><button type="button" class="mc-film__btn" data-act="full" aria-label="'+e[6]+'">'+t("full")+"</button></div>"),A=s.querySelector(".mc-film__big"),a=s.querySelector(".mc-film__bar"),j=a.querySelector('[data-act="play"]'),p=a.querySelector('[data-act="mute"]'),u=a.querySelector('[data-act="full"]'),d=a.querySelector('[data-act="cc"]'),r=a.querySelector(".mc-film__menu"),c=a.querySelector(".mc-film__seek"),w=a.querySelector("[data-cur]"),O=a.querySelector("[data-dur]"),g=!1,v=0,b=function(){return Array.prototype.filter.call(o.textTracks,function(e){return e.kind==="subtitles"||e.kind==="captions"})},b().length||(d.parentNode.hidden=!0),_=null;function f(e){var n,i,t=!s.classList.contains("is-idle")&&s.classList.contains("is-started");if(e!==!0&&t===_)return;_=t,n=o.clientHeight,i=n?Math.max(40,100-(a.offsetHeight+22)/n*100):85,b().forEach(function(e){if(e.mode!=="showing"||!e.cues)return;for(var n,s=0;s<e.cues.length;s++)n=e.cues[s],t?(n.snapToLines=!1,n.line=i,n.lineAlign="end"):(n.snapToLines=!0,n.line="auto",n.lineAlign="start")})}m=function(){f(!0)},window.addEventListener("resize",m),Array.prototype.forEach.call(o.querySelectorAll("track"),function(e){e.addEventListener("load",m)});function y(){o.paused||o.ended?o.play().catch(function(){}):o.pause()}function l(){s.classList.remove("is-idle"),f(),clearTimeout(v),v=setTimeout(function(){!o.paused&&r.hidden&&!a.querySelector(":focus-visible")&&(s.classList.add("is-idle"),f())},2600)}function M(){clearTimeout(v),h(),s.classList.add("is-idle"),f()}function h(){r.hidden=!0,d.setAttribute("aria-expanded","false")}function F(){var t=b(),n=t.filter(function(e){return e.mode==="showing"})[0];r.innerHTML="",[null].concat(t).forEach(function(s){var o=document.createElement("button");o.type="button",o.setAttribute("role","menuitemradio"),o.setAttribute("aria-checked",String(s?s===n:!n)),o.textContent=s?s.label||s.language:e[5],o.addEventListener("click",function(){t.forEach(function(e){e.mode=e===s?"showing":"disabled"}),h(),setTimeout(m,0),l()}),r.appendChild(o)}),r.hidden=!1,d.setAttribute("aria-expanded","true")}function x(){var n=!o.paused&&!o.ended;s.classList.toggle("is-playing",n),j.innerHTML=t(n?"pause":"play"),j.setAttribute("aria-label",n?e[1]:e[0]),n||l()}function S(){if(w.textContent=n(o.currentTime),!g&&o.duration){var e=o.currentTime/o.duration*1e3;c.value=e,c.style.setProperty("--p",e/10+"%")}}A.addEventListener("click",function(){o.play().catch(function(){})}),C="mouse",s.addEventListener("pointerdown",function(e){C=e.pointerType}),o.addEventListener("click",function(){if(!s.classList.contains("is-started"))return o.play().catch(function(){});if(C==="mouse")return y();s.classList.contains("is-idle")?l():M()}),j.addEventListener("click",y),o.addEventListener("play",function(){s.classList.contains("is-started")||i("film_play",o),s.classList.add("is-started"),x(),l()}),"IntersectionObserver"in window&&(E=new IntersectionObserver(function(e){if(!e.some(function(e){return e.isIntersecting}))return;E.disconnect(),i("film_view",o)},{threshold:.5}),E.observe(s)),o.addEventListener("pause",x),o.addEventListener("ended",x),o.addEventListener("timeupdate",S),o.addEventListener("durationchange",function(){O.textContent=n(o.duration),S()}),o.addEventListener("loadedmetadata",function(){O.textContent=n(o.duration)}),o.addEventListener("volumechange",function(){p.innerHTML=t(o.muted?"muted":"volume"),p.setAttribute("aria-label",o.muted?e[3]:e[2])}),p.addEventListener("click",function(){o.muted=!o.muted}),c.addEventListener("input",function(){g=!0,c.style.setProperty("--p",c.value/10+"%"),o.duration&&(o.currentTime=c.value/1e3*o.duration,w.textContent=n(o.currentTime))}),c.addEventListener("change",function(){g=!1}),d.addEventListener("click",function(e){e.stopPropagation(),r.hidden?F():h()}),document.addEventListener("click",function(e){!r.hidden&&!r.contains(e.target)&&h()}),u.addEventListener("click",function(){document.fullscreenElement||document.webkitFullscreenElement?(document.exitFullscreen||document.webkitExitFullscreen).call(document):s.requestFullscreen?s.requestFullscreen().catch(function(){}):s.webkitRequestFullscreen?s.webkitRequestFullscreen():o.webkitEnterFullscreen&&o.webkitEnterFullscreen()}),k=function(){var n=(document.fullscreenElement||document.webkitFullscreenElement)===s;s.classList.toggle("is-fullscreen",n),u.innerHTML=t(n?"exit":"full"),u.setAttribute("aria-label",n?e[7]:e[6])},document.addEventListener("fullscreenchange",k),document.addEventListener("webkitfullscreenchange",k),s.addEventListener("pointermove",function(e){e.pointerType==="mouse"&&l()}),s.addEventListener("focusin",l),s.addEventListener("keydown",function(e){if(e.target===c&&/^Arrow/.test(e.key))return;var t=e.key.toLowerCase();if(t==="k"||t===" "&&e.target===o)y();else if(t==="arrowleft")o.currentTime=Math.max(0,o.currentTime-5);else if(t==="arrowright")o.currentTime=Math.min(o.duration||0,o.currentTime+5);else if(t==="m")o.muted=!o.muted;else if(t==="f")u.click();else if(t==="escape"&&!r.hidden)h();else return;e.preventDefault(),l()}),o.tabIndex=0}window.mcFilm=function(){document.querySelectorAll(".mc-film:not(.is-ready)").forEach(c)},window.mcFilm()})()</script></div>
  <script>
  new MutationObserver(() => {
    const dark = document.documentElement.classList.contains("dark");
    document.querySelectorAll(".film-pair").forEach((pair) => {
      const from = pair.querySelector(dark ? ".film-light video" : ".film-dark video");
      const to = pair.querySelector(dark ? ".film-dark video" : ".film-light video");
      if (!from || !to || !from.currentTime) return;
      const playing = !from.paused;
      from.pause();
      to.currentTime = from.currentTime;
      [...from.textTracks].forEach((track, i) => { if (to.textTracks[i]) to.textTracks[i].mode = track.mode; });
      if (playing) to.play().catch(() => {});
    });
  }).observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
  </script><figcaption>这篇文章的八分钟视频版。旁白是英文，配有中文字幕。部分视频素材来自 Coverr。</figcaption>
</figure>


<h2 class="relative group">为什么一个检出目录撑不住多个并行智能体
    <div id="为什么一个检出目录撑不住多个并行智能体" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%80%e4%b8%aa%e6%a3%80%e5%87%ba%e7%9b%ae%e5%bd%95%e6%92%91%e4%b8%8d%e4%bd%8f%e5%a4%9a%e4%b8%aa%e5%b9%b6%e8%a1%8c%e6%99%ba%e8%83%bd%e4%bd%93" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>九月的某一天，我在自己做的一个交易平台应用的同一个检出目录里，同时开着五个 Claude Code 会话，结果出了两件事。一个测试配置文件的一行修复，挺过了四次构建失败，等到要提交时却不见了：<code>git commit</code> 说工作区是干净的，文件里装的是另一个会话写的内容。这种丢失的修改，git 里不会留下任何记录：reflog 里没有，stash 里也没有，修改时间看起来也很正常。同一天，一个直接拿工作区构建的部署脚本，差点把另一个会话没做完的改动发到了生产环境。</p>
<p>十月又出了第三件事。一个会话在另一个会话未暂存的改动旁边提交自己的文件，pre-commit 钩子在运行期间把所有未暂存的改动<em>全部</em> stash 了。另一个会话的修改在这段时间里从磁盘上消失，类型检查随后在它新建的文件上报错，提交也中止了。再加上所有会话都用我的名字提交，<code>git log</code> 根本看不出是谁做了什么。</p>
<p>用 worktree 的理由一句话就能说清：有多个写入者的工作区就是竞态，输的一方连自己输了都不知道。给每个智能体一个独立的工作区，竞态也就没了。</p>
<figure class="diagram">
  <div role="img" aria-label="示意图：一个共享的 .git 目录，里面有提交、分支、配置，以及每个 worktree 一个的小型私有目录，连向三个工作目录：main 分支上的主检出目录（有 .env 和 node_modules），用 git worktree add 创建、在 fix-login 分支上的检出（两样都没有），用 claude -w 创建、在 worktree-add-search 分支上的检出（.env 由 .worktreeinclude 复制，会话运行期间被锁定）"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 882" aria-hidden="true" focusable="false">
<path class="d-group" d="M30,267 V735 M30,435.5 H62 M30,578.5 H62 M30,735 H62"></path>
<text class="d-title d-mono" x="2" y="30" font-size="28">.git</text>
<text class="d-sub" x="2" y="57" font-size="18">所有 worktree 共用</text>
<rect class="d-panel" x="2" y="75" width="696" height="192" rx="22"></rect>
<rect class="d-head" x="25" y="105" width="8" height="8" rx="2"></rect>
<text class="d-label" x="46" y="115.8" font-size="19">提交和对象</text>
<rect class="d-head" x="25" y="137" width="8" height="8" rx="2"></rect>
<text class="d-label" x="46" y="147.8" font-size="19">分支和标签（refs/）</text>
<rect class="d-head" x="25" y="169" width="8" height="8" rx="2"></rect>
<text class="d-label" x="46" y="179.8" font-size="19">配置、远程、钩子</text>
<rect class="d-head" x="25" y="201" width="8" height="8" rx="2"></rect>
<text class="d-label" x="46" y="211.8" font-size="19">worktrees/：每个 worktree 一个小的私有目录（各自的 HEAD 和</text>
<text class="d-label" x="46" y="239.8" font-size="19">index）</text>
<text class="d-title" x="62" y="327" font-size="26">三个工作目录</text>
<text class="d-sub" x="62" y="354" font-size="18">文件、HEAD 和 index 各自一份</text>
<rect class="d-box" x="62" y="372" width="636" height="127" rx="14"></rect>
<text class="d-label d-mono" x="82" y="409.2" font-size="20">~/code/myapp</text>
<rect class="d-panel" x="82" y="424" width="60.1" height="28" rx="14"></rect>
<text class="d-label d-mono" x="94" y="443.4" font-size="15">main</text>
<text class="d-sub" x="154.1" y="444.1" font-size="17">主检出目录</text>
<path class="d-badge" style="fill:none;stroke-width:2.6" d="M83,474 l4.5,4.5 l9.5,-10"></path>
<text class="d-label" x="107" y="479.6" font-size="17">有 .env</text>
<path class="d-badge" style="fill:none;stroke-width:2.6" d="M190.1,474 l4.5,4.5 l9.5,-10"></path>
<text class="d-label" x="214.1" y="479.6" font-size="17">node_modules 45 MB</text>
<rect class="d-box" x="62" y="515" width="636" height="127" rx="14"></rect>
<text class="d-label d-mono" x="82" y="552.2" font-size="20">~/code/myapp-fix-login</text>
<rect class="d-panel" x="82" y="567" width="105.3" height="28" rx="14"></rect>
<text class="d-label d-mono" x="94" y="586.4" font-size="15">fix-login</text>
<text class="d-sub" x="199.3" y="587.1" font-size="17">用 git worktree add 创建</text>
<path class="d-line" style="fill:none;stroke-width:2.2" d="M85,611.5 l10,10 M95,611.5 l-10,10"></path>
<text class="d-sub" x="107" y="622.6" font-size="17">没有 .env</text>
<path class="d-line" style="fill:none;stroke-width:2.2" d="M208.1,611.5 l10,10 M218.1,611.5 l-10,10"></path>
<text class="d-sub" x="230.1" y="622.6" font-size="17">没有 node_modules（npm install 之前 84 KB）</text>
<rect class="d-box" x="62" y="658" width="636" height="154" rx="14"></rect>
<text class="d-label d-mono" x="82" y="695.2" font-size="20">~/code/myapp/.claude/worktrees/add-search</text>
<rect class="d-panel" x="82" y="710" width="195.6" height="28" rx="14"></rect>
<text class="d-label d-mono" x="94" y="729.4" font-size="15">worktree-add-search</text>
<text class="d-sub" x="289.6" y="730.1" font-size="17">用 claude -w add-search 创建</text>
<path class="d-badge" style="fill:none;stroke-width:2.6" d="M83,760 l4.5,4.5 l9.5,-10"></path>
<text class="d-label" x="107" y="765.6" font-size="17">.env（由 .worktreeinclude 复制）</text>
<path class="d-line" style="fill:none;stroke-width:2.2" d="M401.1,754.5 l10,10 M411.1,754.5 l-10,10"></path>
<text class="d-sub" x="423.1" y="765.6" font-size="17">没有 node_modules</text>
<rect class="d-line" style="fill:none;stroke-width:2" x="84" y="785.5" width="12" height="9" rx="2"></rect>
<path class="d-line" style="fill:none;stroke-width:2" d="M86.5,785.5 v-2.5 a3.5,3.5 0 0 1 7,0 v2.5"></path>
<text class="d-sub" x="107" y="792.6" font-size="17">会话运行期间被锁定</text>
<text class="d-sub" x="2" y="850" font-size="16">Claude Code 还会在同一仓库的各个 worktree 之间共享两样东西：“don't ask again”的授权</text>
<text class="d-sub" x="2" y="874" font-size="16">（保存在主检出目录的 .claude/settings.local.json）和项目级插件。</text>
</svg>
</div><figcaption>一个仓库，三个工作目录。历史只存一份；文件、HEAD 和 index 每个 worktree 各有一份。</figcaption>
</figure>


<h2 class="relative group">claude -w 的用法和行为
    <div id="claude--w-的用法和行为" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#claude--w-%e7%9a%84%e7%94%a8%e6%b3%95%e5%92%8c%e8%a1%8c%e4%b8%ba" aria-label="锚点">#</a>
    </span>
    
</h2>
<figure><video
    class="my-0 rounded-md"
    autoplay loop muted playsinline preload="metadata"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/claude-worktree.jpg" style="background:url('data:image/webp;base64,UklGRkoAAABXRUJQVlA4ID4AAACQAwCdASogABIAPw16sU+mI6IhKAgAxiGJ6QDC+BPdNCPmtcKAAP7ozOlPLz658ebe527EEQOHuoyZaAAAAA==') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="终端：cat .worktreeinclude 显示 .env；claude -w add-search 回答说自己在 worktree-add-search 分支上；git worktree list 显示新的 worktree 处于 locked 状态；ls 显示 .env 已复制，但没有 node_modules；git worktree remove 因为一个进程已退出的 claude 会话留下的锁而拒绝删除；先 unlock 再 remove 就成功了">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/claude-worktree.mp4" type="video/mp4">
  </video><figcaption>在一个用完即删的仓库里真实运行的过程。注意会话结束后锁还在。</figcaption>
</figure>

<p>下面是<a href="https://code.claude.com/docs/en/worktrees"  target="_blank" rel="noreferrer">官方文档 worktrees 页面</a>里写的内容，加上我自己实际试过的结果：</p>
<ul>
<li><strong>位置和分支。</strong> <code>claude -w add-search</code> 会在新分支 <code>worktree-add-search</code> 上创建 <code>.claude/worktrees/add-search/</code>。不写名字的话，会自动生成一个像 <code>bright-running-fox</code> 这样的名字。记得把 <code>.claude/worktrees/</code> 加进 <code>.gitignore</code>，不然主检出目录里会把这些全显示成未跟踪文件。</li>
<li><strong>从哪里切出来（base ref）。</strong> <code>worktree.baseRef</code> 默认是 <code>&quot;fresh&quot;</code>，也就是从远程默认分支（通常是 <code>origin/main</code>）切。如果仓库最近 24 小时没 fetch 过，它会先 fetch 一次，最多等五秒。设成 <code>&quot;head&quot;</code> 的话，新 worktree 从你本地当前的 <code>HEAD</code> 切出来，没 push 的提交也会一起带过去。它不能设成某个分支名；想从特定分支创建，就自己用 <code>git worktree add</code>。</li>
<li><strong>Pull request。</strong> <code>claude --worktree &quot;#1234&quot;</code> 会 fetch 这个 PR 的 head，然后创建 <code>.claude/worktrees/pr-1234</code>。记得加引号，不然 shell 会把 <code>#</code> 后面当成注释。</li>
<li><strong>信任确认。</strong> 在一个从没开过 Claude 的目录里第一次运行交互式的 <code>claude -w</code>，会报错 &ldquo;Workspace trust not yet accepted&rdquo;。在那个目录里先普通地运行一次 <code>claude</code>，接受信任对话框就好。<code>claude -p -w</code> 会跳过这一步。</li>
<li><strong>围栏。</strong> 会话待在 worktree 里的时候，Claude Code 会拒绝修改主检出目录里的文件，拒绝以主检出目录为工作目录的命令，也拒绝通过 <code>git -C</code>、<code>--git-dir</code> 或 <code>GIT_DIR</code> 指回主检出目录的 git 操作。文档也写明了它拦不住什么：往主检出目录里写东西的 <code>cp</code> 或 shell 重定向，这些检查不会拦。</li>
</ul>
<p>worktree 和主检出目录共享什么，和不共享什么一样重要。所有 worktree 都写同一个 <code>.git</code>，所以在任何一个里面做的提交和分支，在所有 worktree 里都看得到。项目级插件在哪里都会加载。还有，在 worktree 里回答的 &ldquo;Yes, and don&rsquo;t ask again&rdquo;，会保存到<em>主</em>检出目录的 <code>.claude/settings.local.json</code> 里。所以这条授权对所有 worktree 都生效，删掉当初授权的那个 worktree 之后也还在。这很方便，也多了一个理由：别把密钥写进你要批准的命令里。</p>

<h2 class="relative group">清理，以及留下来的锁
    <div id="清理以及留下来的锁" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e6%b8%85%e7%90%86%e4%bb%a5%e5%8f%8a%e7%95%99%e4%b8%8b%e6%9d%a5%e7%9a%84%e9%94%81" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>退出交互式 worktree 会话时，Claude Code 会检查删掉会不会丢东西。没有名字的会话、worktree 又是干净的，会连同分支一起自动删除。有名字的会话会先问你。如果里面还有工作，它会问你要不要保留，之后可以用它打印出来的 <code>claude --worktree &lt;名字&gt; --resume</code> 回来接着干。</p>
<p>非交互运行就不一样了，这也是我试的时候唯一被绊了一下的地方。<code>claude -p</code> 退出时没有确认步骤，所以不会清理。另外，Claude Code 在会话运行期间会给 worktree 加一把 <code>git worktree lock</code>，<code>-p</code> 跑完之后，这把锁还挂着一个已经不存在的进程 ID。上面的录屏里也能看到：<code>git worktree remove</code> 报错 &ldquo;cannot remove a locked working tree, lock reason: claude session add-search&rdquo;。文档写的也正是这样：等后面某个会话清理过期锁时会释放，或者先运行 <code>git worktree unlock</code>。知道了就没什么，第一次看到还是会愣一下。</p>

<h2 class="relative group">.worktreeinclude：全新检出里没有的文件
    <div id="worktreeinclude全新检出里没有的文件" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#worktreeinclude%e5%85%a8%e6%96%b0%e6%a3%80%e5%87%ba%e9%87%8c%e6%b2%a1%e6%9c%89%e7%9a%84%e6%96%87%e4%bb%b6" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>worktree 里只有被跟踪的文件。想把被 git 忽略的文件也带过去，就在仓库根目录放一个 <code>.worktreeinclude</code>。它用 <code>.gitignore</code> 的语法，Claude Code 只复制既匹配规则、<em>又</em>被 git 忽略的文件，所以被跟踪的文件不会重复复制：</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl"># .worktreeinclude
</span></span><span class="line"><span class="cl">.env
</span></span><span class="line"><span class="cl">.env.local
</span></span><span class="line"><span class="cl">config/secrets.json</span></span></code></pre></div></div>
<p>它对 Claude Code 用 git 创建的所有 worktree 都生效：<code>claude -w</code>、子智能体的 worktree，以及桌面应用的并行会话。对你自己用 <code>git worktree add</code> 创建的 worktree 不生效；如果你用 <code>WorktreeCreate</code> 钩子替换了创建流程，它会被整个跳过。</p>
<p>我建议别只放 <code>.env</code>，因为最容易被忘掉的被忽略文件，往往不会大声报错。我的应用把部署脚本改成了从干净的 worktree 构建，这样其他会话没提交的修改就永远不会被发出去。可是那个干净的工作区里，缺了某个功能要用的、被 git 忽略的签名证书。镜像照样构建成功，那个功能在生产环境里失败了 13 天，直到脚本改成把证书复制进去、缺一个就报警为止。另一次从 worktree 部署时，缺的是一个被忽略的主题目录，构建是在一个两分钟的导出把工作区弄脏<em>之后</em>才失败的。在把任何要上线的事情交给 worktree 之前，先在主检出目录里运行 <code>git status --short --ignored</code>，想想其中哪些文件是构建要读的。</p>

<h2 class="relative group">node_modules：最贵的部分
    <div id="node_modules最贵的部分" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#node_modules%e6%9c%80%e8%b4%b5%e7%9a%84%e9%83%a8%e5%88%86" aria-label="锚点">#</a>
    </span>
    
</h2>
<p><code>.worktreeinclude</code> 是给小文件用的。它帮不了 <code>node_modules</code>，Claude Code 也没有在 worktree 之间共享它的机制，所以每个需要运行代码的 worktree，都得有自己的一份。</p>
<figure><video
    class="my-0 rounded-md"
    autoplay loop muted playsinline preload="metadata"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktree-basics.jpg" style="background:url('data:image/webp;base64,UklGRkgAAABXRUJQVlA4IDwAAADQAwCdASogABIAPw18sk+mI6IhKAgAxiGJ6QC/OBPdOMkqQtYu1AAA/ujR5WBjJmvERft5/vglvvCMAAA=') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="终端：git worktree add 创建了新分支上的 myapp-fix-login，以及 detached 在 origin/main 上的 myapp-hotfix；git worktree list 列出三个；新 worktree 里既没有 .env 也没有 node_modules；du 显示主检出目录 45M、worktree 84K，npm install 之后两个都是 45M">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/worktree-basics.mp4" type="video/mp4">
  </video><figcaption>在演示仓库里直接用 git worktree：装依赖之前 84 KB，装完又是完整的 45 MB。</figcaption>
</figure>

<p>在真实项目里，这些数字就不好看了。我的移动应用仓库在主检出目录旁边有 19 个 worktree。我量了其中 12 个，从 194 MB 到 12 GB 不等，最大的四个正好就是自带 <code>node_modules</code> 的那四个。这周我笔记本的磁盘用到了 96%，要清理的话，就该从它们开始。</p>
<p>几种做法，从简单到巧妙：</p>
<ol>
<li><strong>每个 worktree 单独安装。</strong> 让智能体一开始就跑 <code>npm install</code>，或者自己装。慢，占磁盘，但不会给你惊喜。</li>
<li><strong>从 lockfile 相同的检出目录克隆。</strong> 在 macOS 上，<code>cp -c -R ../myapp/node_modules .</code> 用的是 APFS 克隆，两边在任何一方改动之前共享数据块。克隆演示里的 45 MB 只用了 0.8 秒。我在真实的 worktree 部署里也用过这个办法。只在 lockfile 一致时克隆，原生构建目录要小心：我的主检出目录里的 iOS Pods 是为比 worktree 分支更旧的 SDK 构建的，所以那部分只能重新安装。</li>
<li><strong>用 pnpm。</strong> 它的包都放在一个 store 里，macOS 上的 pnpm <a href="https://pnpm.io/settings/node-modules#packageimportmethod"  target="_blank" rel="noreferrer">会先尝试克隆，再尝试硬链接</a>（Linux 和 Windows 上是先硬链接）。在演示里，第二个 worktree 的安装大约五秒就完成了。</li>
<li><strong>在 <code>WorktreeCreate</code> 钩子里安装。</strong> 这个钩子会替换 Claude Code 默认的 git 逻辑，所以可以先 <code>git worktree add</code>，再跑安装。这个我还没试过。如果你要试，记住 <code>.worktreeinclude</code> 不会再被处理，<code>.env</code> 也得在钩子里自己复制。</li>
</ol>

<h2 class="relative group">在 tmux 里跑并行智能体
    <div id="在-tmux-里跑并行智能体" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e5%9c%a8-tmux-%e9%87%8c%e8%b7%91%e5%b9%b6%e8%a1%8c%e6%99%ba%e8%83%bd%e4%bd%93" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>我本来就住在 tmux 里（布局写在<a href="https://jared.lynskey.co.nz/zh-cn/posts/2026/2026-10-22-claude-code-day-to-day/" >我每天怎么用 Claude Code</a>那篇里），所以 worktree 直接就能放进去：一个 worktree 一个窗口，或者一个窗格。下面是同时启动的三个智能体，每个都被要求改同一个文件：</p>
<figure><video
    class="my-0 rounded-md"
    autoplay loop muted playsinline preload="metadata"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/parallel-agents-tmux.jpg" style="background:url('data:image/webp;base64,UklGRlQAAABXRUJQVlA4IEgAAACQBACdASogABIAPw10rE0mIyIhKAqoxiGJ6QDImBPdLsmKk7T7Naw9bcwBsAAA/ui+5wOzf0YXxkY1UgYA04COTxtokeJAAAA=') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="分成三个窗格的 tmux：claude -w add-bye、add-greet、add-tests 同时运行，运行期间窗格标题变成各自 worktree 的名字；两个报告修改了 src/index.js，一个报告新建了测试目录；最后上面的窗格显示主检出目录是干净的，还有三个被锁定的 worktree">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/parallel-agents-tmux.mp4" type="video/mp4">
  </video><figcaption>三个智能体，一个文件，没有冲突。主检出目录保持干净，每个改动都在自己的分支里等着。</figcaption>
</figure>

<p>其中两个同时改了 <code>src/index.js</code>，谁也没察觉到对方，因为它们各自的 <code>src/index.js</code> 在磁盘上是不同的文件。主检出目录也一直是干净的。最后留给你的，是三个要审查、要合并的分支，这是 worktree 替你做不了的事：两个智能体改了同一行，照样会有合并冲突。只不过它不再是悄无声息的覆盖，而是出现在你看得见的合并环节。</p>
<p>在 VS Code 里，worktree 就是一个普通文件夹：<code>code .claude/worktrees/add-search</code> 会在一个新窗口里打开它，用的是它自己的分支。</p>

<h2 class="relative group">worktree 是一张快照
    <div id="worktree-是一张快照" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#worktree-%e6%98%af%e4%b8%80%e5%bc%a0%e5%bf%ab%e7%85%a7" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>worktree 也会过时。11 月 19 日，一个两三天前就准备好的 worktree 发出了一次 OTA 更新，结果悄悄回退了这期间从别处发出的两个修复。当天就发现并替换掉了，但 worktree 隔离得太好，问题恰恰在这：它从来没看到过那两个修复。其实一行 git 命令就能在发布前发现：</p>
<figure><video
    class="my-0 rounded-md"
    autoplay loop muted playsinline preload="metadata"
    poster="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/stale-worktree-check.jpg" style="background:url('data:image/webp;base64,UklGRkgAAABXRUJQVlA4IDwAAACwAwCdASogABIAPw16sk+mI6IhKAgAxiGJ6QDEJBPdNJGDbpnawAD+6TSngiBlgQG6+gLgAZ5HH8TQAAA=') center/cover no-repeat"
    width="1280"
    height="720"
    aria-label="终端：main 上有一个新提交 &#39;Fix sign-in token handling&#39;；一个旧的 worktree 还停在第一个提交上；git merge-base --is-ancestor main HEAD 输出 &#39;missing the fix: rebase before you ship&#39;；执行 git rebase main 之后，同样的检查输出 &#39;has the fix&#39;">
    <source src="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/stale-worktree-check.mp4" type="video/mp4">
  </video><figcaption>只要不是刚切出来的 worktree，发布前都该跑一下这个检查。</figcaption>
</figure>

<p><code>baseRef: &quot;fresh&quot;</code> 能让新 worktree 保持最新。可没有什么能让旧的 worktree 保持最新，所以从旧 worktree 发布之前，先检查一下。</p>

<h2 class="relative group">子智能体和钩子
    <div id="子智能体和钩子" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e5%ad%90%e6%99%ba%e8%83%bd%e4%bd%93%e5%92%8c%e9%92%a9%e5%ad%90" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>要得到这种隔离，并不需要给每个智能体开一个终端。在 front matter 里写上 <code>isolation: worktree</code> 的自定义子智能体，每次都会在自己的临时 worktree 里运行：</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-markdown" data-lang="markdown"><span class="line"><span class="cl"><span class="nn">---</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">refactorer</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">description</span><span class="p">:</span><span class="w"> </span><span class="l">Applies mechanical refactors across many files</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">isolation</span><span class="p">:</span><span class="w"> </span><span class="l">worktree</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nn">---</span><span class="w">
</span></span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">Apply the requested refactor across every affected file, then run the tests
</span></span><span class="line"><span class="cl">and report the results.</span></span></code></pre></div></div>
<p>这是<a href="https://code.claude.com/docs/en/worktrees#isolate-subagents-with-worktrees"  target="_blank" rel="noreferrer">文档</a>里的例子。子智能体没做任何改动就结束的话，它的 worktree 会被删除；有改动的会一直保留，直到定期清理能在不丢工作的前提下删掉它。有个细节：在 <code>.claude/worktrees/</code> 下的 worktree 里运行的子智能体，指令来自你的主对话，不会加载 worktree 根目录里的 <code>CLAUDE.md</code>。</p>
<p><code>WorktreeCreate</code> 和 <code>WorktreeRemove</code> 钩子会整个替换 git 逻辑。它们本来是给其他版本控制系统用的，或者用来把 worktree 放在 <code>.claude/worktrees/</code> 以外的地方，不过像上面那样的安装步骤，也可以放在这里。</p>

<h2 class="relative group">什么时候直接用 git worktree add 更好
    <div id="什么时候直接用-git-worktree-add-更好" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e7%9b%b4%e6%8e%a5%e7%94%a8-git-worktree-add-%e6%9b%b4%e5%a5%bd" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>坦白说，我那个应用仓库里的 <code>.claude/worktrees</code> 目录是空的。它的 19 个 worktree 全是用普通的 <code>git worktree add</code> 建的，全是最近一个月建的，一半处于 detached HEAD 状态。笔记本上其他仓库里还有 40、40、18、10、3、2、2 个。而且大多数不是我建的：这台笔记本上的 638 份 Claude Code 对话记录里，有 85 份是智能体在运行 <code>git worktree add</code>，加起来一共 336 次。</p>
<p>直接用 git 更好的情况：</p>
<ul>
<li><strong>需要某个已有的分支时，</strong> 比如为热修复保留的发布分支。<code>baseRef</code> 做不到。</li>
<li><strong>只想发出一个修复时。</strong> 其他会话已经往 <code>main</code> 提交了还没部署的工作，所以我在生产环境正在运行的那个提交上建了一个 detached worktree，只把我的修复 cherry-pick 进去，从那里部署。这样发出去的就只有这一个修复。</li>
<li><strong>自己给智能体写任务说明时。</strong> 在一个仓库上同时启动好几个智能体时，我给每个的说明里都会写：用 <code>git worktree add --detach &quot;$TMPDIR/&lt;task&gt;-wt&quot; refs/remotes/origin/main</code> 建自己的 worktree，用 <code>git push origin HEAD:main</code> 推送，有冲突就 rebase。</li>
<li><strong>想让它一直留着时。</strong> Claude Code 的清理永远不会删除你用 <code>git worktree add</code> 自己建的 worktree。</li>
</ul>
<p><code>claude -w</code> 更适合手动开会话、又想把建分支、复制 <code>.env</code>、围住主检出目录和清理都交给它的时候。两者混着用完全没问题：不管用哪种方式创建，Claude Code 都把它当作 worktree 对待，上面说的和主检出目录的共享也一样。</p>

<h2 class="relative group">简短总结
    <div id="简短总结" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e7%ae%80%e7%9f%ad%e6%80%bb%e7%bb%93" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>每个并行会话用一个 <code>claude -w &lt;名字&gt;</code>，并把 <code>.claude/worktrees/</code> 加进 <code>.gitignore</code>。<code>.worktreeinclude</code> 里不要只写 <code>.env</code>，要把构建需要的被忽略文件都列进去。提前想好 <code>node_modules</code> 怎么办：每个 worktree 单独装、克隆，还是用 pnpm。用 <code>-p</code> 跑完之后，可能会留下一把需要 <code>git worktree unlock</code> 的锁。从不是刚切出来的 worktree 发布之前，先确认它包含了线上已有的内容。worktree 能防止智能体互相覆盖文件，却防不了它们意见不合，最后审查那三个分支的，还是你自己。</p>

<h2 class="relative group">延伸阅读
    <div id="延伸阅读" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e5%bb%b6%e4%bc%b8%e9%98%85%e8%af%bb" aria-label="锚点">#</a>
    </span>
    
</h2>
<p><strong>本站：</strong></p>
<ul>
<li><a href="https://jared.lynskey.co.nz/zh-cn/posts/2026/2026-10-22-claude-code-day-to-day/" >我每天怎么用 Claude Code</a>：tmux、权限模式，以及怎么从手机上用</li>
</ul>
<p><strong>站外：</strong></p>
<ul>
<li>Claude Code 文档里的 <a href="https://code.claude.com/docs/en/worktrees"  target="_blank" rel="noreferrer">Run parallel sessions with worktrees</a>，比大多数文档页写得都细</li>
<li><a href="https://git-scm.com/docs/git-worktree"  target="_blank" rel="noreferrer">git-worktree</a>：哪些是共享的，哪些是每个 worktree 各一份</li>
<li><a href="https://pnpm.io/settings/node-modules#packageimportmethod"  target="_blank" rel="noreferrer">pnpm 的 <code>packageImportMethod</code></a>：克隆和硬链接</li>
</ul>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/featured.jpg" medium="image" type="image/jpeg" />
    </item>
  </channel>
</rss>
