Git Worktree

1 min

Table of contents

  1. What Is Worktree?
  2. Why Worktree?
  3. Practical Use
  4. Essential Commands

1. What Is Worktree?

Git worktree = Have several working folders for the SAME repo.

Concept:

  • 1 Git repo = normally 1 folder
  • With worktree = 1 repo, several folders
  • Each folder can be on a different branch

Analogy: It is like having several desks for the same project.

Back to table of contents

2. Why Worktree?

Classic problem:

bash
# You are working on feature-A
git checkout feature-A
# Code, code, code... (unfinished work)

# EMERGENCY: bug on main!
git stash                # Save work in progress
git checkout main        # Switch to main
# Fix the bug...

git checkout feature-A   # Go back to your work
git stash pop           # Recover your work in progress

Worktree solution:

bash
# You are working on feature-A in the main folder
# Emergency? Create a new folder for main!
git worktree add ../hotfix main

# Now:
# - ./           → feature-A (your work continues)
# - ../hotfix    → main (for the urgent fix)

Benefit: No stash/switch. Just 2 folders, 2 parallel tasks!

Back to table of contents

3. Practical Use

Basic setup:

bash
mkdir demo-worktree && cd demo-worktree
git init

echo "print('main v1')" > app.py
git add . && git commit -m "Main version"

# Create a feature branch
git checkout -b feature-new
echo "print('feature work')" > feature.py
git add . && git commit -m "Feature work"

git checkout main

Create worktrees:

bash
# Create a worktree for feature-new
git worktree add ../feature-work feature-new

# Create a worktree for a new branch
git worktree add ../hotfix-branch -b hotfix

# See all worktrees
git worktree list

Result:

demo-worktree/       → main branch
../feature-work/     → feature-new branch
../hotfix-branch/    → hotfix branch (new)

Work in parallel:

bash
# Terminal 1: Main folder (main)
cd demo-worktree
echo "print('main updated')" >> app.py
git add . && git commit -m "Update main"

# Terminal 2: Feature work
cd ../feature-work
echo "print('feature complete')" >> feature.py
git add . && git commit -m "Complete feature"

# Terminal 3: Hotfix
cd ../hotfix-branch
echo "print('urgent fix')" > fix.py
git add . && git commit -m "Critical fix"

Magic: 3 branches, 3 folders, 0 context switching!

Back to table of contents

4. Essential Commands

CommandAction
git worktree add <path> <branch>Create a worktree on an existing branch
git worktree add <path> -b <new-branch>Create a worktree + new branch
git worktree listSee all worktrees
git worktree remove <path>Delete a worktree
git worktree pruneClean up deleted worktrees

Typical use cases:

1. Urgent hotfix:

bash
git worktree add ../hotfix main
cd ../hotfix && echo "fix" > fix.py
git add . && git commit -m "Hotfix"
git push origin main

2. PR review:

bash
git worktree add ../pr-review pr-branch
cd ../pr-review  # Test the PR without disturbing your work

3. Branch comparison:

bash
git worktree add ../version-a branch-a
git worktree add ../version-b branch-b
# Compare files between ../version-a and ../version-b

Cleanup:

bash
git worktree remove ../feature-work
git worktree remove ../hotfix-branch
git worktree prune                    # Clean up references

Worktree = Git multitasking without compromise!

Back to table of contents