git worktree — Multi-Branch Checkout
Worktree se hotfix karo bina apna kaam chhode.
Normally, a Git repository has ONE working directory checked out at a time. To work on a different branch, you must: stash or commit your current work, then checkout the other branch.
This is painful when you are in the middle of feature work and need to fix an urgent bug on main. You do not want to commit half-done work. Stashing can be risky for complex changes. Cloning the repo again is wasteful — it duplicates the entire .git directory and all history.
git worktree solves this: multiple working directories from ONE repository. No stashing, no cloning, no conflict.
# The traditional problem:
git checkout feature
# editing feature code...
# Urgent bug! Need to switch to main
git status
# modified: src/app.js (half-done work!)
# Option 1: Stash (risky for complex changes)
git stash
git checkout main
# fix bug, commit
git checkout feature
git stash pop # conflicts possible!
# Option 2: Clone the entire repo again
git clone https://github.com/user/repo.git repo-hotfix
cd repo-hotfix
git checkout main
# fix bug
# Now you have TWO full copies of .git — wasteful!
# Option 3: Use git worktree (BEST)
git worktree add ../hotfix main
# Instant! No cloning, no stashing, shared .git
cd ../hotfix
# Fix bug here while feature work stays untouchedgit worktree add <path> <branch> creates a NEW working directory that checks out the specified branch.
All worktrees share the SAME .git repository — no duplication of objects, refs, or history.
The main .git directory stores all objects. Each worktree has a thin .git FILE (not directory) pointing to the main .git.
Each worktree has its own index (staging area) and HEAD, so they can be at different branches simultaneously.
Git prevents you from checking out the SAME branch in two worktrees — this would cause conflicts.
Worktrees are stored in .git/worktrees/ directory with metadata about each one.
# Create a worktree
git worktree add ../hotfix-work main
# Preparing worktree (checking out 'main')
# HEAD is now at abc1234 initial commit
# What was created?
ls ../hotfix-work
# (all your project files, checked out from main)
# The worktree's .git is a FILE, not a directory
cat ../hotfix-work/.git
# gitdir: /path/to/main-repo/.git/worktrees/hotfix-work
# The main repo knows about the worktree
ls .git/worktrees/
# hotfix-work/
cat .git/worktrees/hotfix-work/gitdir
# /path/to/hotfix-work/.git
# Both share the SAME object store
# If you create a blob in one, it's visible in the other
# If you fetch in one, the other sees it too
git fetch in any worktree updates refs for all worktrees, 4) The total disk usage is only the working directory files — the .git overhead is shared. This makes worktrees much more efficient than cloning.git worktree add <path> <branch> — create worktree checking out existing branch.
git worktree add <path> -b <new-branch> — create worktree with a NEW branch.
git worktree list — show all worktrees and their locations.
git worktree remove <path> — delete a worktree.
git worktree prune — clean up stale worktree references (after manual deletion).
git worktree move <source> <destination> — move a worktree to a new location.
You can have MANY worktrees simultaneously — one per feature, one for hotfixes, one for builds.
# Create worktree with existing branch
git worktree add ../feature-work feature
# Create worktree with NEW branch
git worktree add ../experiment -b experiment main
# List all worktrees
git worktree list
# /home/user/main-repo abc1234 [main]
# /home/user/feature-work def5678 [feature]
# /home/user/experiment abc1234 [experiment]
# /home/user/hotfix-work ghi9012 [hotfix/urgent]
# Work in any worktree
cd ../feature-work
echo "new feature" >> app.js
git add . && git commit -m "feat: new feature"
# Commit is in the shared object store
# Remove worktree when done
cd /home/user/main-repo
git worktree remove ../experiment
# Force remove (if dirty)
git worktree remove --force ../experiment
# Clean up after manually deleting a worktree folder
rm -rf ../experiment
git worktree prune # removes stale referenceCANNOT checkout the same branch in two worktrees. Git prevents this to avoid conflicting changes.
Each worktree has its own HEAD and index, but they share refs (branches, tags).
Creating a branch in one worktree is visible in all others immediately.
You can't delete a branch that is checked out in another worktree.
Worktrees don't inherit all settings from the main repo — some config may need to be set per-worktree.
Some Git commands that expect a single working directory may behave unexpectedly with worktrees.
# Error: same branch in two worktrees
git worktree add ../second-main main
# fatal: 'main' is already checked out at '/path/to/main-repo'
# Solution: create a new branch instead
git worktree add ../second-main -b fix-typo main
# Error: deleting a branch checked out elsewhere
git branch -d feature
# error: The branch 'feature' is checked out at '/path/to/feature-work'
# Solution: first remove or switch the worktree
git worktree remove ../feature-work
git branch -d feature
# Checking worktree status
git worktree list
# Shows which branch is checked out where
# What happens if you manually delete a worktree folder?
rm -rf ../feature-work
# Git still thinks it exists!
git worktree list
# Still shows /path/to/feature-work (stale reference)
# Fix: prune stale worktrees
git worktree prune
git worktree list # clean nowHotfix workflow: keep feature work untouched while fixing production bugs in a separate worktree.
Parallel development: work on two features simultaneously without stashing or cloning.
Build and test: run long builds in one worktree while continuing to code in another.
Code review: check out a teammate's branch in a worktree to test it without disturbing your work.
CI optimization: some CI systems use worktrees to test multiple branches efficiently.
# Workflow 1: Hotfix while working on feature
# Main repo: working on feature (don't want to stash)
cd ~/projects/myapp
git worktree add ../myapp-hotfix main
cd ../myapp-hotfix
# Fix the bug
git add . && git commit -m "fix: critical bug"
git push origin main
cd ~/projects/myapp
# Feature work untouched! No stash, no conflict.
# Workflow 2: Test a PR without disturbing your work
git fetch origin pull/42/head:pr-42
git worktree add ../pr-review pr-42
cd ../pr-review
# Build, test, review the PR code
cd ~/projects/myapp
# Your work is exactly as you left it
# Workflow 3: Run builds in parallel
git worktree add ../build-dir main
cd ../build-dir
npm run build # slow build runs here
# Meanwhile, continue coding in main worktree
# Cleanup after PR review
git worktree remove ../pr-review
git branch -d pr-42
# Create alias for convenience
git config --global alias.wt 'worktree'
# Now: git wt add ../hotfix main
# git wt list
# git wt remove ../hotfix
git config --global alias.wt 'worktree' lets you type git wt add ../hotfix main. Also consider a naming convention for worktree directories: main-repo, hotfix-work, feature-auth-work — this keeps things organized when you have multiple worktrees.Lo kar liya — Key Points:
- ✅ git worktree allows multiple working directories from a single repository — no cloning, no stashing needed
- ✅ All worktrees share the same .git directory — objects, refs, and history are shared, saving disk space
- ✅ Each worktree has its own HEAD and index, so different branches can be checked out simultaneously
- ✅ You CANNOT checkout the same branch in two worktrees — Git prevents this to avoid conflicts
- ✅ git worktree add creates a new worktree, git worktree list shows all worktrees, git worktree remove deletes one
- ✅ If you manually delete a worktree folder, use git worktree prune to clean up stale references
- ✅ Worktrees are ideal for hotfixes, parallel feature development, and code reviews without disturbing your current work
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login