DEV Community

abdellah ht
abdellah ht

Posted on • Originally published at

Understanding Git Worktrees: A Solution to Multi-Branch Development


Imagine you're working on a new feature in a Git repository, and suddenly, you need to switch to another branch to fix a critical bug. You could commit your changes, switch branches, and then switch back, but that's cumbersome and can lead to a messy commit history. Alternatively, you could stash your changes, switch branches, and then apply the stash, but that's not always ideal either.

This is where Git worktrees come in. Git worktrees allow you to check out multiple branches simultaneously, each in its own working directory, without the need for stashing or committing changes.

In this article, we'll explore what Git worktrees are, the problems they solve, how they compare to git stash, and briefly touch on some other alternatives for managing multi-branch development.

Setting Up a Worktree

To create a new worktree, you can use the following commands:

# Create a new worktree for an existing branch
git worktree add ../path/to/new-worktree branch-name

# Create a new worktree and a new branch
git worktree add -b new-branch ../path/to/new-worktree
orktree add <path> <branch>
Enter fullscreen mode Exit fullscreen mode

This will create a new worktree in the specified directory and check out the specified branch. You can now work on the new branch independently of the original worktree.

Comparing Worktrees to Git Stash

Git stash is another popular feature used to save uncommitted changes temporarily. While it serves a similar purpose to worktrees, there are some key differences:

  • Visibility: Stashed changes are hidden from the working directory, making it easy to forget about them. Worktrees, on the other hand, provide a more visible and structured way to manage multiple branches.
  • Context switching: With stashes, you need to remember which stash corresponds to which branch. Worktrees provide a more intuitive way to switch between branches.
  • Workflow: Stashes are more suited for short-term changes that you plan to apply quickly. Worktrees are better for long-term context switching and multi-branch development.

Other Alternatives

While worktrees and stashes are the most common solutions for managing multiple branches, there are a few other alternatives worth exploring:

Besides git stash, there are a few other strategies and tools to manage multi-branch development:

Multiple Clones:

You can clone the same repository into different directories. While this allows simultaneous work on multiple branches, it is highly inefficient in terms of disk space and can lead to synchronization issues.

Git Submodules:

Git submodules allow you to keep a Git repository as a subdirectory of another Git repository. This can be useful for including dependencies but is not ideal for handling multiple branches of the same project.

Git Subtrees:

Git subtrees allow you to merge and split repositories without the need for submodule management. This is useful for including external repositories but does not address the issue of switching between branches within the same project.


Git worktrees are a powerful feature that can significantly improve your multi-branch development workflow.
By allowing you to work on multiple branches simultaneously without the hassle of managing stashes or multiple clones, worktrees provide a cleaner and more efficient way to switch contexts.
If you frequently find yourself juggling multiple branches, consider incorporating worktrees into your Git workflow for a more seamless development experience.

Top comments (0)