Menu
Coddy logo textTech

Git Worktree

Last updated

git worktree lets one repository have several working directories at once, each on a different branch. Instead of stashing your work to switch branches, you check out the other branch in a separate folder - review a PR, run a hotfix, or build one branch while editing another, all sharing the same .git history.

Try these in the terminal playground - a real shell in your browser, nothing to install.

Syntax

CommandWhat it does
git worktree add ../hotfix hotfixCheck out hotfix in a sibling folder
git worktree add -b new ../newCreate a branch and a worktree for it
git worktree listList all worktrees and their branches
git worktree remove ../hotfixRemove a worktree
git worktree pruneClean up stale worktree entries

Worked example

Fix a bug on main without disturbing your feature branch.

StepCommandResult
1git worktree add ../fix mainmain checked out in ../fix
2Fix and commit in ../fixHotfix done on main
3git worktree remove ../fixClean up; feature branch was untouched

Git worktree FAQ

What is a git worktree?
A worktree is an additional working directory attached to the same repository, checked out to a different branch or commit. It lets you have multiple branches checked out at the same time in separate folders, all sharing one .git history - so you don't have to stash or clone to work on two branches at once.
How do I add a worktree?
Run git worktree add <path> <branch>, for example git worktree add ../hotfix hotfix. Git checks that branch out into the new folder. To create a new branch at the same time, use git worktree add -b <newbranch> <path>.
When should I use a worktree instead of switching branches?
Use it when switching branches would be disruptive - you have uncommitted work, a long build you don't want to interrupt, or you need to compare two branches side by side. A worktree gives you the other branch in its own folder without touching your current one.
How do I remove a worktree?
Run git worktree remove <path>. If the folder was deleted manually, run git worktree prune to clean up the stale bookkeeping. You can't remove the main worktree (the original repo folder), only the extra ones you added.
Can I practice this online?
Yes. Open the terminal playground to run git worktree in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers branching workflows step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED