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.
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.
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/feature2This 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/feature2Now 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-namingThe resulting directory structure keeps the worktree tidy.
git worktree list
From any worktree you can list all existing worktrees:
git worktree listSample 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-namingIf 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 pruneUsing git‑worktree instead of cloning multiple repositories keeps the disk layout clean, reduces duplication, and streamlines branch‑specific development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
