Git worktree management for working on multiple branches simultaneously.
Overview
Using Git worktree, you can create multiple working directories from a single repository.
Benefits:
- Develop multiple features in parallel without switching branches
- Can do other work while reviewing a PR
- Proceed with production hotfixes and development simultaneously
Basic commands
Create Worktree
# 新規ブランチで作成
git worktree add ../project-feature-x feature-x
# 既存ブランチで作成
git worktree add ../project-hotfix hotfix/urgent-fix
# リモートブランチをチェックアウト
git worktree add ../project-review origin/feature-y
List Worktrees
git worktree list
Example output:
/path/to/project abc1234 [main]
/path/to/project-feature def5678 [feature-x]
/path/to/project-hotfix ghi9012 [hotfix/urgent-fix]
Delete Worktree
# 作業ディレクトリを削除
rm -rf ../project-feature-x
# Git から登録解除
git worktree prune
Or batch:
git worktree remove ../project-feature-x
Workflow example
1. Hotfix during feature development
# 現在: feature-x ブランチで開発中
# 緊急: 本番バグ発生
# ホットフィックス用 worktree 作成
git worktree add ../project-hotfix -b hotfix/login-fix main
# ホットフィックス作業
cd ../project-hotfix
# ... 修正 ...
git commit -m "fix: resolve login issue"
git push origin hotfix/login-fix
# 元の作業に戻る
cd ../project
# feature-x の作業を継続
2. PR Review
# レビュー対象のブランチを worktree で開く
git fetch origin
git worktree add ../project-review origin/feature-y
# レビュー
cd ../project-review
npm install
npm run dev
# レビュー完了後
cd ../project
git worktree remove ../project-review
Best Practices
Directory naming
project/ # メイン (main)
project-feature-x/ # 機能開発
project-hotfix/ # ホットフィックス
project-review/ # PRレビュー
Regular cleanup
# 不要な worktree を確認
git worktree list
# マージ済みブランチの worktree を削除
git worktree remove ../project-merged-feature
# 孤立した worktree を整理
git worktree prune
Notes
- The same branch cannot be opened in multiple worktrees
- node_modules must be installed separately in each worktree
- .env files also need to be copied
Troubleshooting
"already checked out" error
# 別の worktree で使用中のブランチ
git worktree list # どこで使われているか確認
Orphaned worktree
# ディレクトリを手動削除した場合
git worktree prune
When deleting a branch
# worktree で使用中のブランチは削除できない
# 先に worktree を削除する
git worktree remove ../project-feature
git branch -d feature