How To Use Git Worktree To Checkout Multiple Branches At Once

git worktree allows you to checkout multiple branches at the same time. It’s somewhat similar to git checkout -b except that it will create the branch at a new path.

For example, running this command in your repository will create a new example-worktree folder outside of your repository and a new git branch named example-worktree

$~ git worktree add ../example-worktree

If you have an existing branch that you want to use worktree with, you need to run the following command instead

$~ git worktree add ../example-worktree $REPLACE_WITH_EXISTING_BRANCH_NAME

To get a list of worktree

To see all available working trees that you have created, you can run

$~ git worktree list

This will print out the following outputs

/ROOT/go-projects/main-project-folder   14e218b [main]
/ROOT/go-projects/example-worktree      14e218b [example-branch]

The first prefix is the worktree’s folder path and the suffix is the name of the branch it’s on

To remove a worktree

I would recommend you to delete a worktree once you’ve done using it. To delete it use the following command:

$~ git worktree remove $WORKTREE_PATH_NAME

Caveats

One thing that I noticed when using worktree is that you can’t checkout to the same branch that’s already in worktree. For example, I can’t checkout to example-branch from main branch.

Conclusion

git worktree is a pretty useful feature but I would recommend you use it sparingly. It can get confusing if you have too many worktree available. I would recommend you to remove it once you’re done with it.

Hopefully you find this helpful as a quick references.