Skip to main content
Latest Blog Posts

Git Worktree: Managing Multiple Branches Without the Context-Switching Headache

AM

Arthur Marcel

Founder & AI Consultant

English

Hey there ! Look... If you're a dev, you've been there: deep into a complex feature, files everywhere, and suddenly a critical production bug hits. Your usual move ? git stash, switch branches, hope your environment doesn't break, and later try to piece back your train of thought. But what if you could just open a new folder and have another branch running in parallel, powered by the same local repo ?

That’s exactly what Git Worktree offers. Instead of a single working directory, it allows you to maintain multiple linked trees attached to one object database. Essentially, you save disk space and dodge the cognitive cost—those roughly 23 minutes it takes to regain deep focus—of switching contexts in the same directory.

The magic lies in the architecture: Git keeps a main repository and creates linked instances. Commits and history are shared via $GIT_COMMON_DIR, but each folder has its own HEAD and staging area (index). This means running git add in one folder won't leak into the other. It just works !

Keep in mind the "Golden Rule": you cannot check out the same branch in two different worktrees simultaneously. Git blocks this to prevent pointer corruption and data loss. If you absolutely need to inspect the same history in two places, using Detached HEAD mode or creating a temporary branch is the way to go.

For a "state-of-the-art" setup, I recommend the Bare Repository topology. You clone with the --bare flag, creating a central hub without visible files, and then add worktrees as sibling folders (e.g., a /main folder and a /feature-x folder). This keeps your workspace clean and prevents your .gitignore from becoming a mess of relative paths.

Even if you’re riding the AI Agent wave (like Claude Code), worktrees are a game-changer. You can have an agent refactoring code in one worktree while you handle business logic in another, ensuring their file changes don't collide in real-time. Productivity stays high, and the headache stays low, hehe.

To wrap up, don't forget maintenance. If you manually delete a folder, run git worktree prune to clear out "zombie" references in the database. Also, be cautious with submodules; the integration is still a bit tricky and can lead to unnecessary file duplication.

Ready to try it out ? Run this in your terminal: git worktree add ../quick-fix main. You’ll realize how liberating it is to finally stop stashing every five minutes !

Sources:

  • Git Official Documentation (git-scm.com).
  • "Mastering Git Worktree" - Medium.
  • "How I Supercharged My Workflow with Git Worktrees" - DEV Community.
AM

about_author

Arthur Marcel é founder da AMS tech com 25+ anos de experiência atuando na interseção entre tecnologia, produto e negócios. Sua visão 360° conecta soluções técnicas com objetivos claros de negócio, priorizando sempre o princípio de safety-first em projetos de IA e automação.

Conectar no LinkedIn