github

What are git worktrees, and why should I use them? (opens in new tab)

Git worktrees let developers check out multiple branches simultaneously in separate directories, avoiding the stash-and-switch cycle. They preserve editor state, reduce context-switching friction, and make parallel work—especially AI-assisted development—much easier. Their main drawbacks are dependency duplication, folder cleanup, and restrictions on checking out the same branch twice.

Switching Contexts with Branches and Stashing

  • Traditional urgent-work flow often requires:
    • Stashing unfinished changes.
    • Checking out and updating main.
    • Creating a hotfix branch.
    • Committing, pushing, and merging the fix.
    • Returning to the original branch and restoring the stash.
  • This process creates mental overhead and may involve reloading files, reinstalling dependencies, or resolving stash conflicts.
  • Some developers compensate with multiple repository clones or increasingly complex stash commands.

Working in Parallel with Worktrees

  • A worktree creates another working directory connected to the same Git repository:
    git worktree add ../hotfix-workspace -b hotfix-bug main
    
  • The original feature branch and editor remain untouched while the hotfix is developed in a separate folder.
  • After merging, the temporary worktree can be removed:
    git worktree remove ../hotfix-workspace
    
  • Worktrees eliminate stash conflicts and support truly parallel development.
  • Tools such as VS Code provide built-in worktree support.

Why Worktrees Are More Popular Now

  • Worktrees have existed since 2015 but were historically overlooked because Git GUIs offered limited support.
  • Developers increasingly run multiple tasks, coding sessions, reviews, and AI agents simultaneously.
  • Modern tools, including the GitHub Copilot app, use worktrees as a default way to isolate parallel sessions.

Limitations to Consider

  • Dependency bloat: Each worktree may contain its own node_modules, Python packages, or other dependencies.
  • Folder management: Temporary worktrees must be deleted to prevent clutter.
  • .gitignore concerns: Worktrees created inside the repository may need to be ignored; placing them outside the repository avoids this issue.
  • One-branch restriction: Git prevents the same branch from being checked out in multiple worktrees simultaneously.

Worktrees in the GitHub Copilot App

  • New Copilot sessions can be created in a new worktree by default.
  • The app displays the generated worktree name, location, associated project, and changes.
  • Worktree management is integrated into the session workflow.

Worktrees are especially useful for parallel development and AI-assisted workflows, but they are not mandatory. Developers can use worktrees, traditional branching and stashing, or a combination depending on their workflow and resource constraints.