Worktrees
Worktrees let you check out multiple branches into separate directories from the same repo - perfect for hotfixes without stashing or context-switching.
Git worktrees - hands on
EXAMPLE
# Standard situation: you are on a feature branch with WIP changes # and an urgent bug needs a hotfix on main. # 1. Create a worktree for main alongside your repo git worktree add ../hotfix main # Now ../hotfix has a full checkout of main # Your current dir keeps feature/x and uncommitted changes - untouched # 2. Hotfix workflow cd ../hotfix git checkout -b hotfix/issue-1234 # ... edit, commit, push, open PR ... # 3. Run the test suite there without disturbing your main checkout npm test # or hit your CI by pushing # 4. List worktrees git worktree list # /path/to/repo abc123 [feature/x] # /path/to/hotfix def456 [hotfix/issue-1234] # 5. Remove a worktree once the branch is merged git worktree remove ../hotfix # 6. Prune stale entries (after manual deletion) git worktree prune # Bonus patterns # - Long-running release branch in its own dir: git worktree add ../release-2024.10 release/2024.10 # - Read-only docs build directory: git worktree add --detach ../docs-build origin/docs # - Compare branches side-by-side in your IDE
Why it matters
Worktrees beat stash + checkout + stash pop for any non-trivial interruption. They are cheap (a single .git/worktrees entry) and let you keep watchers, dev servers, and editors running on your main work while you ship the hotfix.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
git worktree add ../bugfix bugfix-branch # work on two branches in parallelTry it Yourself »
Discussion
Loading…