git worktree lets you check out a second branch in its own folder from the same repository, so you can fix a bug or review a PR without stashing or cloning again.
The short answer
#Use git worktree add to check out another branch into a separate folder. Both folders share one repository, so commits made in either are visible to both, and you never need to stash your current work.
_10git worktree add ../project-hotfix -b hotfix/login origin/main_10cd ../project-hotfix_10# work, commit, push as usual_10git worktree remove ../project-hotfix
Why it happens
#A normal clone has one working directory, so it can only have one branch checked out at a time. Switching branches means committing or stashing what you are doing, waiting for files to change, and often rebuilding or reinstalling. Cloning the repository again works, but it duplicates the whole history and the two clones do not share branches or commits until you push and fetch.
A worktree is a second working directory attached to the same .git data. Each worktree has its own checked-out branch, its own staged changes and its own files on disk, but they all share commits, branches, tags, remotes and configuration. Creating one is fast because no history is copied.
How to fix it
#Add a worktree for an existing branch
Pass a folder path and a branch name. Keeping worktrees next to the main checkout, rather than inside it, stops tools from scanning one inside the other.
_10git fetch_10git worktree add ../project-review feature/new-checkout
If the branch only exists on the remote, Git creates a local branch that tracks it, as long as only one remote has a branch with that name.
Create a new branch in a new worktree
Use -b to make a new branch, and give a starting point. This is the usual way to start an urgent fix while your feature branch stays untouched.
_10git worktree add -b hotfix/login ../project-hotfix origin/main
To try something out without a branch at all, use --detach.
_10git worktree add --detach ../project-scratch HEAD
List, remove and tidy up
_10# See every worktree and its branch_10git worktree list_10_10# Remove one (refuses if it has uncommitted changes)_10git worktree remove ../project-hotfix_10_10# Clean up records of worktree folders you deleted by hand_10git worktree prune
Removing a worktree does not delete its branch. Delete the branch separately with git branch -d once it is merged.
Set up each worktree like a fresh checkout
Only tracked files appear in a new worktree. Ignored files such as node_modules, build output and .env files are not copied, so install and configure each one.
_10cd ../project-hotfix_10cp ../project/.env.local .env.local_10npm ci
If two worktrees run a dev server at the same time, give them different ports.
Common pitfalls
#- "is already checked out" or "is already used by worktree". A branch can only be checked out in one worktree at a time. Use a different branch, or switch the other worktree away first.
- Deleting the folder with rm. Git still thinks the worktree exists and keeps the branch locked. Run
git worktree prune, or usegit worktree removenext time. - The stash is shared. All worktrees use the same stash list, so
git stash popin one can apply changes saved in another. Name your stashes or prefer commits. - Nesting worktrees inside the main folder. Editors, test runners and linters may pick up the second copy. Keep them as sibling folders.
- Moving folders by hand. Use
git worktree moveso Git updates its records.
Related reading
#Was this resource helpful?