Boost Your Git Game with Worktrees

Boost Your Git Game with Worktrees
Photo by Simon Wilkes / Unsplash

Managing multiple branches in a Git repository can sometimes be cumbersome, especially when switching between them frequently. If you’ve ever wished for an easier way to handle different branches simultaneously without the hassle of constantly stashing or committing changes, Git’s worktree feature is your solution.

In this guide, we’ll explore what Git worktrees are, why they’re useful, and how you can use them to improve your workflow.

What is Git Worktree?

A Git worktree allows you to check out multiple branches from the same repository at the same time. This means you can have several working directories (worktrees) linked to a single repository, each checked out to a different branch. Unlike cloning the entire repository multiple times, worktrees share the same Git directory, making them more efficient in terms of storage

Key Features of Git Worktree:

  • Multiple Working Directories: Work on different branches simultaneously without switching.
  • Shared Git Directory: No need to duplicate the entire repository, saving space.
  • Simplified Workflow: Keep separate environments for feature development, bug fixing, or code review.

Why Use Git Worktree?

Using Git worktrees can significantly improve your productivity in scenarios where you need to:

  • Develop Features in Parallel: Work on a new feature while keeping an eye on a bugfix branch.
  • Test Multiple Versions: Easily switch between different branches to test different versions of your application.
  • Avoid Disruptions: Keep one worktree stable while experimenting in another.

For teams and individual developers alike, Git worktrees help maintain a clean and organized workflow, especially in complex projects with numerous branches.

Common Use Cases

1. Parallel Development

When working on multiple features or bug fixes simultaneously, Git worktrees allow you to maintain separate working directories for each branch. This way, you can easily switch contexts without the overhead of stash/unstash cycles.

2. Code Review

You can check out a pull request branch into a new worktree without disrupting your main development environment. This makes it easy to review code, test changes, and switch back to your main branch seamlessly.

3. Testing Different Versions

With worktrees, you can test different versions of your application without needing multiple clones of the repository. For instance, you can have a worktree for the main branch and another for a release candidate branch.

Setting Up a Git Worktree

Setting up a Git worktree is straightforward. Let’s walk through the process step by step.

Example Scenario

Let’s say you’re working on a project called awesome-project. You’ve been developing a major update on the v2 branch, but you suddenly need to fix a bug in the v1 branch. Instead of stashing your changes or switching back and forth between branches, you can use a Git worktree to manage both tasks simultaneously.

Step 1: Navigate to Your Repository

First, navigate to your repository’s root directory:

cd /path/to/awesome-project

Step 2: Create a Worktree for the v1 Branch

Now, let’s create a new worktree to work on the v1 branch while keeping your current v2 development environment intact.

git worktree add ../awesome-project-v1 v1

Here’s what this command does:

  • ../awesome-project-v1: This is the path where the new worktree will be created. It’s one level above your current directory, in a folder named awesome-project-v1.
  • v1: This is the branch you want to check out in the new worktree.

Step 3: Start Working on v1 in the New Worktree

After running the command, you’ll have a new directory called awesome-project-v1, which is checked out to the v1 branch. You can now navigate to this directory and start fixing the bug:

cd ../awesome-project-v1

Any changes you make here will be applied to the v1 branch, leaving your work on v2 untouched.

Managing and Cleaning Up Worktrees

Over time, you might accumulate several worktrees that you no longer need. Here’s how you can manage and clean them up.

Listing Existing Worktrees

To see a list of all active worktrees, use:

git worktree list

This will display the paths and associated branches of all your worktrees.

Removing a Worktree

To remove a worktree that you no longer need, use:

git worktree remove /path/to/worktree

Ensure that you’ve committed or stashed any changes you want to keep, as removing a worktree will delete its working directory.

Best Practices

  • Keep Worktrees Organized: Use descriptive directory names for worktrees to easily identify them.
  • Limit the Number of Active Worktrees: Too many active worktrees can become difficult to manage. Remove or archive worktrees that you no longer need.
  • Stay Updated: Regularly pull updates from the main repository into your worktrees to avoid merge conflicts later.
  • Avoid manually deleting the worktree folder: Never manually delete worktree folders through file explorer or terminal commands like rm -rf. Always use git worktree remove <path> to ensure that Git properly unregisters the worktree and avoids stale references that can lead to issues.

Wrap up

Git worktree is a powerful feature that can greatly improve your development workflow. Whether you're managing multiple branches or simply looking for a more efficient way to handle your projects, worktrees offer flexibility and organization without the overhead of multiple clones. By mastering Git worktree, you’ll be better equipped to handle complex workflows and improve your development process. For more in-depth details, check out the official Git worktree documentation.