Git Worktrees Explained: Shared Repository State

Updated on
9 min read

Git worktrees let developers keep multiple branches checked out in separate directories without cloning the repository for each task. They are useful when a feature, release fix, code review, or experiment needs its own files and index while still using the same Git history. This guide explains what is shared, what stays independent, and how to create and remove linked worktrees safely.

Why Git Worktrees Are Being Discussed

Branch-based work normally happens in one directory: switching branches updates the files in that working tree. That is fine for sequential work, but it interrupts the current task when an urgent fix or review needs another branch. Stashing changes and switching back adds friction; creating another clone duplicates repository metadata and needs its own fetches and branch coordination.

Worktrees provide another option for local parallel work. A linked worktree is a separate directory attached to the same repository. It can have a different branch checked out, while Git shares the repository’s object database and most common metadata. The official Git project describes Git as a distributed version control system; worktrees add multiple local working directories without changing that underlying model.

What Is a Git Worktree?

A worktree is a checkout of a Git repository: a directory containing files at a particular commit, plus the metadata Git needs to track its state. Every non-bare repository has a main worktree, usually the directory created by git clone or git init. A repository can also have linked worktrees, each in a different path.

The Git worktree manual defines commands to add, list, lock, move, repair, prune, and remove linked worktrees. A linked worktree is not a second independent clone. It points back to the same repository’s common Git directory, so objects and most refs are available from every worktree.

For example, the main checkout can remain on main while another directory checks out a feature branch. Each directory has its own files and can be used by a different editor, terminal, or build process. Commits made on a branch update that branch’s shared ref, so the resulting history is part of the same repository.

The Problem Worktrees Solve

When only one working directory is available, Git cannot safely show two branches’ different file contents there at the same time. Switching branches rewrites tracked files and can be blocked by local changes that would be overwritten. Stashing can help, but it requires remembering which changes belong to which task and applying them again later.

Separate clones isolate files, but repeat the repository setup. Depending on repository size, cloning can consume time, disk space, and bandwidth. Developers must also fetch or push between clones to make commits visible to one another.

Worktrees trade full repository isolation for a lighter working-directory boundary. They are suited to local concurrency when tasks can share Git history and object storage. They do not remove the need for normal branch discipline: each task still needs a sensible branch or detached commit, and shared refs remain visible across the repository.

How Shared Repository State Works

Git separates a worktree’s local state from repository-wide state. The repository layout documentation describes the common directory used by multiple working trees and the per-worktree files maintained for each checkout.

Feature Linked worktree Separate clone
Working files Separate directory for each worktree Separate directory
HEAD and index Per-worktree, so each checkout can be on a different commit and stage different changes Independent
Git object database Shared with the other worktrees Normally stored separately
Local branches and most refs Shared; a local branch is normally checked out in only one worktree at a time Independent copies until fetch or push
Remotes and most repository config Shared Configured independently
Build output and dependencies Not shared automatically; these are ordinary files in each directory Not shared automatically

HEAD records the commit or branch checked out in a particular worktree. The index, also called the staging area, is similarly local, so staging a file in one worktree does not stage it in another. By contrast, the commit objects are stored in the shared object database, and branch names refer to commits in common.

Git prevents the same local branch from being checked out in multiple worktrees by default. This guard avoids two directories independently moving the same branch pointer while presenting different checked-out content. A branch checked out elsewhere can still be viewed by commit or used as a starting point for a different branch. For example, git worktree add -b feature/search ../feature-search main creates a new branch at the current main tip without checking out main again.

To inspect repository paths from a linked worktree, use git rev-parse --git-dir and git rev-parse --git-common-dir. The git rev-parse manual documents how Git reports these locations. The per-worktree Git directory points to administrative state for that checkout; the common directory is where shared repository data lives.

