Operations 10 min read

Master Git Worktree: Efficient Multi‑Branch Development Without Clutter

Git worktree, a little‑known feature introduced in 2015, lets developers maintain a single repository while simultaneously working on multiple branches—such as feature and hotfix—without costly context switches, large repo clones, or environment reconfiguration, and includes commands for adding, listing, removing, and pruning worktrees.

Programmer DD
Programmer DD
Programmer DD
Master Git Worktree: Efficient Multi‑Branch Development Without Clutter

Developers often have to juggle feature development, production hotfixes, testing, and maintenance within the same project, which can lead to frequent branch switching and environment reconfiguration.

Commit the unfinished feature hastily and then switch to the hotfix branch.

Use git stash | git stash pop to stash changes before switching.

The second method is better, but it still falls short in several scenarios:

Long‑running tests on the main branch are interrupted when switching.

Large projects make frequent index switching costly.

Older releases have different settings, making IDE re‑configuration expensive.

Switching branches requires resetting environment variables (dev/qa/prod).

Sometimes you need to check a colleague’s code to reproduce bugs.

Cloning multiple repositories solves some problems but introduces new ones: synchronization difficulties, duplicated history, and management overhead.

git‑worktree

Git has supported git‑worktree since 2015, allowing a single repository to have multiple working trees that operate independently. git worktree --help The help output (illustrated below) shows the available sub‑commands.

git worktree help output
git worktree help output

In simple terms, git‑worktree lets you maintain one repo while working on several branches without interference.

Only one repo is needed, yet you can work on multiple branches simultaneously.

The four most commonly used commands are:

git worktree add [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<commit-ish>]
git worktree list [--porcelain]
git worktree remove [-f] <worktree>
git worktree prune [-n] [-v] [--expire <expire>]

git worktree add

Assume the repository root is amend-crash-demo. Adding a worktree for a new feature:

cd amend-crash-demo
git worktree add ../feature/feature2

This creates a new directory feature/feature2 containing a separate .git file that points back to the main repo’s worktree data.

gitdir: /Users/rgyb/Documents/projects/amend-crash-demo/.git/worktrees/feature2

Now you can commit, pull, or push in feature2 without affecting the main worktree.

Handling branch names with slashes

When branch names contain “/”, Git treats them as directories. Use the -b option to create the branch while adding the worktree:

git worktree add -b "hotfix/JIRA234-fix-naming" ../hotfix/JIRA234-fix-naming

The resulting directory structure keeps the worktree tidy.

git worktree list

From any worktree you can list all existing worktrees:

git worktree list

Sample output:

/Users/rgyb/Documents/projects/amend-crash-demo                82b8711 [main]
/Users/rgyb/Documents/projects/chore/chore                    8782898 (detached HEAD)
/Users/rgyb/Documents/projects/feature/feature2               82b8711 [feature2]
/Users/rgyb/Documents/projects/hotfix/hotfix/JIRA234-fix-naming 82b8711 [JIRA234-fix-naming]
/Users/rgyb/Documents/projects/hotfix/JIRA234-fix-naming      82b8711 [hotfix/JIRA234-fix-naming]

git worktree remove

To delete a worktree, simply specify its path:

git worktree remove hotfix/hotfix/JIRA234-fix-naming

If the worktree has uncommitted changes, add the -f flag to force removal.

git worktree prune

After removing worktrees, run git worktree prune to clean up any leftover administrative files.

Summary

The complete workflow revolves around four commands:

git worktree add
git worktree list
git worktree remove
git worktree prune

Using git‑worktree instead of cloning multiple repositories keeps the disk layout clean, reduces duplication, and streamlines branch‑specific development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsVersion ControlBranch ManagementWorktree
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.