Sub topic 1
The problem: one repository, one checked-out branch
A Git repository stores the whole history of a project, but a clone gives you only one place to work on it. That place holds one checked-out branch at a time. This chapter explains why that is a limit, what people usually do about it, and what a linked worktree changes. The rest of the tutorial covers the commands in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic Git use: committing, creating branches, switching with
git switchorgit checkout, and merging. - The difference between the working directory, the index (also called the staging area), and the repository history.
- How to use a command line, including relative paths such as
../project-copy. - What a remote and a remote-tracking branch are, and what
git fetchdoes.
You do not need to know anything about git worktree itself. You need Git 2.5 or later, released in 2015. Any current installation is newer than that.
One repository, one working directory
A normal clone has three parts that matter here:
- The repository data: commits, trees, file contents and refs (branches and tags). It lives in the
.gitdirectory. - The index, a file inside
.gitthat records what the next commit will contain. - The working directory: the ordinary files you edit, which sit next to
.git.
HEAD records which branch or commit the working directory and index correspond to. A clone has exactly one HEAD, one index and one working directory. So at any moment it shows exactly one version of your project.
When you run git switch other-branch, Git does not open a second view. It rewrites the files in your working directory to match other-branch. It also updates the index and HEAD. Files that differ between the two branches change on disk. Files that exist only on one branch appear or disappear.
This design is simple, and most of the time it is what you want. It becomes a problem when you need two versions of the project available at the same time.
When the limit hurts
Here are some situations where one checked-out branch is not enough:
- You are halfway through a feature, with uncommitted changes in a dozen files. A production bug comes in, and you must fix it on
mainright now. - A colleague asks you to review a branch. You want to run its tests while your own work stays untouched.
- A long test suite or build takes 40 minutes on one branch. You want to keep coding on another branch while it runs.
- You want to compare how two versions behave by running both side by side.
In each case, switching branches changes the files under your feet. Anything running from those files, such as a build, a test run or a dev server, now sees different code. Any uncommitted work blocks the switch or travels along with you.
Git may also refuse to switch at all:
git switch mainIf your uncommitted edits touch a file that differs between your current branch and main, Git stops with an error that starts with error: Your local changes to the following files would be overwritten by checkout. If the edits do not conflict, the switch succeeds and your edits come with you onto main. That is often not what you want either.
The usual workarounds
Most developers meet this problem early and settle on one of a few workarounds. Each one works, and each has a cost.
| Workaround | How it works | Cost |
|---|---|---|
| `git stash` | Saves uncommitted changes on a stack and restores a clean tree | Easy to forget, and it only protects tracked files unless you add `-u` or `-a`. Restoring can conflict if the code has moved on. |
| Temporary "WIP" commit | Commit unfinished work, switch, come back, then undo the commit with `git reset HEAD~1` | You must remember to undo it. Half-done commits can leak into pushes. |
| A second clone | Run `git clone` again into another directory | Duplicates the object database. The two clones know nothing about each other's local branches until you push and fetch between them. |
| Copying the directory | `cp -r` the project | Easy to get confused about which copy is which. The copy has its own `.git`, so it has the same problems as a second clone. |
Stashing and WIP commits do not remove the limit. They let you work around it by temporarily putting your state somewhere else. You still have one working directory, so you still cannot run two versions at once.
A second clone does give you two directories. But the two clones are separate repositories. A branch you create in one does not exist in the other until you push it to a remote and fetch it. Each clone also stores its own full copy of the history. For a large repository that can mean gigabytes of duplicated data and a slow initial clone.
What a linked worktree is
A worktree is a working directory attached to a repository. Your original clone already has one: the directory where .git lives. Git calls this the main worktree. The command git worktree add creates additional ones, called linked worktrees.
A linked worktree has its own copy of the project files, its own HEAD and its own index. It does not have its own repository. It uses the object database and the refs of the repository it was created from.
The following diagram shows the relationship.
Because the repository data is shared, three things follow:
- A commit made in any worktree is immediately visible to all the others. You do not need to push or fetch between them.
- A new branch created in one worktree exists in all of them, since branches are refs in the shared repository.
- Disk use stays small. Each linked worktree costs the size of one checkout of the project, not a full copy of the history.
The chapter on shared and per-worktree state covers exactly which files are shared and which are not. For now, remember this rule: history is shared, and the working directory, index and HEAD are per worktree.
Worktree versus second clone
The two approaches can both give you two directories on two branches. They differ in what sits behind those directories.
| Question | Second clone | Linked worktree |
|---|---|---|
| Object database | Full separate copy | Shared with the original |
| Disk cost | Whole history again | One checkout of the files |
| Branches created locally | Visible only in that clone | Visible in every worktree |
| Getting commits across | Push to a remote, then fetch | Already there |
| Fetching from the remote | Each clone fetches separately | One fetch updates all |
| Can two directories have the same branch checked out? | Yes, which risks diverging copies | No, Git refuses by default |
| Independence | Complete. Deleting one has no effect on the other. | Linked to the main repository. Deleting the main repository breaks the linked worktrees. |
The last row of each half matters. A second clone is fully independent, and that sometimes helps, for example when you want a throwaway copy that cannot affect the original. A linked worktree depends on the repository it came from. If you delete the original repository directory, the linked worktrees lose their history.
The "same branch" row is a deliberate safety rule, and the next chapter covers it. In short, two working directories on one branch could both commit to it and disagree about its tip. Git prevents that by default.
A first look
You have not yet seen the full set of options, but one command shows the idea. Suppose your repository is in myproject and you are working on a feature branch. A hotfix request arrives. From inside myproject:
git worktree add ../myproject-hotfix -b hotfix/login-crash mainThis creates a new directory ../myproject-hotfix next to your repository. It creates the branch hotfix/login-crash starting from main and checks it out there. Your original directory is not touched. Your uncommitted changes stay exactly where they were.
To see what exists, run:
git worktree listThe output has one line per worktree, with its path, the abbreviated commit it is on, and its branch in square brackets. It looks like this, with your own paths and hashes:
/home/you/myproject 3f2a9c1 [feature/search]
/home/you/myproject-hotfix 9b7e4d0 [hotfix/login-crash]You can now cd ../myproject-hotfix, fix the bug, run the tests and commit. Then return to myproject and carry on with your feature. No stash, no WIP commit and no second download of the history.
How Git links the two directories
A linked worktree does not contain a .git directory. It contains a .git file with a single line pointing back to the repository, for example:
gitdir: /home/you/myproject/.git/worktrees/myproject-hotfixThat target is a small administrative directory inside the main repository's .git. It holds the worktree's private HEAD and index. Git commands run in the linked directory read the .git file, find the administrative directory, and from there reach the shared repository data. A later chapter shows how to inspect this with git rev-parse --git-dir and git rev-parse --git-common-dir.
This also explains a common failure. If you move or delete a linked worktree with ordinary file commands, the repository still has an administrative entry for it. Git has commands to clean that up, and they are covered in the chapters on removing, locking and repairing worktrees.
What worktrees do not solve
Worktrees remove the need to switch branches in order to see two versions at once. They do not remove every cost, and it helps to know the main ones now:
- Each worktree is a separate directory of files. Files that are not tracked by Git, such as
node_modules, virtual environments, build output and local configuration, are not copied into a new worktree. You set them up again in each one. - A branch can be checked out in only one worktree at a time, unless you use a detached
HEADor a force option. - Tools that assume a single project directory, such as editors with a stored project path, need to be pointed at the new directory.
Later chapters cover each of these in turn.
Exercise
You are partway through a feature with uncommitted changes, and an urgent fix is needed on main. You decide to make a second clone instead of using a linked worktree. Give two concrete differences you will experience compared with a linked worktree.
Show answer
Any two of the following. The second clone stores a full separate copy of the history, so it uses more disk space and takes longer to create. A commit or branch made in the clone is not visible in your original repository until you push it and fetch it. The clone fetches from the remote separately, so you must update each one. The clone is fully independent, so deleting the original does not break it, whereas a linked worktree depends on the main repository.
Exercise
A linked worktree has its own working directory and index. Name two other things it has of its own and one thing it shares with the main worktree.
Show answer
It has its own HEAD (the branch or commit it is on) and its own administrative directory inside the main repository's .git. It shares the object database, meaning all commits and file contents, as well as the branches and tags.