Git commands

git worktree

git worktree: manage multiple working trees attached to one repository.

The git worktree command lets you check out the same Git repository into multiple directories at the same time. Each worktree has its own files, its own HEAD, and its own working tree, so you can keep different branches open side by side without constantly switching back and forth in one directory.

All worktrees still belong to the same repository and share the same Git object database and most repository metadata. This makes a worktree much lighter than creating a second full clone just to inspect another branch, fix an urgent bug, or run a quick experiment in isolation from your current files.

A common use case is letting an AI agent work on a task in parallel with your main work. The AI works in its own branch, checked out in a separate directory. This way, the AI agent can't mess with your main working tree, where you can keep coding by hand. In essence, creating a worktree is similar to cloning the repository into a different directory, but it's slightly faster.

Git worktree vs. full clone in separate dir

Pros of worktrees

  • Faster to create, because it doesn't need to copy all repository data.
  • Uses less disk space because repository data is shared between worktrees on the same computer.
  • Many dev tools now integrate worktrees, especially for AI agent workflows.

Cons of worktrees

  • The same branch usually cannot be checked out in two worktrees at the same time.
  • Changing repository config or fetching affects all worktrees attached to that repository.
  • If you need a fully independent sandbox with separate remotes or config, a full clone is usually better.

Examples

Create a new worktree for an existing branch called release:

git worktree add ../project-release release

Create a new worktree, create a new branch called hotfix/login, and start it from main:

git worktree add -b hotfix/login ../project-hotfix main

List all worktrees attached to the current repository:

git worktree list

Remove a worktree that you no longer need:

git worktree remove ../project-hotfix
© 2024-2026 GitByBit.All rights reserved.