Master

Many checkouts, one repo

I used to clone a repo five times to work on five branches at once and babysit a pile of duplicate folders. `git worktree` collapsed all of that into one place and made the duplicates go away.

What you will see

C:/Users/You/wt-main abc4567 [main]

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Open several folders that all share one repository, each on its own branch. I stopped cloning a repo five times and started using worktrees to juggle branches side by side.

1

Create a base repo with one commit. This is the single repository everything else will point back to.

mkdir wt-main cd wt-main git init -b main git config user.name Test git config user.email test@test.com printf 'v1\n' > app.py git add app.py git commit -m initial

Before you run it, say out loud what it will print. Then open the answer.

What it prints
HEAD is now at abc4567 initial
The `-b main` flag names the first branch. All the worktrees you add below share this one .git folder, nothing is copied, just linked.
2

Add a second working folder on a brand-new branch in one move. -b creates the branch and checks it out in the new location.

git worktree add ../release-2.0 -b release-2.0 git worktree list

Before you run it, say out loud what it will print. Then open the answer.

What it prints
HEAD is now at abc4567 initial C:/Users/You/wt-main abc4567 [main] C:/Users/You/release-2.0 abc4567 [release-2.0]
The two lines under the hash are the worktree list: the original on `main` and the new one on `release-2.0`. They share all history but each has its own files.
3

Remove a worktree you no longer need. Its branch still exists in the repository; only the extra folder goes away.

git worktree remove ../release-2.0 git worktree list

Before you run it, say out loud what it will print. Then open the answer.

What it prints
C:/Users/You/wt-main abc4567 [main]
You cannot remove a worktree that still has a branch checked out in an open terminal. Close that terminal, or use `remove --force` with care.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Worktrees add working folders that share one object store, letting several branches live at once.
1 Create a worktree pointing at the repo
↓
2 Switch it to a branch
↓
3 Commit there in its own folder
↓
4 Other worktrees stay on their branches
↓
5 All share one .git object store
Why this works
Why worktrees matter

Normally a folder can hold only one branch at a time, so juggling several meant either stashing constantly or cloning several copies that each took up full disk space. Worktrees solve both problems: many folders, one object store, and switching between branches takes a second. The closest git comes to multitasking.multitasking.

The one thing to watch

A worktree is only as clean as the folder that holds it. Delete the folder yourself or git will still list it, which is why git worktree prune and the remove command exist. Keep the worktree and the folder in sync and the feature is almost frictionless.

Examples

A second checkout for free

git worktree add ../fix-hotfix

This creates a new working directory that shares the same .git folder as your main checkout. You can switch it to a branch and commit there without a single file being copied. The magic is that all the worktrees point at one repository object store, so nothing is duplicated on disk.

Work a branch while it is checked out

git worktree add ../release-2.0 -b release-2.0

Add the -b flag to create a brand-new branch in the new worktree in one move. I use this to keep a release branch open side by side with active development on my main tree, each in its own folder, each with its own editor and terminal.

Clean up the stray worktrees

git worktree list
git worktree remove ../fix-hotfix

list shows every worktree and whether it is still in use. If a folder gets deleted out from under git, remove --force clears the stale entry. Leaving one behind is the only real footgun here, and it only ever means a folder git no longer knows how to clean up.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Framing the moment you are proud of
Previous Lesson
Framing the moment you are proud of
Next Lesson
Git that checks its own work
Git that checks its own work →