機能徹底解説

Claude Code × Git Worktrees 並行作業ガイド:複数セッションを衝突なしで回す

セッションを claude --worktree で起動すれば、エージェントごとに .claude/worktrees 配下の独立したチェックアウトが手に入る——機能を2つ同時進行、プロンプト1つでランディングページ案を5つ、ブランチ切り替えは不要。

The frontend-dev.md sub-agent file in .claude/agents with YAML front matter showing model haiku and the isolation worktree line that makes every spawn of this agent run in its own git worktree
frontend-dev.md のフロントマターに isolation: worktree の一行——このサブエージェントは呼び出されるたびに自分専用の git worktree で動きます。

画像付きの12ステップ解説。読了時間は約4分。各ステップから元動画の該当シーンへ飛べます。

要点まとめ

git worktree は同じリポジトリの別ブランチを別フォルダにチェックアウトする機能で、フォルダ同士は同じ git データを共有します。素の claude の代わりに claude --worktree を実行すると、Claude Code が .claude/worktrees/<ランダム名> にそのセッション専用の worktree を作成。ターミナルを2つ開いても、同じリポジトリを互いに干渉せず編集できます。前提条件は git init 済みでコミットが1件以上あることだけ。サブエージェントはプロンプトで都度 worktree 分離を依頼できるほか、.claude/agents のエージェントファイルのフロントマターに isolation: worktree と書けば恒常的に有効になります。

元動画

このページは Developers Digest による Claude Code worktree デモの録画を辿り、手順を1コマずつ再構成したものです。スクリーンショットはすべて元動画の該当時刻にリンクしています。

スクリーンショットは動画のフレームをクレジット付きで引用したものです。文章は弊社オリジナル。手順は2026年9月に動画と Claude Code 公式ドキュメントに対して検証済み。

guide_claude_code_worktrees.sections.why_worktrees_title

guide_claude_code_worktrees.sections.why_worktrees_subtitle

  1. 1

    worktree とは

    git worktree は同じリポジトリの別ブランチを別フォルダにチェックアウトする仕組みです。各フォルダは自分のファイルだけを編集しますが、git データ自体は共有——feature1 フォルダは Feature-1 ブランチに留まり、main は自分のフォルダで一切影響を受けません。

    Diagram of one Git repository folder connected to three worktree folders labeled ../main, ../feature1 and ../feature2, each folder checked out on its own branch while sharing the same git history
    リポジトリ1つ、フォルダ3つ、ブランチ3本——動画の冒頭で示される図です。0:30 から視聴
  2. 2

    最初のコミットを用意する

    worktree にはブランチを切れるリポジトリが必要です。動画では demo-app フォルダで git init、touch で index.html を作成し、git add と git commit でまとめてコミット。条件はコミットが1件以上あることだけで、あとは Claude Code が処理します。

    Terminal in the demo-app folder running git init, touch index.html, git add . and git commit -m, the first commit a repository needs before claude --worktree will start
    動画で手動で行う準備はこれだけ:init、ファイル1つ、コミット1回。1:10 から視聴

guide_claude_code_worktrees.sections.parallel_sessions_title

guide_claude_code_worktrees.sections.parallel_sessions_subtitle

  1. 3

    claude --worktree でセッション開始

    素の claude ではなく claude --worktree と入力します。ウェルカム画面にこのセッションの居場所が表示されます:demo-app ルートではなく .claude/worktrees/clever-munching-toast。ランダムな名前が並行セッションの衝突を防ぎます。

    Claude Code v2.1.50 welcome screen after running claude --worktree in the demo-app terminal, showing Opus 4.6 - Claude Max and the session rooted at ~/.claude/worktrees/clever-munching-toast
    ウェルカムバナーに、このセッションが編集する自動作成済み worktree の名前が表示されています。1:30 から視聴
  2. 4

    ターミナルをもう1つ開いて再実行

    並行化こそが本丸です。同じディレクトリでもう1つターミナルを開き、claude --worktree を再実行。今度は .claude/worktrees/spicy-napping-otter がルートになります。2つのエージェントが同じリポジトリの index.html を、互いの変更を見ずに同時編集できる状態です。

    A second terminal running claude --worktree in the same demo-app folder, its Claude Code welcome screen rooted at demo-app/.claude/worktrees/spicy-napping-otter so both sessions run in parallel
    同じコマンド、同じリポジトリ、別フォルダ——ワークフローはこれだけ。1:36 から視聴
  3. 5

    1つ目のセッションにタスクを渡す

    1つ目のセッションには「HTML を黒背景の hello world にして」と依頼します。index.html に24行を書き込み、Done と報告。セッションは自分の worktree だけを編集するので、メインのチェックアウトには一切影響しません。

    Claude Code session reporting Write index.html of 24 lines and Done, the page displays hello world centered on a black background with white text, the task assigned to the first worktree session
    セッション1は clever-munching-toast の中に黒背景ページを書き出しました。1:40 から視聴
  4. 6

    2つの結果を並べて比較

    両セッションがページをブラウザで開きます。黒の hello world は clever-munching-toast の URL、紫の hello world は spicy-napping-otter の URL。間に挟まった Finder ウィンドウには .claude/worktrees のフォルダが見え、セッションごとにサブフォルダが1つ。同じリポジトリを2回チェックアウトし、それぞれ別々に変更した状態です。

    Two browser windows opening hello world from the clever-munching-toast worktree URL on a black page and the spicy-napping-otter worktree URL on a purple page, with the .claude/worktrees folder tree visible in Finder between them
    黒と紫のページが、同じリポジトリ配下の2つの worktree URL に共存しています。1:50 から視聴

guide_claude_code_worktrees.sections.subagent_isolation_title

guide_claude_code_worktrees.sections.subagent_isolation_subtitle

  1. 7

    プロンプト1つでエージェントを5つ展開

    worktree 分離はその場で依頼することもできます。プロンプト1つ——サブエージェントを5つ起動し、HTML のクリエイティブな SaaS ランディングページ案を5つ、全員 git worktree 分離で——Claude Code は Running 5 Task agents と応え、Variation 1〜5 と各エージェントのツール使用回数・トークン数を一覧表示します。Ctrl+B で部隊をバックグラウンドへ回すことも可能。

    Claude Code transcript with the prompt to spawn five sub-agents creating creative SaaS landing page variations with git worktree isolation, answering Running 5 Task agents with Variation 1 through 5 and per-agent tool use and token counts
    プロンプト1つ、Task エージェント5体、それぞれが独立した git worktree で動作。2:52 から視聴
  2. 8

    部隊の完了を待つ

    数分後、全バリエーションが Done を報告——各4回のツール使用、約5万トークン。同じファイルの5つの異なるデザインが同時に存在し、メインのチェックアウトには誰も触れず、途中の stash やマージも不要でした。

    Claude Code reporting 5 Task agents finished with each SaaS landing page variation marked Done after 4 tool uses and roughly 50k tokens, all generated in their own isolated git worktrees
    Done が5行:並行エージェントが衝突ゼロで完走しました。4:14 から視聴
  3. 9

    worktree のパスで方向性を選ぶ

    Claude Code は5つのテーマ——Minimal Dark Mode、Gradient Dreamscape、Retro Terminal / Hacker、Soft Pastel / Friendly、3D / Isometric——をまとめ、それぞれの正確なファイルパス(.claude/worktrees/agent-a01ee76a/index.html など)を提示します。採用するなら、その worktree のブランチを残すだけです。

    Claude Code quick overview of five landing page themes, Minimal Dark Mode through 3D Isometric, each with its .claude/worktrees/agent-*/index.html path, beside the rendered FlowSync Ready to move faster preview
    各テーマが .claude/worktrees 配下の1フォルダに対応——見比べて気に入った案だけマージします。4:22 から視聴

guide_claude_code_worktrees.sections.agent_config_title

guide_claude_code_worktrees.sections.agent_config_subtitle

  1. 10

    分離付きサブエージェントを自然言語で頼む

    分離設定はエージェント定義に組み込めます。/clear の後の一文は「フロントエンド開発のサブエージェントを作成して。モデルは Haiku、worktree 分離を有効にして」。Claude Code はまず自前のサブエージェント文書を調べ、それから設定を組み立てます——YAML を手書きする必要はありません。

    A cleared Claude Code session with the request to create a front end developer sub agent using the Haiku model with work tree isolation, while the claude-code-guide agent fetches the sub-agents documentation from code.claude.com
    Haiku と worktree 分離を頼めば、ファイル自体を Claude Code が書いてくれます。5:46 から視聴
  2. 11

    Claude Code がエージェントファイルを書く

    設定はプロジェクトに落ちてきます。mkdir -p で .claude/agents を作り、そこに frontend-dev.md を書き込み。プロジェクト単位のエージェントはこのフォルダ——skills と同じ場所——に置かれるので、定義はリポジトリと一緒に運ばれます。

    Claude Code running mkdir -p on the demo-app/.claude/agents folder after researching the custom agent config, ready to write the frontend-dev.md agent file
    mkdir -p .claude/agents、続いて frontend-dev.md——エージェントの導入はファイル1つ。6:18 から視聴
  3. 12

    並行化を約束するあの一行

    フロントマターにすべてが書かれています:name、description、model: haiku、そして isolation: worktree と、Read・Edit・Write・Bash・Glob・Grep のツールホワイトリスト。今後このエージェントが呼ばれるたび、専用の git worktree が自動で用意されます——フラグも、プロンプトでの前置きも不要です。

    The Write tool creating .claude/agents/frontend-dev.md with YAML front matter listing name frontend-dev, description, model haiku, isolation worktree and the tools Read, Edit, Write, Bash, Glob, Grep
    フロントマターの isolation: worktree——このエージェントの worktree 分離が恒久化されます。6:34 から視聴

よくある質問