DEV Community

Cover image for ⚙️ Tuesday Tech Tip: One Command That Changed My Workflow Forever
Sumit Roy
Sumit Roy

Posted on

⚙️ Tuesday Tech Tip: One Command That Changed My Workflow Forever

Today's game-changer: git worktree (because context switching is expensive)

The Problem

You're deep in a feature branch, everything's messy, half-committed, and then:

"Hey, can you quickly check that bug in main?"

Your options:

  1. Stash everything, switch branches, lose your mental model
  2. Clone the repo again (and wait for node_modules to install... again)
  3. Commit incomplete work with "WIP: will fix later"

All terrible.

The Solution

Image description

Git worktrees let you check out multiple branches simultaneously in separate directories:

# Create a new worktree for hotfix
git worktree add ../myproject-hotfix main

# Now you have:
# ~/myproject/          (your feature branch)
# ~/myproject-hotfix/   (clean main branch)
Enter fullscreen mode Exit fullscreen mode

The Workflow

# Working on feature-xyz
cd ~/myproject

# Bug report comes in
git worktree add ../myproject-hotfix main
cd ../myproject-hotfix

# Fix bug, commit, push
git checkout -b hotfix/urgent-fix
# ... fix and commit
git push origin hotfix/urgent-fix

# Back to feature work
cd ~/myproject
# Your half-finished work is exactly as you left it
Enter fullscreen mode Exit fullscreen mode

Why This Rules

  • No context switching - your mental model stays intact
  • No stashing - work in progress stays in progress
  • Separate node_modules - no dependency conflicts
  • Parallel development - review PRs while coding

Pro Tips

List all worktrees:

git worktree list
Enter fullscreen mode Exit fullscreen mode

Clean up when done:

git worktree remove ../myproject-hotfix
Enter fullscreen mode Exit fullscreen mode

Share .env files between worktrees:

# In your main project
ln -s ../myproject/.env .env
Enter fullscreen mode Exit fullscreen mode

My Setup

I keep a dedicated worktree for code reviews:

git worktree add ../myproject-review main
Enter fullscreen mode Exit fullscreen mode

Now I can review PRs without losing my place. Game changer for focus.


How do you handle context switching? Still stashing like it's 2015, or do you have a better trick? Share your workflow below! 🔄

Tomorrow: What I Broke Wednesday (spoiler: involves leaking credentials)

Top comments (0)