Git Worktree
Git Worktree is a feature that allows you to maintain multiple working trees associated with a single Git repository. Each working tree is an isolated environment with its own working directory and allows you to work on different branches or even different commits simultaneously.
Creating a New Worktree
To create a new worktree, use the following command:
git worktree add <PATH>
Example: Create a feature-x
Directory and Branch
To create a new worktree for a branch and have a directory with the same name, you can run:
git worktree add ../feature-x
Naming the Branch (Optional)
You can give your branch a unique name using the -b
flag with the add
command:
git worktree add -b feature-xyz ../feature-xyz
Tracking a Remote Branch (Optional)
If you want to switch to a new branch that tracks a branch on a remote repository, you can use the following syntax:
git worktree add -b <branch-name> <PATH> <remote>/<branch-name>
Example:
git worktree add -b feature-zzz ../feature-x origin/feature-zzz
For existing branches, you can also track a remote branch:
git worktree add --track -b <branch> <path> <remote>/<branch>
Example:
git worktree add --track -b branchname ../folder origin/branchname