Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Git

Git Worktrees: Work on Multiple Branches Simultaneously Without Stashing

- 23.02.26 | 23.02.26 - ErcanOPAK

🔀 The Context Switch Nightmare

You’re deep in feature work. Urgent bug reported. You stash, switch branches, fix bug, switch back, pop stash… 20 minutes lost. Your flow? Destroyed.

Git Worktrees: Multiple Working Directories, One Repo

❌ The Old Way (Painful)

# Working on feature branch
$ git status
On branch feature/new-dashboard
Changes not staged...

# Urgent bug!
$ git stash
$ git checkout hotfix/critical-bug
$ vim fix.js
$ git add . && git commit -m "Fix"
$ git checkout feature/new-dashboard
$ git stash pop

# Problems:
# - Lost 5-10 minutes switching
# - Forgot what you were doing
# - Stash conflicts
# - Can't run both branches simultaneously

✅ Git Worktrees (Seamless)

# Setup once
$ cd ~/projects/myapp
$ git worktree add ../myapp-hotfix hotfix/critical-bug
$ git worktree add ../myapp-feature2 feature/other-feature

# Your folder structure:
~/projects/
  myapp/              (main branch)
  myapp-hotfix/       (hotfix branch)
  myapp-feature2/     (feature branch)

# Now just switch folders!
$ cd ../myapp-hotfix
$ vim fix.js
$ git commit -m "Fix"
$ cd ../myapp  # Back to feature work instantly!

# Benefits:
# - Zero context switching time
# - No stashing
# - Run multiple branches at once (different ports)
# - Each worktree has its own node_modules!

🛠️ Essential Worktree Commands

# Create worktree for existing branch
git worktree add ../myapp-hotfix hotfix/critical-bug

# Create worktree AND new branch
git worktree add -b feature/new-thing ../myapp-newthing

# List all worktrees
git worktree list
# /Users/dev/projects/myapp           abc1234 [main]
# /Users/dev/projects/myapp-hotfix    def5678 [hotfix/critical-bug]

# Remove worktree (after you're done)
git worktree remove ../myapp-hotfix

# Or just delete the folder
rm -rf ../myapp-hotfix
git worktree prune  # Clean up references

# Move worktree
git worktree move ../myapp-hotfix ../archive/myapp-hotfix
Scenario Without Worktrees With Worktrees
Switch to fix urgent bug 5-10 min (stash, switch, pop) 10 sec (cd ../hotfix)
Test 2 branches together Impossible Easy (run both)
Compare branches Switch back & forth Open both in editor
node_modules conflicts Frequent (rebuild) Never (separate)

✅ Perfect Use Cases

  • Hotfix while working on feature
  • Code review (checkout PR in worktree)
  • Long-running branches (release vs main)
  • Testing multiple branches together
  • Different dependencies per branch

⚠️ Gotchas

  • Can’t checkout same branch in 2 worktrees
  • Disk space: Each worktree = full working copy
  • Must remember to prune deleted worktrees
  • VS Code: Open worktrees as separate windows

🎯 My Personal Workflow

~/projects/
  myapp/           # main - stable code
  myapp-dev/       # develop - current sprint
  myapp-hotfix/    # For urgent fixes
  myapp-review/    # For reviewing PRs

# Terminal tabs:
Tab 1: myapp-dev    (my active work)
Tab 2: myapp-hotfix (ready for emergencies)
Tab 3: myapp-review (reviewing teammates code)

# Can run all 3 on different ports simultaneously!
# myapp-dev:    localhost:3000
# myapp-hotfix: localhost:3001
# myapp-review: localhost:3002

“I haven’t used git stash in 2 years. Worktrees changed my life. When a hotfix comes in, I just cd to the hotfix folder. Feature work stays untouched.”

— Staff Engineer, Big Tech

Related posts:

Git Shows Merge Conflicts That Make No Sense

Git Bisect: The Bug-Hunting Superpower Every Developer Must Know

Git: Use Submodules for External Repositories

Post Views: 15

Post navigation

AJAX Fetch API: The Request Interceptor Pattern That Saved Our Auth System
.NET Core Middleware: Building a Production-Grade Request Pipeline

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (837)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (624)
  • Add Constraint to SQL Table to ensure email contains @ (590)
  • Average of all values in a column that are not zero in SQL (553)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (526)
  • Find numbers with more than two decimal places in SQL (468)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (837)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com