A comprehensive, hands-on tutorial for mastering Git worktrees to supercharge your development workflow, especially in the era of AI-assisted coding.
Git worktrees allow you to check out multiple branches simultaneously into separate directories. This is incredibly powerful for:
- Context Switching: Work on urgent bug fixes without stashing your feature work
- Parallel Development: Develop multiple features concurrently
- AI-Assisted Development: Run multiple AI coding agents on different tasks simultaneously
- Code Review: Keep a clean main branch for reviewing while working on features
- Experimentation: Try different approaches without affecting your main work
This repository is a GitHub template designed to teach you git worktrees through hands-on exercises.
- Git 2.5+ (worktrees were introduced in Git 2.5)
- Basic familiarity with Git branches
- Terminal/command line access
- Use this template to create your own repository
- Clone your new repository locally
- Follow the exercises in order (see Exercises below)
- Use the provided scripts to practice and validate your learning
A worktree is a separate working directory linked to your repository. Each worktree can have:
- Its own checked-out branch
- Independent file changes
- Separate index/staging area
Traditional Workflow Problems:
# Working on feature-a
git add .
git stash
git checkout main
git pull
git checkout -b hotfix-urgent
# Fix bug, commit, merge
git checkout feature-a
git stash pop
# Lost context, mental overheadWorktree Solution:
# Working on feature-a in main directory
git worktree add ../myproject-hotfix hotfix-urgent
cd ../myproject-hotfix
# Fix bug in separate directory, no stashing neededWork through these exercises in order to master git worktrees:
Learn to create, list, and remove worktrees
Work on multiple features simultaneously without context switching
Handle urgent bug fixes while preserving feature work
Use worktrees with AI coding assistants for parallel development
Properly manage and clean up worktrees
This repository includes several scripts to help you practice:
scripts/setup-worktrees.sh- Initialize practice environmentscripts/cleanup-worktrees.sh- Clean up all practice worktreesscripts/worktree-status.sh- Show status of all worktreesscripts/validate-exercise.sh- Check your exercise solutions
- Command Cheatsheet - Quick reference for all git worktree commands
- Best Practices - Advanced workflows and tips
- AI Development Guide - Using worktrees with AI coding tools
This repository includes a comprehensive Slidev presentation to showcase Git worktrees concepts and tutorial overview.
cd slides
npm install # First time only
npm run dev # Start presentation serverThe presentation covers:
- Problem statement and Git worktrees solution
- Real-world scenarios and use cases
- Tutorial repository walkthrough
- Exercise highlights with code examples
- Best practices and AI development workflows
npm run dev # Development server with hot reload
npm run build # Build for production deployment
npm run preview # Preview production build
npm run export # Export slides to PDFπ‘ Perfect for: Team presentations, conference talks, or personal learning reinforcement.
The sample-project/ directory contains a simple web application with intentional bugs and TODO items for practicing worktree workflows in realistic scenarios.
You're implementing a complex feature when a critical production bug is reported. With worktrees:
- Keep your feature work untouched in the main directory
- Create a hotfix worktree:
git worktree add ../project-hotfix main - Fix the bug in the hotfix directory
- Deploy the fix
- Return to your feature work without any context loss
Run multiple AI coding agents simultaneously:
- Main worktree: Claude Code working on authentication refactor
- Feature worktree: Another AI agent building a new dashboard
- Review worktree: Clean main branch for code review
- Each agent works independently without conflicts
Keep a dedicated worktree for code reviews:
git worktree add ../project-review main
# Always have a clean main branch for reviewing PRs
# Continue feature work in your main directory~/workspace/
βββ myproject/ # Main worktree (usually main branch)
βββ myproject-feature-a/ # Feature branch worktree
βββ myproject-hotfix/ # Hotfix worktree
βββ myproject-review/ # Code review worktree
Add these to your .gitconfig:
[alias]
wt = worktree
wtlist = worktree list
wtadd = worktree add
wtremove = worktree remove
wtprune = worktree pruneEach worktree appears as a separate folder in VSCode, allowing you to:
- Open multiple instances of your project
- Work on different features in separate windows
- Use different extensions/settings per worktree
Found an issue or want to improve the tutorial? Please:
- Create an issue describing the problem or improvement
- Use the provided issue templates
- Submit a pull request with your changes
This tutorial is open source and available under the MIT License.
Having trouble with the exercises? Check out:
- GitHub Issues for common problems
- Discussions for questions and tips
- The
docs/directory for additional resources
Ready to supercharge your development workflow? Start with Exercise 1!