Tools & Workflow

Git Worktree for One Repository and Two Tasks

Use Git worktrees to keep a hotfix and ongoing feature open at the same time without cloning the repository twice.

2 min read
#git#worktree#branches#productivity

The Milky Way above a rugged desert landscape

Photo: Unsplash.

You are halfway through a feature when a production fix arrives. Stashing works, but it also hides context. A Git worktree gives another branch its own directory while sharing the repository’s object database.

From the main checkout:

git fetch origin
git worktree add ../project-hotfix -b hotfix/login origin/main

Now the directories are independent working trees:

project/          # your feature branch
project-hotfix/   # hotfix/login

Edits, indexes, and checked-out branches stay separate. Commits and fetched objects are shared. You can run tests in the hotfix directory without packing away the feature.

See active worktrees with:

git worktree list

After the hotfix is merged and the directory has no changes:

git worktree remove ../project-hotfix
git branch -d hotfix/login
git worktree prune

Git normally prevents the same branch from being checked out in two worktrees. That safeguard matters: two directories silently moving one branch would be confusing.

There are two practical cautions. First, tools may cache absolute paths, ports, or dependency state, so give simultaneous development servers distinct ports. Second, a worktree directory is not an ordinary disposable copy. Remove it with git worktree remove so Git can clean up its administrative records.

For one urgent interruption, this is less ceremony than another clone and clearer than a stack of unnamed stashes.

Reference