DEV Community

z z
z z

Posted on

You're Using Git Wrong — How Worktrees Will Change Your Workflow Forever

You have a pull request open. Tests are failing. Your PM asks you to fix a production bug — right now.

You have two choices:

  1. Stash your current work, switch branches, fix the bug, switch back, unstash
  2. Clone the entire repo again to a separate folder

Both are terrible. But there's a third option most developers don't know about.

What Are Git Worktrees?

A worktree is a linked copy of your repository that lets you check out a different branch in a separate directory — without switching your current working tree.

# While working on feature/login, create a worktree for a hotfix
git worktree add ../project-hotfix fix/production-crash

# Fix the bug in a SEPARATE directory
cd ../project-hotfix
# ... make changes, commit, push ...
cd ../project

# You're STILL on feature/login. Nothing stashed, nothing lost.
Enter fullscreen mode Exit fullscreen mode

No stashing. No cloning. No context switching overhead.

Why This Is a Superpower

1. Parallel Branches Without the Pain

# Review a PR without touching your current work
git worktree add ../pr-review feature/new-dashboard
cd ../pr-review
git log --oneline -5  # Check the commits
npm test               # Run the tests
cd ../project
git worktree remove ../pr-review
Enter fullscreen mode Exit fullscreen mode

2. Side-by-Side Comparison

Want to compare your branch against main? Open them in two editor windows:

git worktree add ../project-main main
# Now open ../project/ (your branch) and ../project-main/ (main) side by side
Enter fullscreen mode Exit fullscreen mode

3. CI Debugging Without Stopping Everything

# Keep working on feature/x while debugging CI on a specific commit
git worktree add ../ci-debug 7a3f9e1
cd ../ci-debug
npm ci && npm test
# ... debug CI failure without touching ../project/
Enter fullscreen mode Exit fullscreen mode

4. Documentation and README Updates

The classic "quick fix while in the middle of something":

git worktree add ../docs-update main
cd ../docs-update
# Edit README, commit, push
cd ../project
git worktree remove ../docs-update
# Back to your feature branch, zero context loss
Enter fullscreen mode Exit fullscreen mode

The Workflow That Changed My Life

Here's my daily workflow now:

# Monday morning: start feature
git checkout -b feat/new-api

# Monday afternoon: urgent hotfix
git worktree add ../hotfix main
cd ../hotfix
git checkout -b fix/crash
# fix, commit, PR
cd ../project
git worktree remove ../hotfix

# Tuesday morning: code review colleague's PR
git worktree add ../review fix/crash
cd ../review
# review, test, approve
cd ../project
git worktree remove ../review

# Back on feat/new-api. Never ran git stash once.
Enter fullscreen mode Exit fullscreen mode

Pro Tips

List all worktrees

git worktree list
# /home/me/project         (main)
# /home/me/project-hotfix  (fix/crash)
# /home/me/pr-review       (feat/new-dashboard)
Enter fullscreen mode Exit fullscreen mode

Prune stale worktrees

After deleting a worktree directory manually, clean up Git's references:

git worktree prune
Enter fullscreen mode Exit fullscreen mode

Use with VS Code

VS Code works perfectly with worktrees. Open a worktree as a new window:

code ../project-hotfix  # Opens in a new VS Code window
Enter fullscreen mode Exit fullscreen mode

Worktrees on the same branch? Yes you can

git worktree add --detach ../experiment HEAD~3
# Check out an older commit without affecting branches
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls

Issue Solution
Branch already checked out in another worktree Use git worktree add -b new-branch ../path main to branch off
"fatal: '' already exists" The directory must not exist. Git creates it for you
Worktree uses disk space Slightly more than git clone --shared (objects are linked, working tree is separate)
Can't git checkout a branch in main repo It's already checked out in a worktree. Use the --ignore-other-worktrees flag or switch in the worktree

What About Commit Messages?

Worktrees solve the "context switching" problem. But there's another time sink: writing good commit messages for each branch.

When you're context-switching through multiple worktrees, the last thing you want to do is compose a thoughtful commit message for each one. That's why I built git-copilot — a free CLI tool that reads your staged changes and generates a conventional commit message instantly:

$ git commit -m "$(git-copilot gen)"
✨ feat(api): add user routes and controller
Enter fullscreen mode Exit fullscreen mode

No AI, no API keys, no internet. Pure pattern matching. Free and open-source:

👉 git-copilot on GitHub

For teams that need commit templates, CI/CD output formats, and breaking change tracking, there's a Pro Templates Pack ($9.99).


Do you use git worktrees? What's your favorite workflow with them? Let me know in the comments.

Top comments (0)