DEV Community

Cover image for How to work on multiple branches using git worktree command?
Soft Heart Engineer
Soft Heart Engineer

Posted on

How to work on multiple branches using git worktree command?

Mastering git worktree: A Complete Guide for Developers

Managing multiple branches in Git often means repeatedly switching contexts, which can slow down development workflows. Enter git worktree —a powerful Git feature that allows you to work on multiple branches simultaneously by checking them out into separate directories.

In this blog, we’ll explore what git worktree is, why it’s useful, and how to implement it effectively in your projects. With step-by-step examples and real-world use cases, this guide is perfect for web developers, software engineers, and developers aiming to boost their Git productivity.


What is git worktree?

git worktree is a Git command that enables you to have multiple working directories linked to the same repository. Each directory (or worktree) can have its own branch checked out, allowing you to work on multiple branches concurrently without the need to stash changes or repeatedly switch branches.


Why Use git worktree?

Here are some key reasons to use git worktree:

  1. Concurrent Development: Work on multiple branches simultaneously without switching contexts.
  2. Efficient Testing: Test features in one worktree while continuing development in another.
  3. Simplified Code Reviews: Easily view or update multiple branches when reviewing pull requests.
  4. Eliminate Stashing: No need to stash or commit unfinished work before switching branches.

How Does git worktree Work?

With git worktree, you can create additional working directories tied to the same Git repository. These worktrees share the same .git repository metadata, ensuring efficient use of storage and history.


Basic Syntax of git worktree

The basic syntax for git worktree is:

git worktree <command> [options]
Enter fullscreen mode Exit fullscreen mode

Common Commands:

  • add <path> <branch>: Add a new worktree for a branch.
  • list: List all active worktrees.
  • remove <path>: Remove a worktree.
  • prune: Clean up worktrees no longer in use.

Installing and Verifying git worktree

Most modern Git versions (2.5 or later) support git worktree. Verify your Git version with:

git --version
Enter fullscreen mode Exit fullscreen mode

If needed, update Git to the latest version via Git Downloads.


Using git worktree: Practical Examples

1. Adding a New Worktree

Suppose you’re working on the main branch but want to create a feature branch and work on it in parallel.

Command:

git worktree add ./feature-branch feature/new-feature
Enter fullscreen mode Exit fullscreen mode
  • ./feature-branch: Path to the new worktree directory.
  • feature/new-feature: Branch to check out in the new worktree. If the branch doesn’t exist, Git will create it.

Outcome:
A new directory named feature-branch is created, containing the code for the feature/new-feature branch.


2. Listing All Active Worktrees

To view all active worktrees associated with a repository, use:

git worktree list
Enter fullscreen mode Exit fullscreen mode

Output Example:

/project-dir                1234567 [main]
/project-dir/feature-branch abcdef0 [feature/new-feature]
Enter fullscreen mode Exit fullscreen mode

3. Removing a Worktree

Once you’re done with a worktree, you can remove it.

Command:

git worktree remove ./feature-branch
Enter fullscreen mode Exit fullscreen mode

Outcome:
The directory feature-branch is removed, but the branch feature/new-feature remains in the repository.


4. Pruning Unused Worktrees

If a worktree directory is manually deleted or corrupted, you can clean up references with:

git worktree prune
Enter fullscreen mode Exit fullscreen mode

5. Testing Code on a Separate Branch

Suppose you want to test an experimental branch (experiment) while keeping your main branch intact.

Command:

git worktree add ./experiment experiment
Enter fullscreen mode Exit fullscreen mode

You can now make changes or test the experimentbranch without affecting the main branch.


Best Practices for Using git worktree

  1. Organize Your Worktrees: Use meaningful names and paths for your worktree directories to avoid confusion.
  2. Remove Unused Worktrees: Regularly clean up worktrees you no longer need to keep your environment tidy.
  3. Avoid Nested Worktrees: Don’t nest worktrees inside each other, as it can cause issues with Git metadata.
  4. Stay Updated: Ensure your Git version supports the latest git worktree features.

Common Issues and Troubleshooting

1. Error: "Worktree already checked out"

This occurs when you try to create a worktree for a branch that is already checked out in another worktree.

Solution: Use a different branch or detach the branch in the existing worktree


2. Error: "Cannot remove worktree containing unstaged changes"

Git prevents you from removing a worktree with unsaved changes.

Solution: Commit, stash, or discard the changes before removing the worktree.


Advantages of Git Worktree Over Other Workflows

Feature git worktree git clone Branch Switching
Shared History ❌ (separate history)
Disk Space Efficiency ✅ (shared repository) ❌ (duplicates repo)
Simultaneous Branches
Easy Cleanup ❌ (manual removal)

Conclusion

git worktree is a game-changing tool for developers working on multiple branches or tasks simultaneously. By creating additional worktrees tied to the same repository, you can streamline your workflows, improve productivity, and eliminate the need for constant branch switching.

Key Takeaways:

  • Use git worktree add to create separate working directories for different branches.
  • Clean up unused worktrees with git worktree remove or git worktree prune.
  • Incorporate git worktree into your development process to simplify branch management and testing.

Start using git worktree in your projects today and enjoy the flexibility it offers for multi-branch workflows!


Further Reading

Top comments (0)