Git Advanced: Git Worktrees

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Git worktrees allow you to have multiple working directories for the same repository, each checked out to a different branch.

The Problem Worktrees Solve

You’re working on a feature branch with uncommitted changes. Suddenly you need to:

  • Check something on the main branch
  • Fix a critical bug in production
  • Review a pull request

Traditional approach:

git add .
git commit -m "WIP: user authentication"
git checkout main
# do your work
git checkout feature/user-authentication

Or stash your changes. Either way, it disrupts your workflow.

What Are Worktrees?

A worktree is a separate working directory linked to your main repository. Think of it as having multiple copies of your project folder, but they all share the same Git history and can be synchronized.

Instead of switching branches, you can have multiple directories, each with a different branch checked out.

Creating a Worktree

git worktree add <path> <branch>

Example: Create a worktree for the main branch:

git worktree add ../test-main main

This creates a new directory ../test-main checked out to the main branch.

Creating a New Branch with a Worktree

git worktree add -b feature-2 ../test-feature-2

This creates both a new branch and a worktree for it.

Listing Worktrees

git worktree list

Shows all worktrees and which branches they’re on.

Removing Worktrees

When done with a worktree:

git worktree remove <path>

Force remove (if there are uncommitted changes):

git worktree remove --force <path>

Clean up stale worktrees regularly to avoid confusion and save disk space.

Use Cases

Quick Bug Fixes

# Create worktree for hotfix
git worktree add ../hotfix main

# Fix the bug in ../hotfix/
cd ../hotfix
# make changes, commit, push

# Remove when done
git worktree remove ../hotfix

Code Reviews

# Check out a PR branch
git worktree add ../review-pr feature/new-api

# Review in ../review-pr/ without affecting your current work

Working with AI Agents

Worktrees are particularly useful when working with AI coding assistants. You can have an AI work on one branch while you work on another:

git worktree add ../ai-feature feature/ai-improvements
# AI works in ../ai-feature/
# You work in your main directory

Comparing Branches

Have two worktrees side by side to easily compare implementations between branches.

Important Considerations

Disk Space

Each worktree is a full checkout of your repository. For large repositories, this adds up. Clean up worktrees you’re not using.

Conflicts

If you modify the same file in multiple worktrees, you’ll encounter conflicts when merging. Be mindful of what you’re editing.

.gitignore Files

Files in .gitignore are not copied to new worktrees. This includes your .env file.

Solution: Create a .worktreeinclude file listing files you want included:

.env

When to Use Worktrees

Good use cases:

  • Frequently switching between branches
  • Working on multiple features simultaneously
  • Working with AI agents on parallel tasks
  • Quick context switches for bug fixes or reviews

Maybe not needed:

  • Simple projects with infrequent branch switching
  • When you’re only working on one feature at a time

Worktrees are powerful but optional. Use them when they genuinely improve your productivity.

Lessons in this unit:

0: Introduction
1: Working with Remotes
2: Squashing Commits
3: Rebase vs Merge
4: Git Bisect for Debugging
5: ▶︎ Git Worktrees
6: Git Submodules
7: Understanding Detached HEAD
8: Managing Secrets in Git
9: Git Workflows and Best Practices
10: How to push to 2 repositories at the same time and keep them in sync
11: How to update a Git branch from another branch
12: Git, detached HEAD
13: Trigger deploys on Netlify with submodules
14: A Git Cheat Sheet
15: Git, squashing vs not squashing
16: An incomplete list of great Git tutorials
17: Git, what if you forgot to add a file to a commit?
18: Git workflow to manage work on multiple branches
19: How to setup the Git SSH keys