Skip to content

Git Worktree

Git Worktrees are a feature allowing you to work on multiple branches at the same time in separate directories. Each worktree is a working copy of your repository linked to a different branch.

Basic Usage

Create a Worktree: Utilise the command

Terminal window
git worktree add ../new-branch-name new-branch-name

Navigate Worktrees: Use regular Git commands. Current worktrees can be seen using

Terminal window
git worktree list

Prune Worktrees: After removing a worktree directory, clean up the meta information using

Terminal window
git worktree prune

When To Use Git Worktrees?

Concurrency Over Stashing

Typically, developers use git stash to switch context between different tasks across different branches. However, this might clutter your stash list with frequent context switching and could lead to confusion. On the other hand, Git worktrees allow you to work on different branches simultaneously, thereby eliminating the need for stashing and un-stashing repeatedly. It’s as if each branch had its own sandbox, allowing for smoother task transitions.

Debugging While Developing

Instead of pausing your work and stashing changes to inspect a bug on a live branch, Git worktree allows you to quickly switch contexts and debug the issue in a completely isolated environment.

Long-Running Tasks

Have a branch that requires a long compile time or batch processing? Kick off the task in one worktree and work on something else in another.

Code Review and Testing

You can have an isolated environment for each pull request by checking them out in different worktrees. This makes the code review and testing process more streamlined compared to switching branches within the same directory.

Conflict Resolution

Git worktrees can also assist with complex conflict resolutions. Instead of complicating your main working directory, you can create a new worktree where you can carefully resolve merge conflicts, test your changes, and ensure everything works as expected.