Git Worktree

Updated · 10 min read
mediumGit 2.53gitworktreebranchescode-reviewinterview

A worktree is a working folder connected to a repository. Every repository has one, the main worktree, where you ran git init or git clone. The git worktree command adds more: each extra worktree is another folder with its own checked-out branch, its own staging area and its own files, while all of them share one history, one set of branches and one object database. It is used to work on two branches at the same time, for example to review a teammate's pull request or fix an urgent bug, without stashing or committing your half-finished work.

Think of two desks in the same office, sharing one filing cabinet. You can leave papers spread out on one desk while you work on something else at the other, and anything you file is in the same cabinet.

Many working folders, one repository

Syntax

Terminal
git worktree add <path> [<branch>]
git worktree list
git worktree remove <path>
CommandDescription
git worktree add <path> <branch>It is used to create a folder at path with an existing branch checked out.
git worktree add -b <new-branch> <path> [<start>]It is used to create a new branch and check it out in a new folder.
git worktree add --detach <path> <commit>It is used to check out a commit without a branch, for a quick look.
git worktree listIt is used to show every worktree, its commit and its branch.
git worktree remove <path>It is used to delete a worktree's folder and unregister it.
git worktree pruneIt is used to forget worktrees whose folders were deleted by hand.
git worktree lock <path> / unlockIt is used to stop a worktree from being pruned, such as one on a removable drive.
git worktree move <path> <new-path>It is used to move a worktree to another folder.

The Starting Point

Navika is halfway through a change on her branch feature/hostel-fee. Advitha has pushed a branch fix/late-fee and asked her to review it:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git switch --quiet -c fix/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Add the late fee"
git switch --quiet main
git switch --quiet -c feature/hostel-fee
echo 'Hostel fee: $9,000 (draft' >> fees.txt
git status --short
git log --oneline --all
Output
 M fees.txt
3e057f4 (fix/late-fee) Add the late fee
6b651a0 (HEAD -> feature/hostel-fee, main) Add the term fee

Her fees.txt has an unfinished line that isn't ready to commit. Without worktrees, she would have to stash or commit it before switching branches.

Example 1: Adding a Worktree for a Review

Terminal
git worktree add ../fee-tracker-review fix/late-fee
git worktree list
Output
Preparing worktree (checking out 'fix/late-fee')
HEAD is now at 3e057f4 Add the late fee
/home/navika/projects/fee-tracker        6b651a0 [feature/hostel-fee]
/home/navika/projects/fee-tracker-review 3e057f4 [fix/late-fee]

