Git Worktrees Explained: Work on Two Branches at Once Without Switching

Git Worktrees Explained: Work on Two Branches at Once Without Switching

Git worktrees explained in one sentence: a worktree gives you a second working directory for the same repository, checked out to a different branch, so you can work on two branches at once without switching. You have probably felt the pain that worktrees solve. You are deep in a feature branch with uncommitted changes when a teammate asks you to review their pull request, or a bug report lands on the main branch and needs a quick fix. The usual options are all bad: stash your work, commit a half-finished mess, or clone the repo again and wait for the download. A worktree gives you a cleaner way out, and it is built into Git already.

Git Worktrees Explained: The Core Idea

Every Git repository you clone has one working tree by default. That is the folder where your files live and where you run your commands. A worktree is an extra working tree attached to the same repository. It shares the same .git data, the same objects, and the same branches, but it has its own checked-out files and its own HEAD. Change a file in one worktree and the other worktree does not see it, because each worktree tracks its own branch.

Think of it like two desks in the same office. Both desks belong to the same project, but the papers on one desk are a different branch from the papers on the other. You can walk between them without packing anything away. That is the whole idea, and once you use it, going back to constant stashing feels like a chore you no longer need.

If you are still deciding between merge and rebase for the branches you juggle, our guide on git rebase vs merge and what each does to your history pairs well with this one, because worktrees make experimenting with either strategy cheap.

When Two Branches at Once Saves Your Day

The most common use case is the interrupted feature. You are halfway through a feature branch, the code does not compile yet, and a production bug needs a fix on main. With a worktree, you open a second folder, fix the bug there, push, and go back to your feature. Your half-finished code never moves.

Code review is the second big win. Reviewing a pull request in your own editor beats reading a diff on a web page, especially for a large change. A worktree lets you check out the review branch, run the tests, and poke around, while your own branch stays exactly as you left it. Our checklist on how to do a useful code review covers what to look for once the code is in front of you.

Long-running experiments are a third case. Say you want to try a risky refactor on a branch while keeping the stable version around for reference. A worktree keeps both versions on disk at the same time, so you can diff files side by side or run the old version to compare behavior. The same trick helps when you maintain two supported versions of a project and need to backport a fix to the older one.

Create Your First Worktree with git worktree add

The git worktree add command creates a new worktree in one step. From inside your repository, run:

git worktree add ../my-project-hotfix main

This creates a folder called my-project-hotfix next to your current folder, checked out to the main branch. If you want a new branch instead of an existing one, add the -b flag:

git worktree add -b feature/new-api ../my-project-api feature/new-api

The first argument after the path is the branch, and the path comes first. That order trips people up, so remember: path, then branch. You can also create a worktree from a remote branch or a specific commit, which is handy when you need to inspect an old release without disturbing your current work.

To see every worktree attached to your repository, run:

git worktree list

The output shows each worktree path, its commit, and its branch. When you are done with one, remove it with git worktree remove, covered in detail below. If you have ever tangled your history while switching branches in a hurry, our guide on how to fix broken Git history with rebase, reset, and reflog is a useful safety net to know about.

Review a Pull Request Without Touching Your Branch

Here is a full workflow for a review pull request separate worktree setup. A teammate opens a pull request from a branch called feature/checkout-redesign. You fetch the branch, then create a worktree for it:

git fetch origin
git worktree add ../my-project-review feature/checkout-redesign

Now open ../my-project-review in your editor. Run the test suite there, click through the app, and leave comments. Meanwhile, your main worktree still has your feature branch checked out with your uncommitted changes intact. Nothing was stashed, nothing was committed early, and nothing was lost.

When the review is done, delete the worktree and get on with your day. If the author pushes new commits to the branch, you can pull them inside the review worktree just like any other checkout. This workflow shines in teams where reviews are frequent, because the cost of starting a review drops to one command. It also works well with a visual client; if you prefer a GUI, see our roundup of the best Git GUI clients in 2026 compared.

Keep Worktrees Tidy: List, Remove, and Prune

Worktrees are cheap, but they still need housekeeping. The git worktree remove cleanup routine has three commands worth memorizing. First, list what you have:

git worktree list

Second, remove a worktree you no longer need:

git worktree remove ../my-project-review

Git refuses to remove a worktree that has uncommitted changes, which protects you from losing work by accident. If you are sure, add the --force flag. Third, if you deleted a worktree folder by hand instead of using the remove command, Git still remembers it. Clean up those stale entries with:

git worktree prune

A good habit is to keep worktrees in a predictable place, such as a folder next to your main checkout, and to name them after the branch they hold. When a branch is merged and deleted, remove its worktree in the same breath. A quick git worktree list at the end of the week tells you if any forgotten worktrees are piling up.

Worktree Limits and Gotchas Worth Knowing

Worktrees have a few rules that surprise first-time users. The most important one: a branch can only be checked out in one worktree at a time. If you try to check out the same branch in a second worktree, Git stops you with an error. This is a safety feature, not a bug, since two checkouts of one branch would fight over the same ref.

Second, submodules and untracked files are per worktree. An untracked file you create in one worktree does not appear in the other, and each worktree can have its own build output. That is usually what you want, but it also means a fresh worktree needs its own dependency install and build step, which can take a few minutes on a large project.

Third, tools that assume one working directory can get confused. Some IDEs, linters, and Git hooks read the repository path in ways that expect a single checkout. Most modern tools handle worktrees fine, but if a hook behaves oddly, check whether it hardcodes the main worktree path. Finally, remember that worktrees share the repository, so a git gc or a fetch in one worktree affects the shared object store for all of them. That is normally a benefit, since fetching in one worktree makes the commits available everywhere.

Good commit habits matter even more when you juggle branches across worktrees, so it is worth reading our guide to conventional commits and Git messages your team will thank you for. Clear messages make it obvious which worktree holds which piece of work.

Start small: create one worktree for your next code review and see how it feels. Most developers who try worktrees once keep using them, because the alternative is going back to stashing, cloning, or committing work that is not ready. Git already has the feature. All it costs is one command.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.