The Problem with One Working Directory
Think of a standard Git repository like a single dressing room. You can only wear one outfit at a time. To try on a different outfit (branch), you have to take off the current one first. If your current outfit is half-assembled — pins sticking out, alterations in progress — swapping is messy and risky.
In Git terms: you have one working directory. Switching branches with git checkout physically replaces every file in that folder. If you have uncommitted changes, you must stash or commit them first. If your project takes five minutes to compile, every branch switch costs you five minutes of rebuild time.
How Worktrees Work
git worktree lets you check out multiple branches simultaneously into separate directories, all sharing the same .git folder and commit history. No duplication of the repository database — just multiple working directories pointing into the same object store.
A typical layout looks like this:
~/project/ ← main worktree (branch: main)
~/project-hotfix/ ← linked worktree (branch: hotfix/login-crash)
~/project-feature/ ← linked worktree (branch: feature/new-dashboard)
All three directories share the same .git folder inside ~/project/. You can open each in a separate terminal (or IDE window), run the app from each independently, and commit to each branch without touching the others.
Key Constraint
Git enforces one rule: the same branch cannot be checked out in two worktrees at the same time. If main is checked out in ~/project/, you cannot also check it out in ~/project-hotfix/. Git prevents this to avoid two processes writing conflicting index state for the same branch. If you try, Git will refuse with an error: fatal: 'main' is already checked out at '...'.
Interviewers sometimes ask: "How do you work on two branches simultaneously without stashing?" The expected answer is git worktree. A common follow-up is: "What's the constraint?" Answer: you cannot check out the same branch in two worktrees at once, because Git's index (.git/index) is per-worktree but the branch ref is shared — two worktrees writing to the same branch ref simultaneously would corrupt state.