Skip to content

The Deska blog

The Best Way to Manage Git Worktrees

Learn the best way to manage Git worktrees for efficient context switching and how to integrate them into your development workflow with modern tools.

10 min read

Developing software often requires jumping between different tasks. You might be halfway through a complex feature when an urgent bug report arrives or a peer asks for a code review. The traditional approach involves stashing changes or committing WIP code, but many developers find that the best way to manage Git worktrees provides a much cleaner solution. Instead of twisting a single working directory to fit multiple branches, worktrees allow you to have multiple branches checked out simultaneously in different folders.

Understanding the Git Worktree Model

A standard Git repository consists of a hidden .git folder and a single working tree. When you switch branches, Git replaces the files in your directory to match the target branch. Git worktrees change this relationship by allowing a single repository to support multiple linked working trees. Each worktree is a separate directory on your file system, but they all share the same central .git history and configuration.

This architecture solves the primary friction point of context switching. When you use git checkout, you lose your uncommitted state, your build artifacts, and your IDE setup for the current task. With worktrees, you simply move to a different directory. The files for your feature remain exactly as you left them, while your bug fix happens in a completely isolated environment.

Why Worktrees Beat Stashing and Branch Switching

For years, git stash was the standard recommendation for interruptions. While functional, stashing has several drawbacks that make worktrees a superior choice for high velocity teams.

  • Persistent State: Stashing only saves tracked or specifically included files. It does not preserve your running terminal processes, your debugger state, or your IDE’s open files. A worktree is a physical directory that stays exactly as you left it.
  • Parallel Execution: You cannot run two versions of your application simultaneously from a single working tree. Worktrees allow you to run the production version in one terminal and the development version in another, side by side.
  • Clean History: Developers often make "WIP" commits just to switch branches. These clutter the history and require rebasing later. Worktrees eliminate this need entirely.
  • Dependency Management: Switching branches often triggers massive re-installs of node_modules or re-compilation of binaries. By maintaining separate worktrees for long lived branches, you keep your dependencies hot and ready.

Standard Workflow for Managing Worktrees

To start using this pattern, you need to familiarize yourself with the core commands. The process begins by adding a new path linked to a specific branch.

git worktree add ../bugfix-branch-name origin/main

This command creates a new folder outside your current directory. Once you are done with the task, you can remove it easily:

git worktree remove ../bugfix-branch-name

Managing these paths manually can become cumbersome if you have five or six active tasks. It requires a disciplined naming convention and a way to navigate between these directories quickly. Most developers find that the best way to manage Git worktrees involves using a terminal multiplexer or a specialized workspace tool to keep the various contexts organized.

Streamlining Context with Deska

When managing multiple worktrees, the cognitive load moves from Git commands to window management. This is where Deska provides a unique advantage. Rather than flipping between different IDE windows or terminal tabs that all look the same, you can map your worktrees to a visual layout.

Deska is a local-first desktop app that uses an infinite canvas. You can place different terminals and code editor panels anywhere on this space. For a worktree workflow, you might have one area of your canvas dedicated to your main feature and another area, perhaps zoomed out or off to the side, dedicated to hotfixes.

Because Deska is local-first, your sessions and files stay on your machine. You can open a terminal panel in your feature-a worktree and another in hotfix-b. The Ask Deska assistant can help you manage these sessions. You can use voice or chat to ask the assistant to open specific sessions or run commands across your workspace, which reduces the manual overhead of navigating directory structures.

Comparing Management Approaches

Different developers prefer different levels of abstraction when handling multiple working directories. The following table compares common methods for managing these environments.

MethodContext IsolationResource UsageSetup Complexity
Git StashLowLowSimple
Multiple ClonesHighHighModerate
Git WorktreesHighModerateModerate
Deska CanvasHighestModerateSimple

While multiple clones provide isolation, they waste disk space and make it harder to share local configurations. Git worktrees are more efficient but require a way to visualize the concurrent tasks. Using a canvas based workspace allows you to see the state of all worktrees at a glance, which is often the missing piece in a command line only workflow.

Advanced Worktree Automation with AI

Modern development involves more than just moving files. When you switch to a new worktree to fix a bug, you often need to re-index the code or get a summary of recent changes. Deska supports coding agents like Claude Code and OpenCode that can run side by side as panels.

You can direct an agent to a specific worktree panel to analyze a bug while you continue working on your main feature in another panel. This parallelization is only possible because worktrees provide separate file system entry points. By combining worktrees with agent threads, you can effectively multi-thread your own productivity, letting the AI handle boilerplate or diagnostic tasks in one directory while you focus on logic in another.

Best Practices for Long Term Worktree Usage

To keep your environment clean, follow these guidelines:

  1. Use a dedicated parent folder for all worktrees related to a project to avoid cluttering your root dev directory.
  2. Regularly run git worktree prune to clean up administrative data for worktrees you have manually deleted.
  3. Leverage notes within your workspace to document what each worktree is currently doing.
  4. If you need to step away from your desk, use the mobile app to monitor long running tests or builds within your worktree sessions via the secure relay.

Git Worktree FAQ

How to remove a git worktree?

The safest way to remove a worktree is using the command git worktree remove <path>. This ensures that Git cleans up the internal metadata located in the .git/worktrees folder. If you simply delete the folder, you will need to run git worktree prune to synchronize the repository state.

Can I have two worktrees on the same branch?

Git generally prevents you from having the same branch checked out in two different worktrees to avoid data corruption and HEAD conflicts. If you need to work on the same branch in two places, you must create a temporary branch or use a detached HEAD state in one of the worktrees.

Where are git worktrees stored?

A worktree can be stored anywhere on your local file system. However, the administrative data that links it to the main repository is stored inside the original repository hidden .git/worktrees directory. This is why you should treat the primary clone as the "source of truth" for the entire project.

Get Started with Better Workflows

Efficiently managing multiple streams of work is a hallmark of a senior developer. Moving beyond the limitations of stashing allows you to maintain focus and reduce the friction of switching tasks. By using Git worktrees in combination with a flexible, visual workspace, you can create a development environment that matches the way you actually think and work.

You can experiment with this workflow by using the infinite canvas and integrated terminals in Deska. The app is free for Mac, Windows, and Linux.

Download Deska