git-worktree - Linux


Overview

git-worktree creates, manages, and switches between multiple working trees of a single Git repository. It allows for independent development and experimentation in separate workspaces while sharing the underlying history.

Syntax

git worktree <command> [<options>] <path> [<extra-args>]

Commands:

  • add: Add a new working tree.
  • list: List all worktrees.
  • prune: Remove untracked worktrees.
  • rm: Remove a worktree.
  • status: Show the status of a worktree.
  • use: Switch to a different worktree.

Options/Flags

  • -b : Create a new branch in the worktree.
  • -s: Sync the worktree with its parent repository.
  • –detach: Create a worktree detached from its head.
  • –lock: Lock the worktree for exclusive use.

Examples

Create a new worktree:

git worktree add worktree2

List all worktrees:

git worktree list

Switch to a different worktree:

git worktree use worktree2

Sync a worktree:

git worktree sync

Remove a worktree:

git worktree rm worktree2

Common Issues

  • Permission denied: Ensure that you have write access to the directory where the worktree is created.
  • Branch already exists: If you create a worktree on an existing branch, Git will fail. Use -b to create a new branch for the worktree.
  • Locked worktree: If a worktree is locked, other users cannot access it. Use the --lock option to lock and unlock a worktree.

Integration

  • Multiple editors: Use worktrees to edit different branches or versions of a project in separate editors.
  • Experimental testing: Create worktrees to test new features or changes without affecting the main branch.
  • Collaboration: Allow multiple people to work on different aspects of a project while sharing the same underlying repository.

Related Commands

  • git: Main Git command
  • git branch: Manage branches
  • git clone: Clone a repository
  • git checkout: Switch branches