Git Worktree: A Practical Guide (with Examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

When working on multiple features or fixing bugs in any given project, it’s crucial to organize your workflow efficiently. Git is a powerful version control system deeply embedded in the daily routines of developers worldwide. One of its lesser-known features that can significantly enhance workflow is the git worktree. In this tutorial, we’ll cover the basics of git worktree, dive into some real-world examples, and learn how to integrate this tool into your daily development practice effectively.

What is Git Worktree?

Git worktree allows you to have multiple working trees attached to the same repository. This can be tremendously useful when you want to work on two branches simultaneously without having to switch contexts constantly. Before diving into the examples, make sure that your Git version is 2.5 or newer, which is when git worktree was introduced.

Creating a New Worktree

git worktree add ../path_to_new_worktree branch_name

By running this command, you create a new directory on the specified path and a new worktree checked out to the given branch. This enables you to work on multiple branches without the overhead of stashing or committing unfinished work.

Example: Fixing a Bug While Developing a Feature

Imagine you’re working on a new feature in a topic branch called feature-x, and you need to address a critical bug that’s popped up on main.

# Assume you're currently on feature-x branch
git worktree add ../bugfix main

This command creates a new folder named bugfix in the parent directory, checked out to the main branch. You can now fix the bug in the bugfix directory without disturbing your feature development in the current worktree.

Pruning Worktree

git worktree prune

Over time, you might have stale worktrees that are no longer needed. Running the prune command cleans up any worktree entries that have been removed from the file system.

Advanced Usage: Bisecting with Worktrees

git bisect helps to identify the commit that introduced a bug. Combining this with worktrees allows you to bisect without disrupting your current work.

# Start git bisect in the main worktree
git bisect start bad_commit good_commit
# Create a new worktree for the bisect operation
git worktree add ../bisect-worktree HEAD
cd ../bisect-worktree
# Now you can build and test in the bisect-worktree
git bisect good # or bad, depending on test outcome

In this example, bisect operations are performed in a separate worktree, enabling seamless context switching.

List All Worktrees

git worktree list

The list command gives you an overview of all the worktrees linked to the repository along with their associated branches.

Best Practices and Tips

  • Keep your worktrees organized by naming them sensibly; this becomes crucial as the number of worktrees grows within a repository.
  • Remember to prune regularly to maintain clarity in your local development environment.
  • While worktrees are best suited for parallel development, avoid using too many at the same time as it can become challenging to manage.

Conclusion

Git worktree is an invaluable feature for developing features in tandem, fixing bugs, or doing experiments without compromising on the progress of other tasks within the same repository. By incorporating git worktree into your Git workflow, you’ll enable a cleaner, more organized environment, leading to increased productivity and efficiency in your development career.