Explanation: Git created the folder fee-tracker-review next to the project and checked out fix/late-fee there ("HEAD is now at" refers to the new folder's HEAD). git worktree list shows both folders, the commit each one is on and its branch. Navika's unfinished work is still in her own folder, untouched.

She reviews and runs the code in the new folder:

Terminal
cd ../fee-tracker-review
cat fees.txt
git status --short --branch
cd ../fee-tracker
git status --short --branch
Output
Term 1 fee: $12,000
Late fee: $250
## fix/late-fee
## feature/hostel-fee
 M fees.txt

Explanation: each folder has its own branch and its own changes: the review folder is clean on fix/late-fee, and her own folder still has the modified fees.txt on feature/hostel-fee. Two editor windows, two builds, and nothing to stash.

Example 2: Shared History

Commits made in any worktree are immediately visible in the others, because there is only one repository:

Terminal
cd ../fee-tracker-review
echo "Late fee applies after the 10th" >> fees.txt
git commit --quiet -am "Explain when the late fee applies"
cd ../fee-tracker
git log --oneline fix/late-fee
Output
14247ec (fix/late-fee) Explain when the late fee applies
3e057f4 Add the late fee
6b651a0 (HEAD -> feature/hostel-fee, main) Add the term fee

Explanation: Navika's own folder sees the new commit on fix/late-fee without any fetch. Branches, tags, the stash and remote settings are shared by all worktrees. What each one has for itself is its HEAD, its staging area and its working files.

Terminal
git switch fix/late-fee
Output
fatal: 'fix/late-fee' is already used by worktree at '/home/navika/projects/fee-tracker-review'

The message says where the branch is checked out, so you know which folder to use.

Example 3: A New Branch in a New Folder

An urgent bug comes in. Navika starts a fix from main in a third folder, with a new branch:

Terminal
git worktree add --quiet -b fix/term-fee ../fee-tracker-hotfix main
git worktree list
Output
/home/navika/projects/fee-tracker        6b651a0 [feature/hostel-fee]
/home/navika/projects/fee-tracker-hotfix 6b651a0 [fix/term-fee]
/home/navika/projects/fee-tracker-review 14247ec [fix/late-fee]

Explanation: -b fix/term-fee created the branch from main and checked it out in fee-tracker-hotfix. This is like git switch -c, but in a new folder.

Example 4: Cleaning Up

When the review is finished, she removes its worktree. The branch and its commits stay:

Terminal
git worktree remove ../fee-tracker-review
rm -rf ../fee-tracker-hotfix
git worktree list
git worktree prune
git worktree list
git branch
Output
/home/navika/projects/fee-tracker        6b651a0 [feature/hostel-fee]
/home/navika/projects/fee-tracker-hotfix 6b651a0 [fix/term-fee] prunable
/home/navika/projects/fee-tracker 6b651a0 [feature/hostel-fee]
* feature/hostel-fee
  fix/late-fee
  fix/term-fee
  main

Explanation: git worktree remove deleted the review folder and unregistered it. Navika deleted the hotfix folder with rm -rf instead, so Git still listed it, marked prunable, until git worktree prune cleaned up the record. Both branches still exist. + in git branch marks a branch checked out in another worktree, so here none is left. git worktree remove refuses to delete a folder with uncommitted changes unless you add --force.

Worktrees vs Other Ways

NeedWorktreegit stash + switchA second clone
Keep unfinished work as it isYesYes, but you must remember to pop itYes
Two branches open at the same timeYesNoYes
Disk spaceOnly the working filesNoneA whole second copy of the history
Commits visible in both at onceYes, one repositoryYesNo, until you push and fetch

Common Mistakes

Trying to check out the same branch in two worktrees. Git refuses. Use another branch, or --detach to look at the commit.

Deleting a worktree folder by hand. Git keeps a record of it. Run git worktree prune, or use git worktree remove in the first place.

Putting worktrees inside the project folder. They appear as untracked files. Put them next to it.

Forgetting a worktree exists. git worktree list shows all of them, and git branch marks their branches with +.

Under the Hood

An extra worktree's folder has a file called .git, not a folder. It contains one line, gitdir: /path/to/fee-tracker/.git/worktrees/fee-tracker-review, which points into the main repository. There, .git/worktrees/<name>/ holds what belongs to that worktree alone: its HEAD, its index (staging area), its own reflog for HEAD, and a gitdir file pointing back to the folder. Everything else, such as objects/, refs/ and config, is shared. When the folder disappears, git worktree prune notices that the gitdir path no longer exists and removes the record. This is also how Git knows a branch is already checked out somewhere: it reads every worktree's HEAD.

Interview Corner

What is a Git worktree?

An extra working folder for the same repository, with its own checked-out branch, staging area and files, sharing the history and branches with the main folder. It lets you work on two branches at once without stashing.

How do you review a pull request without disturbing your current work?

git fetch, then git worktree add ../review <branch>, review and test in that folder, and git worktree remove ../review when finished.

Can one branch be checked out in two worktrees?

No. Git refuses, because both folders would move the same branch. Use --detach to look at the same commit without the branch.

How are worktrees different from a second clone?

Worktrees share one repository: commits, branches and stashes are visible in all of them at once, and history is stored only once. A second clone is a separate repository that needs push and fetch to share work.

Practice

Challenge: a quick look at an old commit. From Navika's folder, add a detached worktree at ../fee-tracker-old for main, print its fees.txt and the worktree list, then remove it.

What does Git print? Before you open the solution, decide what the list shows instead of a branch name for the new folder.

Show solution
Terminal
git worktree add --quiet --detach ../fee-tracker-old main
cat ../fee-tracker-old/fees.txt
git worktree list
git worktree remove ../fee-tracker-old
Output
Term 1 fee: $12,000
/home/navika/projects/fee-tracker     6b651a0 [feature/hostel-fee]
/home/navika/projects/fee-tracker-old 6b651a0 (detached HEAD)

The file has only the term fee from main, and the list shows (detached HEAD) for the new folder, because --detach checked out the commit without a branch. Removing it leaves no trace.

Checkpoint. In one of your own projects, with uncommitted work on your branch, add a worktree for another branch, run the project's tests there, and remove it, then confirm with git status that your own work is untouched.

Quick check: Git worktree

4 questions
1What do all worktrees of a repository share?
2Which command creates a new branch fix/x from main in a new folder?
3You deleted a worktree folder with rm -rf. What tidies up Git's record of it?
4What is inside the .git entry of an extra worktree folder?

Key Takeaways

Quick reference

CommandIt is used to
git worktree add <path> <branch>Open a branch in a new folder
git worktree add -b <branch> <path> <start>Create a branch in a new folder
git worktree listShow all worktrees
git worktree remove <path>Delete a worktree
git worktree pruneForget worktrees deleted by hand

Was this tutorial helpful?

Lesson 88 of 106