Git Worktree
Normally a repository has one working folder, so working on two branches at once means stashing and switching back and forth. git worktree lets one repository have several working folders, each with a different branch checked out. They share the same history, objects and configuration, so there's no second clone to keep in sync.
Typical uses: fixing an urgent bug while a long feature is half-done, reviewing a pull request without disturbing your work, or running a slow test suite on one branch while coding on another.
Adding a worktree
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Initial version"
git switch -q -c feature/big-redesign
echo "half-done redesign" > redesign.txt # uncommitted work in progress
# urgent bug: open main in a second folder without touching the current one
git worktree add ../app-hotfix main
git worktree list
cd ../app-hotfix
git branch --show-current
lsPreparing worktree (checking out 'main')
HEAD is now at 3e99e51 Initial version
/home/learner/app 3e99e51 [feature/big-redesign]
/home/learner/app-hotfix 3e99e51 [main]
main
app.txt
The main folder still has feature/big-redesign checked out with its uncommitted file; ../app-hotfix has main.
Working in both
Commits made in any worktree go into the shared repository, so they're immediately visible from the others:
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Initial version"
git switch -q -c feature/big-redesign
echo "half-done redesign" > redesign.txt
git worktree add -q -b hotfix/crash ../app-hotfix main
cd ../app-hotfix
echo "v1.0.1" > app.txt && git commit -q -am "Fix crash on start"
git switch feature/big-redesign 2>&1
git log --oneline -1
cd ../app
git status -s
git log --oneline --graph --allfatal: 'feature/big-redesign' is already used by worktree at '/home/learner/app'
867f909 Fix crash on start
?? redesign.txt
* 867f909 Fix crash on start
* 3e99e51 Initial version
-b hotfix/crash created a new branch for the worktree. Notice that feature/big-redesign couldn't be checked out in the hotfix folder: a branch can only be checked out in one worktree at a time, which prevents two folders from fighting over the same branch.
Cleaning up
When you're done, remove the worktree. Git refuses if it has uncommitted changes, unless you add --force:
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Initial version"
git worktree add -q -b review ../app-review
git worktree add -q -b scratch ../app-scratch
echo "temp" > ../app-scratch/notes.txt
git worktree list --porcelain | grep worktree
git worktree remove ../app-review
git worktree remove ../app-scratch
echo "exit status: $?"
git worktree remove --force ../app-scratch
git worktree listworktree /home/learner/app
worktree /home/learner/app-review
worktree /home/learner/app-scratch
fatal: '../app-scratch' contains modified or untracked files, use --force to delete it
exit status: 128
/home/learner/app 3e99e51 [main]
If you delete a worktree folder by hand, run git worktree prune to make Git forget it.
Commands
| Command | Does |
|---|---|
git worktree add <path> <branch> |
new folder with an existing branch |
git worktree add -b <new> <path> [<start>] |
new folder with a new branch |
git worktree add --detach <path> <commit> |
folder at a commit, no branch (e.g. to build an old release) |
git worktree list |
show all worktrees |
git worktree remove <path> |
delete a worktree (and its folder) |
git worktree prune |
forget worktrees whose folders were deleted |
git worktree lock <path> |
protect a worktree on a removable drive from pruning |
Tip: Keep worktrees next to the main folder (
../app-hotfix), not inside it, so tools andgit statusin the main folder don't see them as untracked files.
Test yourself
What does git worktree add ../hotfix main create?
- A new clone with its own history
- A second working folder of the same repository with main checked out
- A branch called hotfix
- A copy of the .git folder
Can the same branch be checked out in two worktrees at once?
- Yes, always
- No, Git refuses so the folders don't conflict
- Only main
- Only with --force on clone