Git Worktree Guide

Documentation for Developers

Git Worktree — Work on Another Branch Without Leaving Yours

git worktree lets you check out a second branch in a separate directory while your current branch stays untouched. No stashing, no switching, no risk.

When to Use

Quick Reference (3 commands)

# 1. Create a worktree (checks out the branch in a temp directory)
git worktree add /tmp/my-worktree <branch-name>

# 2. Do your work there (cd, edit, commit, push — whatever you need)
cd /tmp/my-worktree
# ... make changes ...
git add -A && git commit -m "fix: description"
git push origin <branch-name>

# 3. Clean up (back in your main repo)
cd /path/to/main/repo
git worktree remove /tmp/my-worktree

Real Example: Patching a Deploy Branch

You're on integrate/150-151-152 with a dev server running. You need to push a fix to deploy/vitto-mobile.

# Create worktree for the deploy branch
git worktree add /tmp/deploy-fix deploy/vitto-mobile

# Apply the patch there
cd /tmp/deploy-fix
git apply /path/to/fix.patch
git add -A
git commit -m "fix(vitto-mobile): register Capacitor plugins"
git push origin deploy/vitto-mobile

# Clean up — back to your main workspace
cd ~/developer/repos-gspace/gspace1
git worktree remove /tmp/deploy-fix

# Your branch is still integrate/150-151-152, dev server still running

How It Works

Main repo (~/repos-gspace/gspace1/)     Worktree (/tmp/deploy-fix/)
┌─────────────────────────────┐         ┌─────────────────────────┐
│ Branch: integrate/150-151   │         │ Branch: deploy/vitto    │
│ Status: untouched           │         │ Status: independent     │
│ Dev server: still running   │   ───►  │ Full checkout of branch │
│                             │  same   │ Own working tree        │
│                             │  .git   │ Can commit & push       │
└─────────────────────────────┘         └─────────────────────────┘

Both directories share the same .git database — no extra cloning, no wasted disk space.

Useful Commands

# List all active worktrees
git worktree list

# Remove a worktree (must be clean — no uncommitted changes)
git worktree remove /tmp/my-worktree

# Force remove (if you have uncommitted changes you want to discard)
git worktree remove --force /tmp/my-worktree

# Prune stale worktree entries (if you deleted the directory manually)
git worktree prune

Rules

  1. One branch per worktree — you can't have the same branch checked out in two places
  2. Use /tmp/ for short-lived worktrees — keeps things disposable
  3. Always git worktree remove when done — don't just rm -rf the directory (use prune if you do)
  4. Commits in a worktree are real — they go to the same repo, same branches, same remote