Git Worktree: Checking Out Branches in Different Directories

Git worktrees provide a flexible way to work with multiple branches in separate directories.

Basic Syntax

The basic syntax for creating a new worktree is:

git worktree add <path> <branch>

Important Note: You should run these commands from within your main Git repository directory. This ensures that Git understands the context of your worktree operations.


Example 1: Basic Usage

To check out the branch myotherbranch into a new directory at the same level as your current repository:

git worktree add ../myproject-myotherbranch myotherbranch

Using ../ places the new worktree directory next to your current repository directory, which is often preferred for organization and clarity.

Example 2: Creating a New Branch

To create a new branch named feature-branch and check it out in a new directory:

git worktree add -b feature-branch ../feature-branch main

This creates a new branch feature-branch based on main and checks it out in the ../feature-branch directory.

Benefits of Using Worktrees

  1. Work on multiple branches simultaneously without switching
  2. Easily compare different branches side by side
  3. Build and test different versions of your project concurrently

Managing Worktrees

  • List all worktrees: git worktree list
  • Remove a worktree: git worktree remove <path>
  • Prune worktrees: git worktree prune

Git Worktree: Checking Out Branches in Different Directories

[Previous sections remain unchanged]

Best Practices

  1. Use descriptive directory names for your worktrees

    Clear naming helps you quickly identify the purpose of each worktree.


    git worktree add ../myproject-feature-login feature-login
    git worktree add ../myproject-bugfix-123 bugfix-123
  2. Regularly prune unused worktrees to keep your workspace clean

    Pruning removes references to deleted worktrees, preventing clutter.


    # List worktrees to see what exists
    git worktree list
    # Prune any worktrees that no longer exist on disk
    git worktree prune
    # List again to confirm cleanup
    git worktree list
  3. Be cautious when deleting worktree directories manually

    Manually deleting worktree directories without using Git commands can cause reference issues.

    Example of proper removal:

    # Remove a worktree properly
    git worktree remove ../myproject-feature-login
    # If the worktree is gone but Git still references it, force prune
    git worktree prune --expire now
  4. Consider using ../ to place worktrees alongside your main repository

    This keeps all project-related directories at the same level, improving organization.

    Example directory structure:

    ├── myproject (main repository)
    ├── myproject-feature-login (worktree)
    └── myproject-bugfix-123 (worktree)
  5. Use worktrees for long-running features or experiments

    Worktrees are ideal for work that might take several days or weeks, allowing you to easily switch contexts.


    # Create a worktree for a long-running feature
    git worktree add ../myproject-major-refactor major-refactor
    # Create another for an experimental idea
    git worktree add -b experiment ../myproject-experiment main
  6. Regularly fetch and update all worktrees

    Keep all worktrees up-to-date with remote changes to avoid conflicts.


    # Update main repository
    git fetch --all
    git pull
    # Update each worktree
    cd ../myproject-feature-login
    git pull
    cd ../myproject-bugfix-123
    git pull

Leave a Reply

Your email address will not be published. Required fields are marked *