DEV Community

Cover image for Git Worktree: Managing Multiple Working Trees in Git
PGzlan
PGzlan

Posted on

Git Worktree: Managing Multiple Working Trees in Git

Table of Contents:

Git is a powerful version control system widely used to track changes in your projects. One of the coolest (and sadly lesser-known) features of Git is the Git Worktree which allows you to manage multiple working trees within the same repository.

Work what now?

Worktree! (I mean for a something that has a lot of branches, it makes sense to call it a worktree). Git Worktree is a feature introduced in Git 2.5 that enables you to have multiple working trees associated with a single repository.
Each working tree is a separate directory with its own set of files, allowing you to work on different branches or commits simultaneously without the need for multiple copies of the repository.

Using it allows you to switch between branches or commits quickly and easily, keeping your changes separate and organized. It is particularly useful when you need to work on different features or bug fixes simultaneously or when you want to review changes on a separate branch without affecting your current work.

Setting up a Git Worktree

To create a new Git Worktree, you can use the git worktree add command followed by the path where you want to create the new working tree and the branch or commit you want to associate with it. For example:

git worktree add <path> <branch/commit>
Enter fullscreen mode Exit fullscreen mode

Seems pretty straight forward and easy. But what's so useful about it? Well, let's dive into some practical use cases where Git Worktree benefits really shine.

1. Simultaneous Feature Development

Imagine you are working on a project that requires the development of multiple features simultaneously. Instead of switching back and forth between branches, Git Worktree allows you to work on each feature in its own separate working tree.

Let's say you have a master branch and two feature branches: feature-a and feature-b. You can create a working tree for each feature and switch between them effortlessly using Git Worktree. Here's how you can set it up:

# Create a worktree for feature-a
git worktree add ../feature-a feature-a

# Create a worktree for feature-b
git worktree add ../feature-b feature-b
Enter fullscreen mode Exit fullscreen mode

Now you can navigate to the respective directories and work on each feature independently. Any changes made in one working tree will not affect the others, allowing you to keep your work organized and separate.

2. Reviewing Changes on a Separate Branch

When you are in the development process, code reviews are considered business as usual. Git Worktree helps you easily review changes on a separate branch without disturbing your current work.

Let's say you are working on the develop branch, and a colleague has requested a review of their changes on the feature-x branch. Instead of switching to the feature-x branch in your current working tree, you can create a new worktree specifically for reviewing the changes:

git worktree add ../review-feature-x feature-x
Enter fullscreen mode Exit fullscreen mode

Now you can navigate to the review-feature-x directory and review the changes without affecting your current work on the develop branch. You can compile, test, and provide feedback on the code changes with ease.

3. Building and Testing Different Versions

In some cases, you may need to build and test different versions of your project, such as stable releases or experimental features. Git Worktree can help you manage these different versions efficiently.

Let's say you have a stable release branch named v1.0 and a development branch named dev. You can create separate working trees for each version:

# Create a worktree for the stable release
git worktree add ../v1.0 v1.0

# Create a worktree for the development version
git worktree add ../dev dev
Enter fullscreen mode Exit fullscreen mode

Now you can navigate to each directory and build, test, and deploy the respective version independently. This approach ensures that the changes made in one version do not interfere with the other, allowing you to maintain project stability while working on new features.

4. Hotfixes and Critical Bug Fixes

When critical bugs or security vulnerabilities are discovered in a released version of your software, you need to address them promptly. Worktrees can be handy in such situations by allowing you to create a separate working tree for the hotfix or bug fix branch.

The released version branch of your software is v1.2 and you need to fix a critical bug. Instead of switching to the v1.2 branch in your current working tree, you can create a new worktree specifically for the hotfix:

git worktree add ../hotfix-v1.2 v1.2
Enter fullscreen mode Exit fullscreen mode

Now you can navigate to the hotfix-v1.2 directory, make the necessary changes, and test them without interrupting your work on other branches. Once the hotfix is ready, you can merge it back into the appropriate branches and deploy the fix without any delays.

Conclusion

Git Worktree is a powerful feature that allows you to manage multipleworking trees within a single Git repository. It enables simultaneous feature development, easy code review on separate branches, efficient management of different versions, and streamlined handling of hotfixes and critical bug fixes.

By integrating Git Worktree into your workflow, you can work on different branches or commits simultaneously without the need for multiple copies of the repository. This not only improves productivity but also helps maintain code integrity and organization.

So, the next time you find yourself needing to work on multiple branches or versions, or when you need to review changes or address critical issues, consider using Git Worktree to manage your multiple working trees effortlessly.

References

Git Kraken
Git Documentation

Top comments (0)