Git Worktree: Use Multiple Working Directories
Two checkouts, one repository
You are editing feature notes and need to fix main without disturbing that directory. A linked worktree is another checkout backed by the same repository. Each worktree has its own files, HEAD and index. Commit objects, ordinary branch references and the stash are shared. Changes in one worktree do not magically edit files in another; shared branch updates are visible as references. This is not a security sandbox or an independent backup.
Create a separate hotfix checkout
In a fresh practice repository with committed main and a checked-out feature branch, create a sibling directory using a new hotfix branch from main. Do not reuse an existing directory or branch. Use git -C to execute commands in that checkout without changing the shell directory. worktree list shows the checkout paths, tips and branches; use --porcelain for machine parsing.
git worktree add -b hotfix ../hotfix maingit worktree list --porcelaingit -C ../hotfix status --shortgit -C ../hotfix branch --show-currentUnderstand the branch guard
Git normally refuses to check out the same branch in two linked working directories. For example, with feature already checked out, adding another worktree on feature fails. Do not bypass the guard with force. A hotfix commit advances hotfix but not feature. You can inspect the new commit from the original directory. A staged feature edit stays staged there and does not appear in the hotfix index.
git status --shortgit -C ../hotfix status --shortgit log -1 hotfixRemove clean worktrees, not branches
After finishing the hotfix, commit or otherwise preserve all valuable files, including untracked and ignored files, and check status --short --ignored in the linked checkout. Run remove from the original directory only after confirming the path is the disposable clean hotfix. Git refuses ordinary removal of modified or untracked worktrees; ignored files can still be lost, so inspect them yourself. The branch and its commits remain after successful removal. Never assume a folder deletion is equivalent to worktree remove.
git -C ../hotfix status --short --ignoredgit worktree remove ../hotfixgit worktree listgit log -1 hotfixCheck Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Worktree: Use Multiple Working Directories — knowledge check
Reason about temporary Git work and recovery.
What is separate for each linked worktree?
Which action is normally refused?
What does git -C ../hotfix status inspect?
What survives successful clean worktree removal?
Why review ignored files before removing a worktree?