Dev tools and workflow · Intermediate · 14 min
Work on two branches at once with git worktree
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Git 2.56.1 (git-core PPA)
You will fix an urgent bug in a second working tree without stashing your half-finished feature, merge the fix, and clean up every extra tree afterwards.
On this page
You are halfway through a feature. Files are edited, nothing is committed, and the tests do not pass yet. Then a message arrives: prices on the live site show NGN 1.5 instead of NGN 1.05, and it needs fixing now.
The usual answer is git stash, switch branch, fix, switch back, git stash pop, and hope nothing collides. git worktree gives you a better option. It checks out a second branch into a second folder, backed by the same repository. Your feature folder stays exactly as you left it, and the fix happens next door.
In this tutorial you build a small throwaway repository in ~/worktree-demo, leave uncommitted feature work in it, and then add worktrees for the fix and for main. Along the way you meet the rules that trip people up: one branch per worktree, a stash shared by all of them, and untracked files such as .env and node_modules that each folder needs separately. You finish with the fix merged and only your feature folder left.
Step 1 · Install a current Git and make a practice repository
Ubuntu 26.04 ships Git 2.53. The Ubuntu Git maintainers publish the current release in the git-core PPA, and packages from it keep getting updates through apt upgrade like any other. Install Git 2.56 from there:
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt install -y git
git --versionEvery git worktree command in this tutorial also works on the stock 2.53 package, so you can skip the PPA if you prefer. On macOS, brew install git gives you a current release.
Now make the practice repository. It holds a price formatter with a bug in it: amounts are stored in kobo (one hundredth of a naira), and the script forgets to pad the kobo to two digits. The .gitignore keeps .env and node_modules/ out of Git, as most real projects do.
mkdir -p ~/worktree-demo/shop
cd ~/worktree-demo/shop
git init -b main
cat > price.sh <<'SH'
#!/bin/sh
# Print an amount given in kobo as naira.
kobo=$1
echo "NGN $((kobo / 100)).$((kobo % 100))"
SH
printf 'rice\nbeans\nplantain\n' > products.txt
printf '.env\nnode_modules/\n' > .gitignore
git add price.sh products.txt .gitignore
git commit -m "Add price formatter and product list"
printf 'PORT=3000\n' > .envCheck it worked
sh price.sh 105
git log --onelineThe script prints NGN 1.5, which is the bug. The log has one commit.
Step 2 · Leave some feature work half done
Start the feature on its own branch. Edit a tracked file and create a new one, and do not commit:
cd ~/worktree-demo/shop
git switch -c feature/search
printf 'yam\n' >> products.txt
cat > search.sh <<'SH'
#!/bin/sh
grep -i "$1" products.txt
SHThis is the state that makes stashing awkward: a modified file, an untracked file, and a branch that is not ready.
Check it worked
git status --shortYou see M products.txt and ?? search.sh. Leave them exactly like this for the rest of the tutorial.
Step 3 · Add a worktree for the fix on a new branch
From the feature folder, ask Git for a second working tree. -b creates a new branch, the path is the new folder, and the last argument is where the branch starts:
cd ~/worktree-demo/shop
git worktree add -b hotfix/kobo-padding ../shop-hotfix mainGit creates ~/worktree-demo/shop-hotfix, makes the branch hotfix/kobo-padding from main, and checks it out there. It prints Preparing worktree (new branch 'hotfix/kobo-padding') and the commit it starts on.
The new folder has no .git directory, only a small .git file that points back to shop/.git. There is still one repository: one set of commits, one set of branches, one configuration. What each worktree has of its own is its files, its index (what is staged) and its HEAD. shop is the main worktree, and the others are linked worktrees.
Put linked worktrees beside the main folder, not inside it. A worktree inside another one shows up as untracked files there and confuses search tools and editors.
Check it worked
git worktree list
git -C ../shop-hotfix status --short --branch
git status --shortgit worktree list shows two lines: shop on [feature/search] and shop-hotfix on [hotfix/kobo-padding], each with its commit. The hotfix tree is clean. Your feature changes in shop are untouched.
Step 4 · Make the fix and set up the untracked files
Move to the hotfix folder and replace the formatter with one that pads the kobo to two digits:
cd ~/worktree-demo/shop-hotfix
cat > price.sh <<'SH'
#!/bin/sh
# Print an amount given in kobo as naira.
kobo=$1
printf 'NGN %d.%02d\n' $((kobo / 100)) $((kobo % 100))
SH
sh price.sh 105
sh price.sh 250000
git commit -am "Pad kobo to two digits"The script now prints NGN 1.05 and NGN 2500.00.
A new worktree contains only tracked files. Anything ignored or untracked in shop is missing here, and that is where most worktree surprises come from:
.envand other local config. Copy them in, then change anything that must differ.- Ports. If both trees run a dev server on the same port, the second one fails to start. Give each tree its own port in its
.env. node_modules, virtual environments and build output. Each tree needs its own install, so disk use grows with every worktree. On a metered or prepaid connection,npm install --prefer-offlineuses packages already in npm's local cache before downloading, and pnpm links packages from one store on disk, so a second tree costs little data.
Copy the .env from the feature folder, then change its port line so the hotfix tree gets its own port:
cd ~/worktree-demo/shop-hotfix
cp ../shop/.env .env
sed -i 's/^PORT=.*/PORT=3001/' .envOn macOS, BSD sed needs an empty suffix after -i: sed -i '' 's/^PORT=.*/PORT=3001/' .env.
Check it worked
cd ~/worktree-demo/shop-hotfix
git log --oneline -2
cat .env
git status --shortThe log shows Pad kobo to two digits on top. .env says PORT=3001, and git status --short prints nothing, because .env is ignored.
Step 5 · See that the stash is shared
Worktrees share every ref under refs/, and the stash is one of them. A stash you make in one tree appears in all of them. Try it with a scratch edit in the hotfix tree:
cd ~/worktree-demo/shop-hotfix
echo "# scratch" >> price.sh
git stash push -m "hotfix scratch"
git -C ../shop stash listThe last command runs in the feature folder and still lists stash@{0}: On hotfix/kobo-padding: hotfix scratch. If you run git stash pop in shop now, the hotfix edit lands in your feature work. When several trees are in use, name your stashes with -m and check git stash list before you pop or drop anything. This one is not needed, so drop it from the tree that made it:
cd ~/worktree-demo/shop-hotfix
git stash list
git stash drop stash@{0}Check it worked
cd ~/worktree-demo/shop-hotfix
git stash list
git status --short
sh price.sh 105git stash list prints nothing, the tree is clean, and the fix still prints NGN 1.05.
Step 6 · Check out main in its own worktree and merge the fix
To merge the fix you need main checked out somewhere. A branch can be checked out in only one worktree at a time. Git refuses a second checkout so that two folders never move the same branch under each other. See the rule from the feature folder by asking for the hotfix branch again:
cd ~/worktree-demo/shop
git worktree add ../shop-again hotfix/kobo-padding || echo "refused: the branch is in use in another worktree"Git stops with a message like fatal: 'hotfix/kobo-padding' is already used by worktree at '.../shop-hotfix', and no folder is created. The || echo only keeps your shell session tidy; the refusal is the point. git switch gives the same refusal. main is not checked out anywhere, so give it a worktree. With an existing branch, leave out -b:
cd ~/worktree-demo/shop
git worktree add ../shop-main mainMerge the fix there. main has not moved since the hotfix branch started, so Git fast-forwards:
cd ~/worktree-demo/shop-main
git merge hotfix/kobo-padding
sh price.sh 105Check it worked
cd ~/worktree-demo/shop
git log --oneline -1 main
git status --shortFrom the feature folder, main already points at Pad kobo to two digits. You did not fetch or switch, because every tree reads the same branches. Your feature work still shows M products.txt and ?? search.sh. In a team you would push main (or open a pull request from the hotfix branch) from shop-main now.
Step 7 · Lock and move a worktree
git worktree prune and git worktree remove delete worktree records. If a tree lives on a removable drive or a network share that is sometimes unplugged, or you want to make sure nobody removes it by accident, lock it and say why:
cd ~/worktree-demo/shop
git worktree lock --reason "release checks running" ../shop-main
git worktree list
git worktree remove ../shop-main || echo "refused: the worktree is locked"The list marks shop-main as locked, and remove refuses. Removing or moving a locked tree takes --force twice, which is hard to do by accident. Unlock it when you are done:
cd ~/worktree-demo/shop
git worktree unlock ../shop-mainTo rename or relocate a linked worktree, use git worktree move rather than mv, so Git updates its records. Give the hotfix folder a name that says what it holds:
cd ~/worktree-demo/shop
git worktree move ../shop-hotfix ../shop-fix-koboThe main worktree cannot be moved this way. If you have already moved a folder by hand, git worktree repair run inside it reconnects it.
Check it worked
cd ~/worktree-demo/shop
git worktree list --verboseThree trees are listed: shop, shop-fix-kobo on [hotfix/kobo-padding] and shop-main on [main]. None says locked any more.
Step 8 · Remove the extra worktrees and prune
There are two ways to finish with a worktree. git worktree remove is the clean one. It refuses if the tree has uncommitted changes or untracked files, so you do not lose work. Deleting the folder by hand also happens, by you or by a clean-up script, and it leaves a stale record behind.
Copy anything you still need, such as a .env you edited, out of a tree before you remove it. The next command deletes the shop-fix-kobo folder, including its .env; the fix itself is safe because it is committed and merged. Delete it by hand to see what Git does:
cd ~/worktree-demo/shop
rm -rf ../shop-fix-kobo
git worktree listThe list still shows shop-fix-kobo, now marked prunable. Its record also still holds hotfix/kobo-padding as checked out, so you could not check that branch out anywhere else yet. prune removes records whose folders are gone. Use -n first to see what it would do:
cd ~/worktree-demo/shop
git worktree prune -n -v
git worktree prune -vNow delete the hotfix branch. Run it from shop-main: git branch -d only deletes a branch that is merged into the branch you are on, and main has the fix while feature/search does not.
cd ~/worktree-demo/shop-main
git branch -d hotfix/kobo-paddingFinally, remove the main tree the clean way. It has no changes and no untracked files, so Git removes it without fuss:
cd ~/worktree-demo/shop
git worktree remove ../shop-mainCheck it worked
cd ~/worktree-demo/shop
git worktree list
git branch
git status --short
git show main:price.sh | grep '%02d'One worktree is left, shop, on feature/search. The branches are feature/search and main, and main holds the padded printf. Your feature work is still uncommitted and intact, and you never stashed it.
Clean up
When you have finished with the practice repository, delete the whole demo folder. Check the path first; this removes everything under it:
cd ~
rm -rf ~/worktree-demoTroubleshooting
fatal: '<branch>' is already used by worktree at '<path>'(older Git releases word it differently, for exampleis already checked out at): the branch is in another tree. Work there, or rungit worktree listto find it. If the path no longer exists, rungit worktree prune.fatal: '<path>' already exists: the folder you named is already there. Pick another path;git worktree addnever writes into an existing non-empty folder.fatal: '<path>' contains modified or untracked files, use --force to delete it: commit, stash or copy out what you need, then rungit worktree removeagain. Add--forceonly when you are sure.
Next steps
- Tidy the hotfix commits before review: clean up commits before a pull request with interactive rebase.
- Go back over branches and merge styles: create, switch and merge branches in Git.
- Recover when a merge or commit goes wrong: undo mistakes in Git.
Related tutorials
- Version releases with semantic versioning, Git tags and a changelogYou will take a small shell library from 1.0.0 to 1.1.0 with written version rules, Conventional Commits, a Keep a Changelog file, annotated Git tags, a build stamped by git describe and release notes drafted from the commits since the last tag.
- Search files and shell history interactively with fzfYou will have fzf 0.75 wired into your shell for history search, file insertion and directory jumps, fed by fd with a preview pane, plus two small pickers that switch Git branches and stop processes.
- Run project tasks with just, a command runnerYou will install just 1.59 and write a justfile for a small site project, with recipes that take parameters, depend on each other, read variables and a .env file, run Python, ask before deleting and list themselves as help.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials