Git Worktree: Work on Two Branches at Once

Suman SharmaSuman Sharma

You're halfway through a feature. Someone asks you to review a PR, or production needs a hotfix. The usual dance is git stash, switch, do the thing, switch back, git stash pop, and hope nothing conflicts.

git worktree skips all of that. It checks out another branch into a separate folder, backed by the same repository.

Add a worktree

# from inside your repo git worktree add ../myapp-hotfix -b hotfix/login main

That creates ../myapp-hotfix, makes a new branch hotfix/login from main, and checks it out there. Your original folder is untouched, uncommitted changes and all.

To check out an existing branch:

git worktree add ../myapp-review feature/new-checkout

Work in it

cd ../myapp-hotfix # edit, test, commit, push, as normal git commit -am "Fix login redirect" git push -u origin hotfix/login

Commits are written to the shared repository, so you can git log hotfix/login from the original folder straight away.

List and remove

git worktree list # /code/myapp a1b2c3d [feature/payments] # /code/myapp-hotfix e4f5a6b [hotfix/login] git worktree remove ../myapp-hotfix

If you deleted the folder by hand instead, clean up Git's records:

git worktree prune

The branch survives removing the worktree. Delete it separately with git branch -d hotfix/login if you're done.

Where worktrees shine

Reviewing a PR without losing your place. Check out the PR branch in its own folder, run it, leave comments, remove it.

Hotfixes mid-feature. Your feature branch keeps its half-finished state, dev server and all.

Comparing behaviour. Run main on port 3000 and your branch on 3001, side by side.

Long builds or test runs. Kick off a slow test suite in one worktree while you keep coding in another.

Running AI coding agents in parallel. Give each agent its own worktree and branch. They can each edit files, run tests, and commit without stepping on each other or on your working copy. When one finishes, review its branch like any PR. Several AI coding tools now create worktrees for this automatically.

Things to know

  • One branch, one worktree. Git won't check out the same branch in two places. Use --detach if you just want to look at a commit.
  • Dependencies aren't shared. Each folder needs its own node_modules (pnpm's shared store makes this cheap) and its own .env file, since ignored files aren't copied.
  • Hooks and config are shared, because they live in the common .git directory.
  • Keep worktrees outside the main folder (../name), or add their folder to .gitignore, so tools don't index them twice.

A naming convention that helps

git worktree add ../$(basename "$PWD")-review -b review/pr-142 origin/pr-branch

A Git alias shortens it further:

git config --global alias.wt "worktree" git wt add ../app-fix -b fix/thing

Worktree vs stash vs clone

StashWorktreeSecond clone
Keeps current changes in placeNoYesYes
Disk useNoneWorking files onlyEverything
Shares commits instantlyYesYesNo, must fetch
Good forquick 2-minute switchesparallel worktotally separate setups

Frequently asked questions

What is git worktree?

A Git command that lets one repository have several working directories, each with a different branch checked out. They share the same .git history and objects.

Is git worktree better than cloning the repo twice?

Usually. Worktrees share objects, so they're fast to create and use little disk, and commits made in one are instantly visible in the others. A second clone duplicates everything and needs its own fetches.

Can two worktrees have the same branch checked out?

No. Git refuses so that two folders can't move the same branch in different directions. Create a new branch for the second worktree instead.

Contact

Get in Touch

Want to call me? book a call and I'll respond in the available time slot. I will ignore all soliciting.