🧭

ワンキーでレビュー用 worktree を作成して Cursor で開く(LazyGit+git worktree/macOS)

に公開

概要

Cursor + vim で日々開発していると、コードレビューを始めるたびに「レビュー用の worktree を切る → そのパスで Cursor を開く」という一連の操作が煩雑になりがちです。そこで、LazyGit のブランチ一覧からワンキーで git worktree を作成し、Cursor をそのディレクトリで即起動できる最小構成を整えました。

この記事では、コピペで使える設定、実際の運用手順、注意点とトラブルシュートまでを短くまとめます。

対象と前提

  • gitworktree を利用できる環境であること(一般的な最新環境で可)
  • LazyGit が導入済みであること
  • cursor コマンドが利用可能であること。

ゴール(できること)

  • 小文字の w:選択したリモートブランチから worktree を作成し、そのまま Cursor で開く
  • 大文字の W:ベースブランチから新規ブランチを切って worktree を作成し、Cursor で開く

LazyGit 設定ファイル

LazyGit を起動し、"1 Status" で 'o' または 'e' を押すと設定ファイルをエディタで開いてそのまま修正できます。

customCommands:
  # localBranches: 選択したローカルブランチを指定ディレクトリに worktree 追加し Cursor で開く
  # 説明: ローカルブランチからworktreeを作成して開く
  - key: 'w'
    command: "git worktree add ../{{.Form.Path}} {{.SelectedLocalBranch.Name}} && cursor ../{{.Form.Path}}"
    description: "Open selected branch in new worktree"
    context: "localBranches"
    prompts:
      - type: 'input'
        title: 'New worktree path (directory name)'
        key: 'Path'
        suggestion: '{{.SelectedLocalBranch.Name | replace "/" "-"}}'

  # remoteBranches: 選択したリモートブランチをローカル化してから worktree 追加し Cursor で開く
  # 説明: リモートブランチからworktreeを作成して開く
  - key: 'w'
    command: "bash -lc 'set -e; BR={{.SelectedRemoteBranch.Name}}; DIR=../{{.Form.Path}}; if git show-ref --verify --quiet refs/heads/$BR; then git worktree add \"$DIR\" \"$BR\"; else git fetch --prune; git worktree add -b \"$BR\" \"$DIR\" origin/$BR; fi; git -C \"$DIR\" branch --set-upstream-to=origin/$BR \"$BR\" || true; cursor \"$DIR\"'"
    description: "Open selected remote branch in new worktree (auto create/use local and set upstream)"
    context: "remoteBranches"
    prompts:
      - type: 'input'
        title: 'New worktree path (directory name)'
        key: 'Path'
        suggestion: '{{.SelectedRemoteBranch.Name | replace "/" "-"}}'

  # worktrees: BaseBranch から NewBranch を作成し、指定ディレクトリに worktree 追加して Cursor で開く
  # 説明: 指定したブランチから新しいブランチを作成してworktreeを作成して開く
  - key: 'W'
    command: "git worktree add -b {{.Form.NewBranch}} ../{{.Form.Path}} {{.Form.BaseBranch}} && cursor ../{{.Form.Path}}"
    description: "Create new branch as worktree and open in Cursor"
    context: "worktrees"
    prompts:
      - type: 'input'
        title: 'New worktree path (directory name)'
        key: 'Path'
        suggestion: '{{.Form.NewBranch}}'
      - type: 'input'
        title: 'New branch name'
        key: 'NewBranch'
      - type: 'input'
        title: 'Base branch to create from (e.g., main or master)'
        key: 'BaseBranch'
        initialValue: 'main'

使い方(操作フロー)

  1. LazyGit を開く。
  2. リモートブランチ一覧上で w
    • プロンプトに作成先のディレクトリ名(例: feature-foo)を入力すると、選択したリモートブランチ名をそのままローカルに作成(既存なら再利用)し、../feature-foo に worktree を作成、upstream を設定してから Cursor で開きます。
  3. 既存ローカルブランチに対して同様の操作をしたい場合は、ローカルブランチ一覧でも w を有効化しておきます(上の設定例のとおり)。
  4. 新規ブランチを作って worktree 化したい場合は、worktrees ペイン上で W
    • New branch と Base branch を入力すると、git worktree add -b <new> ../<path> <base> を実行し、そのまま Cursor で開きます。

まとめ

LazyGit のカスタムコマンドを最小限だけ仕込むことで、レビューや検証用の環境を「押して即開く」ワンアクションに統一できます。

Discussion