git worktree
最終更新日
公開日
複数のブランチを同時にチェックしたい時に
目次
某ハッカソンで大量のPRをレビューする必要があり、複数のブランチを行ったり来たりするのが大変だった。
- branch Aをcheckoutして手元で動作確認、画面共有しながら、手元でデバッグしたりする
- branch Bのレビュー依頼が来て、手元で確認したいが、branch Aの作業が途中
- branch Cのレビュー依頼が来て、手元で確認したいが、branch Aの作業が途中
- …
みたいな状況。
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みたいなのを書いていい感じにやる。