git worktree コマンド

git worktree コマンドを紹介する

git worktree add

上では単純に git worktree add <path> でワークツリーを作成したが、実際には特定の branch を指定したり、新しい branch 名を直接指定したりすることもできる。

  • git worktree add <path> <branch> を使用すると、ワークツリーを作成しながら既存の branch を使うように指定できる。
  • git worktree add <path> -b <branch> を使用すると、ワークツリーを作成しながら同時に作成する branch 名を指定できる。
  • git worktree add <path> <commit-ish> を使用すると、現在の HEAD コミットではなく、特定のコミットからワークツリーを作成することもできる。

git worktree list

git worktree list を使用すると、現在作成されているワークツリーの一覧、branch 名、状態などを確認できる。

❯ git worktree list
/Users/sample/Development/project             cbfc60b [main]
/Users/sample/Development/project-hotfix  cbfc60b [project-hotfix] prunable

ワークツリーのパス、現在どのコミットと branch を参照しているかなどを確認できる。

git worktree remove#

git worktree remove は、不要になったワークツリーを削除するためのコマンドである。

❯ git worktree remove project-hotfix
❯ git worktree list
/Users/sample/Development/project  cbfc60b [main]

削除したいワークツリーの名前またはパスを入力すると、ワークツリーを削除できる。このとき branch は削除されない。