git worktree

最終更新日
公開日

複数のブランチを同時にチェックしたい時に

目次

某ハッカソンで大量のPRをレビューする必要があり、複数のブランチを行ったり来たりするのが大変だった。

みたいな状況。

git worktreeを使うと、複数のブランチを並列で確認できるので便利。

$ cd <project root>

$ git worktree list

$ git worktree add <path to new worktree dir> -b <target branch name>

$ cd <path to new worktree dir>
# `cd ...` instead of `git switch ...` to switch branches.

$ git worktree remove <path to new worktree dir>
# remove a worktree.
# only clean (no untracked files and no modification in tracked files) worktrees can be removed.
# unclean worktrees can be removed with `--force` option.
# the main worktree cannot be removed.

$ git worktree prune
# prune worktree information from .git/worktrees

.envなど、gitの追跡対象外のファイルは当たり前だがworktreeにcopyしてくれない。 git-worktree-init.shみたいなのを書いていい感じにやる。