Mastering Git Worktree
Use Cases - why and when to use
You can, of course, clone another repository of the same code base and for small repos this could work fine. However, you can do better. This is where?git worktree?comes to save the day and some space on your computer.
Git worktree is a powerful feature that allows developers to work on multiple branches simultaneously without switching between them. It creates separate working directories for different branches, enabling you to have multiple versions of your project checked out at once. Let's explore this feature through following examples of increasing complexity.
1. Your First Worktree
Let's say, you're working on the?feature-x?branch and want to create a new worktree for another?fix?.
# CWD is your main repository directory
git worktree add ../fix-branch fix-branch
This command creates a new directory named?fix-branch?one level up from your current directory, checks out the?fix-branch?there. You can now work on both branches simultaneously in separate directories, without having to clone another copy of the repository.
2. Managing Multiple Worktrees
As your project grows, you might need to juggle multiple features or bug fixes. Here's how to manage several worktrees:
# Create worktrees for different features
git worktree add ../feat-a feat-a
git worktree add ../feat-b feat-b
git worktree add ../major-bugfix major-bugfix
# List all worktrees
git worktree list
# Remove a worktree when you're done
git worktree remove ../feat-a
This example demonstrates creating multiple worktrees, listing them, and removing one when it's no longer needed.
领英推荐
3. Working with Detached HEAD
Why stop here. Often, you need to examine or work on a specific commit without creating a branch. Worktrees can help with this:
# Create a worktree for a specific commit
git worktree add --detach ../old-version randomcommit
# Make changes and create a new branch if needed
cd ../old-version
git switch -c new-hotfix
# Clean up when done
cd -
git worktree remove ../old-version
Here, we create a worktree in a detached HEAD state, make required changes to it, and potentially create a new branch from an old commit.
4. Worktrees for CI/CD
Worktrees can be invaluable in CI/CD pipelines. In an Enterprise setup, you might want to create multiple "deployables" for different environments with their own configurations, such as - for customer-x, customer-y or geo-1, goe-2 and so on... Here's an example of how you might use worktrees to build and deploy different versions of your app:
#!/bin/bash
# Create worktrees for production and staging
git worktree add ../customer-x-build customer-x
git worktree add ../geo-y-build staging
# Build production version
cd ../customer-x-build
npm install
npm run build
# Deploy x build...
# Build staging version
cd ../geo-y-build
npm install
npm run build
# Deploy staging build...
# Clean up
cd -
git worktree remove ../customer-x-build
git worktree remove ../geo-y-build
This example script creates separate worktrees for different configs, builds each version, and could be extended to deploy them to different environments.
### Tip
Use git worktree prune to remove references to worktrees that no longer exist on disk.
---
As we saw here, Git worktree is a powerful yet simple feature that can significantly improve the development workflows, especially when working on multiple features or maintaining different versions of your project. As you've seen through these examples, it can be used for simple branch management, complex CI/CD pipelines, and even automated development environments. What would you use the worktrees for? Comment and share with us.