Git Worktrees The Most Underappreciated Feature

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

February 28, 2024

As a developer, you know the value of a powerful version control system like Git. But even the most robust tools can sometimes leave you wishing for a bit more flexibility. Git worktrees are here to answer that call, providing a level of development agility that will reshape the way you approach juggling tasks within a single repository.

The Struggles We've All Faced

Imagine this: You're in the zone, making great progress on a new feature. Suddenly, an urgent production bug report demands your attention. The classic pre-worktree dilemma hits – do you:

  • Stash those uncommitted changes? Even with careful notes, stashes can be messy and hard to untangle later.
  • Risk creating a complex commit? Including unfinished work with your hotfix complicates your development history.
  • Waste time with a full repository clone? That eats up precious time and disk space.

These scenarios lead to cluttered workspaces, inefficient context switching, and the potential for errors. Enter Git worktrees!

Worktrees: Your Project Multitasking Superpower

Git worktree

A Git worktree is a linked, parallel workspace within your existing repository. Imagine it as a neatly organized workbench where each tool (branch) has its dedicated area. You can switch between worktrees seamlessly, each reflecting its own distinct branch of your project's history.

Worktree Use Cases: Where the Magic Happens

  • Parallel Feature Development: Work on multiple features simultaneously. Each worktree becomes a dedicated zone, promoting focus and minimizing merge conflicts down the line.

  • Bug Fixes without Disruption: Isolate critical bug fixes in their own worktree. Stash-free switching protects your main development branch from half-finished commits.

  • Fearless Experiments: Want to test out a radical approach? Spin up a worktree for your experiment. Success? Merge it back in. Failure? Delete the worktree, no harm done.

  • Code Review Made Easy: Need to analyze an older version or a teammate's pull request in depth? Create a worktree. You get a full codebase sandbox to explore without impacting your current work.

  • Tackling Legacy Code: Sometimes you may need to work with an older version of your project. Worktrees let you check out different historical points in time side-by-side with your current work.

Getting Down to Work with Worktrees

Let's get started by cloning a repository and using the --bare flag. This flag creates a bare repository, which is a repository without a working directory. This is useful for sharing a repository with others, as it doesn't contain the actual files, only the version history.

git clone --bare [email protected]:vercel/next.js.git

If you list the contents of the directory, you'll see that it only contains the .git directory.

$> ls
$> HEAD        config      description hooks       info        objects     packed-refs refs

Now, let's create a worktree for the repository. We'll use the git worktree add command to do this. This command takes two arguments: the path to the worktree and the branch to check out.

git worktree add canary

This command creates a new worktree in the canary directory and checks out the main branch. If you list the contents of the canary directory, you'll see that it contains the files from the repository.

$> ls canary
$> LICENSE  README.md  examples  learn  packages  test  tsconfig.json

Now you can make changes to the worktree without affecting the main repository. You can also create additional worktrees for other branches.

To remove a worktree, you can use the git worktree remove command.

git worktree remove canary

This command removes the worktree and deletes the directory.

You can also use the git worktree list command to see a list of all the worktrees in the repository.

$> git worktree list
$> /path/to/repo canary [main]

Worktree Limitations

While worktrees are incredibly powerful, they do have some limitations. For example, you can't create a worktree for a repository that has uncommitted changes. You also can't create a worktree for a repository that has untracked files that would be overwritten by the worktree.

Tips for Worktree Success

  • Name them wisely: Descriptive worktree names help keep things organized.
  • Watch out for detached HEADs: Working in a worktree sometimes leads to a detached HEAD state. Don't panic! Just switch back to a named branch.
  • Automate when possible: Some common worktree tasks can be scripted for greater efficiency.

Many plugins and tools streamline worktree management. These can integrate worktrees into your editor, provide handy shortcuts, and help visualize their status within your project.

Experience the Transformation

Git worktrees aren't just a convenience; they fundamentally change the way you interact with your code. Embrace them, and you'll discover a more fluid, organized, and error-resistant development process. Get ready for a new level of Git mastery!



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using