Sitemap

Git Worktree Explained

2 min readFeb 21, 2026

--

Most people hear about git worktree, try it once, and either love it or get confused.

Let’s simplify it properly.

What Problem Does Git Worktree Solve?

Normally with Git it is:

  • One repository
  • One working directory
  • One active branch

If you switch branches, your files change.

If you are building a feature and suddenly need to fix a production bug, you either stash or commit unfinished work before switching.

That context switching adds friction.

git worktree allows you to have multiple working directories for the same repository at the same time.

One repository. Multiple active branches. No switching.

That is the core idea.

The Mental Model

Think of it like this:

project/
project-feature-x/
project-hotfix/

All of these share the same Git history.

But each directory has a different branch checked out.

You can run separate servers.
You can open separate IDE windows.
You can work in parallel.

No stashing required.

Creating a Worktree

If the branch already exists:

git worktree add ../feature-x feature-x

This means:

  • Create a folder at ../feature-x
  • Checkout branch feature-x inside it

If the branch does not exist yet:

git worktree add -b feature-x ../feature-x

This creates the branch and the working directory in one step.

The folder name does not need to match the branch name. It is just a directory.

Where Does Git Store the Data?

Worktrees are lightweight.

Get Ravi’s stories in your inbox

Join Medium for free to get updates from this writer.

The new folder does not contain a full .git directory.

Inside the worktree you will see a .git file. That file points back to the original repository.

All commits, objects, and references are shared.

Additional metadata is stored inside:

original-repo/.git/worktrees/

So you are not cloning the repo. You are creating another checkout of the same repo.

What If the Worktree Is Inside the Same Repo?

Example:

git worktree add worktrees/feature-x feature-x

Now your structure looks like:

repo/
worktrees/
feature-x/

Git understands this is another working tree.

However, the worktrees/ folder may show up as untracked in git status.

To avoid noise, add it to .gitignore:

worktrees/

That keeps your main working directory clean.

Removing a Worktree

When you are done:

git worktree remove <path>

Do not manually delete the folder.

If you accidentally delete it manually, run:

git worktree prune

This cleans up stale metadata.

Important Rule

You cannot checkout the same branch in two worktrees at the same time.

Git will prevent it.

Each branch can only be active in one working directory.

When Is Git Worktree Useful?

It is most useful when:

  • You need to work on a hotfix without touching your feature branch
  • You want to experiment without affecting your current work
  • You run multiple services locally tied to different branches
  • You want clean mental separation between tasks

If you rarely switch branches, you probably do not need it.

--

--