Key Concepts and Variants

  • Main worktree: The original checkout associated with a non-bare repository. It cannot be removed with git worktree remove.
  • Linked worktree: An additional checkout whose administrative metadata is linked to the common repository.
  • Branch-backed worktree: A checkout with a local branch. This is the usual choice for work that will produce commits.
  • Detached worktree: A checkout at a commit rather than a branch, useful for temporary testing or inspection. Create a branch first if the commits need a durable branch name.
  • Locked worktree: A worktree whose metadata Git should retain even if the directory is temporarily unavailable, such as a removable drive or an intermittently mounted network location.

Worktree-specific configuration is available for settings that should differ between checkouts, but it must be enabled deliberately with Git’s extensions.worktreeConfig. Without that extension, most repository configuration remains shared. Do not assume that changing a remote, hook, or other shared setting in one worktree affects only that directory.

Real-World Use Cases

Urgent fixes: Keep a feature branch open in the primary directory and create another worktree for a production fix. After committing the fix, return to the feature directory without stashing or switching its files away.

Pull request review: Check out a review branch in a separate directory to run its tests or inspect generated output while preserving current uncommitted work elsewhere.

Parallel development: Developers can use distinct worktrees for separate tasks in one local repository. Each task needs its own branch; worktrees do not make concurrent edits to the same branch safe.

Build comparisons: Build two revisions in separate directories to compare behavior. Build products and dependency folders are not magically shared; configure a build cache separately if reuse is appropriate.

Temporary experiments: A detached worktree can test an older commit without creating a named branch. If the experiment produces commits worth keeping, create a branch before removing the worktree.

Getting Started: Add, Verify, and Remove a Worktree

Run these commands from any directory inside the repository. The example creates a feature branch from main in a sibling directory:

# Create a new branch and its separate working directory
git worktree add -b feature/search ../feature-search main

# See the main and linked worktrees
git worktree list

# Confirm the branch and working state in the new directory
git -C ../feature-search status --short --branch

If the branch already exists and is not checked out elsewhere, add it without -b:

git worktree add ../release-review release/review

For disposable testing at a specific commit, use a detached worktree:

git worktree add --detach ../test-commit HEAD~3

Inspect the worktree list before cleanup. Remove a linked worktree with Git rather than deleting its directory manually:

git worktree list --porcelain
git worktree remove ../feature-search

Removal is designed for clean worktrees; Git refuses by default if tracked changes or untracked files would be lost. Review or save those files before removing the directory. Use a force option only when you have explicitly decided to discard the worktree’s data. If a directory was deleted outside Git and its administrative record is stale, preview cleanup first with git worktree prune -n, then run git worktree prune after confirming the paths.

For a worktree stored on a removable device, lock it while the device is offline and unlock it when it is available again:

git worktree lock ../portable-work --reason "Stored on removable drive"
git worktree unlock ../portable-work

Common Misconceptions

A worktree is a separate clone. It is a distinct checkout, but it shares repository objects and most refs with its linked worktrees. This saves duplicated Git data, while also meaning that operations involving common branches or configuration are not isolated.

All state is shared. The working files, HEAD, and index are per-worktree. Staging changes in one directory does not stage them in another, and one worktree can have a different branch checked out.

A worktree is a backup. Worktrees are different views of the same repository data, not independent copies. A damaged shared object store affects its linked worktrees too. Use a separate, verified backup for recovery.

Worktrees share dependencies and build results. Git only coordinates repository state. Build output, virtual environments, package directories, and editor settings are files on disk and need their own sharing or caching strategy.

Deleting the directory is the normal cleanup method. Use git worktree remove when possible so Git can clean up both the directory and administrative metadata. For a path removed manually, inspect stale metadata with git worktree prune -n before pruning it.

Changelog

  • 2026-11-30: Initial publication.
TBO Editorial

About the Author

TBO Editorial writes about the latest updates about products and services related to Technology, Business, Finance & Lifestyle. Do get in touch if you want to share any useful article with our community.