Worktrees
Check out several branches of one repository at once in separate folders with git worktree, no stashing or recloning, and learn to remove them cleanly.
Creating and switching branchesStashing unfinished workRefs, branches and HEAD
Worktrees
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `git worktree add <path> <branch>` creates another working directory for the same repository, sharing one `.git` database, so a branch can be open in two places without a second clone.
- A branch can be checked out in only one worktree at a time, and Git refuses to add a second one for the same branch.
- Commits made in one worktree are immediately visible to all the others, because they share the same objects and branches.
- Remove a worktree with `git worktree remove <path>` (use `--force` for dirty trees), and `git worktree prune` cleans up records of directories that were deleted by hand.
- Untracked, ignored and build files such as `node_modules` are not shared, so each worktree needs its own install.
The interruption problem
You’re halfway through the search command in jot. Files are modified, and nothing is ready to commit. A bug report arrives for the released version. Your usual options are all a little painful. You can stash and switch, then pop later and hope nothing collides, or make a second clone and have two copies of the history to keep in sync.
A worktree is a third option. One repository, one .git database, several working directories, each with its own branch checked out. Think of it like one library with several reading desks: one catalogue, but you can have a different book open at each desk.
I ran all of this on Git 2.54, with jot on a search branch holding an uncommitted search.txt. Hashes will differ for you.
Adding one
$ git worktree add ../jot-hotfix -b hotfix main
Preparing worktree (new branch 'hotfix')
HEAD is now at c0a903c Add jot.sh
$ git worktree list
<w14>/jot c0a903c [search]
<w14>/jot-hotfix c0a903c [hotfix]
(I replaced my long scratch path with <w14>.) -b hotfix main created a new branch hotfix from main and checked it out in a new folder next to the project. My search.txt wasn’t touched. I cd ../jot-hotfix, edited, and committed there:
$ git commit -qam "Hotfix"
$ cd ../jot
$ git log --oneline hotfix -2
a1f42d8 Hotfix
c0a903c Add jot.sh
The main folder sees the commit at once, because branches and objects are shared. Only the working files and the index are separate. Look at how it’s wired:
$ cat ../jot-hotfix/.git
gitdir: <w14>/jot/.git/worktrees/jot-hotfix
$ ls .git/worktrees
jot-hotfix
In the linked folder, .git is a one-line file pointing back to private state (its own HEAD and index) under the main repository’s .git/worktrees/.
The one-branch rule
$ git worktree add ../jot-again search
fatal: 'search' is already used by worktree at '<w14>/jot'
Git refuses. If two folders could both commit to search, each would move the branch under the other’s feet. Check out a different branch, or use --detach for a throwaway view of a commit, which is handy for reviewing someone’s work or running a test on a tag.
Removing and cleaning up
$ git worktree remove ../jot-hotfix
fatal: '../jot-hotfix' contains modified or untracked files, use --force to delete it
$ git worktree remove --force ../jot-hotfix
Git won’t delete a folder with uncommitted changes by default. The commit survives: hotfix still lists a1f42d8. Removing the worktree removes the folder, not the branch. Delete the branch separately when you’re done.
If you delete the folder yourself, Git notices:
$ rm -rf ../jot-tmp
$ git worktree list
<w14>/jot-tmp c0a903c [tmp] prunable
$ git worktree prune -v
Removing worktrees/jot-tmp: gitdir file points to non-existent location
A worktree on removable storage can be protected with git worktree lock --reason "on a USB stick" <path>. A locked worktree refuses removal, and prints the reason.
What isn’t shared
Worktrees share history, branches, tags and config. They do not share untracked files: build output, node_modules, virtual environments and .env are per folder. A project with a heavy install needs it once per worktree, which costs disk and time. For some projects that cancels the benefit. Also, since git branch -v lists a branch checked out elsewhere with a +, you can tell at a glance which are in use.
When to reach for one
- An urgent fix while you’re mid-feature, as above.
- Reviewing a pull request in a clean folder without disturbing your own.
- Running a long test or build on one branch while you keep coding on another.
- Comparing two versions side by side in your editor.
Stashing, covered in stashing unfinished work, is still simpler for a 30-second detour. Worktrees are for when the detour is long.
What the video covers
Andrew Burgess’s “You need to use Git Worktrees” is under six minutes and was published around 2025. From its title it argues for worktrees as a workflow improvement. I judge English from the title, description and channel only, and I could not read a transcript, so I can’t confirm whether it covers pruning or locking. Net Ninja’s series installment on adding worktrees is linked as a runner-up.
Where this fits
A worktree is one repository seen several ways. The next lesson is about one repository containing another: submodules and subtrees.
Check yourself
You're mid-feature and need to fix a bug on `main`. Why might a worktree beat `git stash`?
You keep your half-finished work untouched in its folder and fix the bug in another folder, with no stash to apply later and nothing to rebuild when you switch back.
Why does `git worktree add ../x search` fail if `search` is already checked out?
A branch can only be checked out in one worktree, because two places moving the same branch pointer would collide.
What is in the `.git` entry of a linked worktree?
A small file, not a directory, containing `gitdir:` followed by the path of that worktree's private data inside the main repository's `.git/worktrees/`.
You deleted a worktree folder with `rm -rf`. How do you clean up Git's record?
Run `git worktree prune`. `git worktree list` shows the stale one as `prunable`.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.