Skip to main content

Git Worktree

[[Git]]

Git Worktree

git worktree add <PATH>

1/ Create feature-x directory and branch with the same name.

git worktree add ../feature-x

2/ Named Branch (optional)

If you want to give you branch a unique name then you can use the -b flag with the add command.

git worktree add -b feature-xyz ../feature-xyz

3/ Track remote branch (optional)

Let’s say you want to switch to a new branch that is tracking the branch at remote, where you want to push changes to.

git worktree add -b <branch-name> <PATH> <remote>/<branch-name>

git worktree add -b feature-zzz ../feature-x origin/feature-zzz

git worktree add --track -b <branch> <path> <remote>/<branch>

usage: git worktree add [<options>] <path> [<commit-ish>]

or: git worktree list [<options>]

or: git worktree lock [<options>] <path>

or: git worktree move <worktree> <new-path>

or: git worktree prune [<options>]

or: git worktree remove [<options>] <worktree>

or: git worktree unlock <path>

for existing branch

git worktree add --track -b <branch> <path> <remote>/<branch>

git worktree add --track -b branchname ../folder origin/branchname