Sub topic 2
Creating your first linked worktree
This chapter assumes you can commit, create and switch branches, and merge. It also assumes you know the difference between the working directory, the index and the repository history, and that you are comfortable with relative paths on the command line.
What `git worktree add` does
A linked worktree is an extra working directory attached to your existing repository. It has its own checked-out files, its own index and its own HEAD. It does not have its own copy of the history. The objects, branches and tags stay in the original repository, so creating a worktree is fast and uses little disk space beyond the checked-out files.
The command has this shape:
git worktree add <path> [<commit-ish>]The path is where the new directory will be created. Git creates it for you, and it must not exist or must be empty. A relative path is resolved from your current directory, not from the repository root. Placing the new directory next to the repository, as in ../myproject-hotfix, keeps it outside the original working tree.
Creating a worktree with a new branch
Suppose you are in the middle of a feature in myproject and a login crash needs a fix now. Instead of stashing your work, create a second directory with a new branch based on main:
git worktree add ../myproject-hotfix -b hotfix/login-crash mainThe pieces are:
../myproject-hotfixis the new directory.-b hotfix/login-crashcreates a new branch with that name.mainis the starting point for the new branch.
Git prints a short message saying it is preparing the worktree and which commit HEAD is now at. Your original directory is unchanged, including any uncommitted edits in it.
Now work in the new directory like any other repository:
cd ../myproject-hotfix
git status
# edit files, then:
git commit -am "Fix crash when the session cookie is missing"
git push -u origin hotfix/login-crashCommits you make here are written to the shared repository. When you go back to myproject, git log hotfix/login-crash shows them.
Checking out an existing branch
If the branch already exists, leave out -b and name the branch:
git worktree add ../myproject-feature feature/searchGit checks out feature/search in the new directory. If you give a name that matches only a remote-tracking branch, such as origin/feature/search, Git does not treat that as permission to create a local branch of the same name. The next chapters cover that case.
Verifying the result
Run this from any worktree of the repository:
git worktree listYou get one line per worktree, with its path, the abbreviated commit and the branch in brackets. The original directory is listed first, followed by the linked ones. The next chapters use this output more fully.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: '<path>' already exists` | The target directory exists and is not empty | Choose a new path or empty the directory |
| Error saying the branch is already checked out | Another worktree has that branch | Use a different branch, create one with `-b`, or switch the other worktree first |
| New directory appears inside the repository | The path was relative to the wrong directory | Run the command from the intended location, or use an absolute path |
| `fatal: a branch named '...' already exists` | You used `-b` with a name that is taken | Drop `-b` to use the existing branch, or pick a new name |
Try it
Exercise
You are on main in ~/code/shop with uncommitted changes. You must start a branch called fix/price-rounding from main in a separate directory next to the repository, without touching your current changes. Write the command, and say where the new directory will be.
Show answer
Run git worktree add ../shop-price-fix -b fix/price-rounding main. The new directory is ~/code/shop-price-fix, because .. is the parent of the current directory. The uncommitted changes stay in ~/code/shop and are unaffected.
Exercise
You run git worktree add ../shop-search feature/search and Git reports that feature/search is already checked out. What does that tell you, and which command shows where it is checked out?
Show answer
Some worktree, possibly the main directory, already has feature/search checked out, and Git allows a branch in only one worktree. Run git worktree list to see which path holds it.