DEV Community

Alex Chen
Alex Chen

Posted on

Quick Tip: Work on Two Git Branches at Once with `git worktree` (No Stashing)

Quick Tip

You're mid-feature with 14 dirty files. Production is on fire. Instead of git stash roulette:

git worktree add ../hotfix main
cd ../hotfix
# fix the bug, commit, push
git worktree remove ../hotfix
Enter fullscreen mode Exit fullscreen mode

git worktree checks out another branch into a separate directory — same repo, shared .git, no re-clone, no stash. Your feature branch sits untouched in the original directory.

Commands I actually use weekly:

git worktree list                 # see all active worktrees
git worktree add -b fix/typo ../typo main   # create branch + worktree together
git worktree remove ../typo       # clean up when done
Enter fullscreen mode Exit fullscreen mode

Where this saved me last sprint: reviewing a teammate's PR while my own half-done migration was mid-refactor. PR in ../review, my mess stayed put, zero context switch.

Caveat: two worktrees can't check out the same branch — that's the whole point of the isolation.

Found this while cleaning up a MonkeyCode-assisted monorepo workflow: https://ly.cyberserval.tech/iIETXiF

Team stash or team worktree — which one lives in your muscle memory?

Top comments (1)

Collapse
 
topstar_ai profile image
Luis Cruz

I love how you've highlighted the utility of git worktree for managing multiple branches without the hassle of stashing. It's indeed a game changer for maintaining focus while juggling urgent fixes and ongoing feature work. One practical insight I've found is to integrate git worktree into CI/CD pipelines for temporary environments, streamlining testing and reviews. If you're exploring ways to enhance your monorepo workflow further, I’d be interested in collaborating on this aspect. What additional features do you think could make this setup even more efficient?