Home·Terminal commands

git worktree

git

Usage

git worktree add <path> [<branch>]

Checks out a second working folder from the same repository so you can build two branches at once without cloning again; the same branch cannot be checked out twice, and deleting the folder by hand leaves an entry until prune.

Common flags

FlagMeaning
add <path> <branch>check that branch out into another folder
add -b <new> <path>create a branch and check it out there
listshow every working tree of this repository
remove <path>delete a working tree cleanly
pruneforget trees whose folder you deleted by hand
--detachcheck out a commit without a branch

Examples

git worktree add ../hotfix hotfix/login

a second folder on another branch, sharing one .git

git worktree add -b release ../rel main

creates release from main in ../rel

git worktree remove ../hotfix

removes the folder and its bookkeeping entry

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git worktree do?

Checks out a second working folder from the same repository so you can build two branches at once without cloning again; the same branch cannot be checked out twice, and deleting the folder by hand leaves an entry until prune.

Q. How do I type it?

git worktree add <path> [<branch>] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-worktree