git worktree

git

書き方

git worktree add <path> [<branch>]

同じリポジトリから二つ目の作業フォルダーを取り出し、クローンし直さずに二つのブランチを並行して扱えます。同じブランチを二か所にチェックアウトはできず、フォルダーを手で消すとpruneまで一覧に残ります。

よく使うオプション

オプション意味
add <path> <branch>check that branch out into another folder
add -b <new> <path>create a branch and check it out there
listshow every working tree of this repository
remove <path>delete a working tree cleanly
pruneforget trees whose folder you deleted by hand
--detachcheck out a commit without a branch

git worktree add ../hotfix hotfix/login

a second folder on another branch, sharing one .git

git worktree add -b release ../rel main

creates release from main in ../rel

git worktree remove ../hotfix

removes the folder and its bookkeeping entry

取り消しに何通りもあるのが混乱のもとです — ブランチの指す先を動かすのと、打ち消すコミットを積むのは別です。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. git worktree は何をしますか。

同じリポジトリから二つ目の作業フォルダーを取り出し、クローンし直さずに二つのブランチを並行して扱えます。同じブランチを二か所にチェックアウトはできず、フォルダーを手で消すとpruneまで一覧に残ります。

Q. どう打ちますか。

git worktree add <path> [<branch>] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは6個です。全部は man git にあります。このコマンドはgitの仲間です。

関連するコマンド

man ページ: man git-worktree