– updated

Git Worktree

As I've understood Git worktrees, there are two main workflows to use.

Tags:

Option 1: Using an existing Git repository

If you already have a normal Git repository locally, the simplest way to use Git worktree is to start there.

To add a new worktree for an existing branch:

git worktree add ../<new-folder-name> <branch>

To create a new branch and worktree simultaneously:

git worktree add -b <new-branch-name> ../<new-branch-name>

You now have a folder for just one branch---an advantage for many cases, e.g.: updating/changing NPM packages, and keeping WIP without committing and fixing a bug for production in a worktree.

Option 2: Creating a new Git repository

If you’re doing a fresh clone, you can clone it as a --bare repository and add worktrees for each branch you’re working on:

git clone --bare <repository> <new-directory-name>
cd <new-directory-name>
# for new branch
git worktree add -b <new-branch-name> ../<new-branch-name> main

Each branch will then be a folder in the Git repository folder.

References: