Sub topic 2
Creating Your First Linked Worktree
Prerequisites for this chapter
This chapter assumes you can commit, branch and switch in Git, and that you know the difference between the working directory, the index and the commit history. You should also be comfortable with relative paths on a command line. The commands need Git 2.17 or newer. git init -b needs 2.28 or newer.
The previous chapter described the problem: a normal repository has one working directory, so it has one checked-out branch at a time. Here you create a second working directory that is attached to the same repository, and you look at what Git built to make it work.
The command
The general form is:
git worktree add [options] <path> [<commit-ish>]<path> is where the new working directory goes. <commit-ish> is anything that names a commit, such as a branch, a tag, a remote-tracking branch or a commit hash. The new directory is called a linked worktree. The directory you were already using is the main worktree.
You can run the command from any worktree of the repository. Git resolves <path> relative to your current directory, not relative to the repository root. That is why the examples below use ../. They place the new directory beside the repository, not inside it.
Walkthrough: a hotfix on a new branch
The first block builds a small throwaway repository so that you can run everything yourself. If you already have a repository, skip to the git worktree add line and adjust the names.
mkdir demo && cd demo
git init -b main myproject
cd myproject
git config user.name "Demo User"
git config user.email "[email protected]"
echo "hello" > app.txt
git add app.txt
git commit -m "Initial commit"
git worktree add ../myproject-hotfix -b hotfix/login-crash mainThe last command does three things in one step:
- It creates the branch
hotfix/login-crashat the tip ofmain. - It creates the directory
../myproject-hotfix. - It checks the new branch out into that directory.
Git prints something like this:
Preparing worktree (new branch 'hotfix/login-crash')
HEAD is now at 3f2a9c1 Initial commitYour original directory is unchanged. It is still on main, and any uncommitted edits in it stay where they are. The new directory starts as a clean checkout of the starting point. Nothing from your current working directory or index is copied over, so you do not need to stash anything first.
Now look at the new directory:
cd ../myproject-hotfix
ls -A
git branch --show-current
git status --shortYou see app.txt and a .git entry. The branch is hotfix/login-crash, and the status output is empty because the checkout is clean.
What Git created on disk
In the new directory, .git is a file, not a directory. Print it:
cat .gitThe output is one line of this form:
gitdir: /home/you/demo/myproject/.git/worktrees/myproject-hotfixThat line points to a small private directory inside the main repository. Git creates one such directory for each linked worktree, named after the last component of the worktree path. If two worktrees would get the same name, Git appends a number to the second one.
ls ../myproject/.git/worktrees/myproject-hotfixThis directory holds the state that belongs to one worktree only, mainly its HEAD and its index. It also holds two pointer files. gitdir records the worktree's location, and commondir points back to the main .git directory. Objects, branches, tags, remotes and configuration are not copied. They live once, in the main .git directory.
You can confirm this from inside the linked worktree. Run these two commands there:
git rev-parse --git-dir
git rev-parse --git-common-dirThe first prints the private directory under .git/worktrees/. The second prints the shared .git directory. The next chapters use this split to explain what is shared and what is not. For now, remember that there is one repository and several working directories.
Working in the new worktree
Commit in the hotfix worktree as you would anywhere else:
echo "fixed" >> app.txt
git commit -am "Fix login crash"Switch back to the main worktree and look at the branch:
cd ../myproject
git log --oneline hotfix/login-crashThe commit is visible immediately, because both directories use the same object store and the same branch references. You did not push, fetch or copy anything. In a second clone you would have to push the commit and fetch it before the other copy saw it.
The two directories still have separate working files and separate indexes. Editing app.txt in one does not change it in the other.
The three ways to choose what gets checked out
The arguments after the path decide what the new worktree contains. The common forms are these:
| Command | Effect |
|---|---|
| `git worktree add ../wt-a -b new-branch main` | Creates `new-branch` at `main` and checks it out |
| `git worktree add ../wt-b feature/search` | Checks out the existing branch `feature/search` |
| `git worktree add --detach ../wt-c origin/feature/search` | Checks out that commit with a detached `HEAD` |
| `git worktree add ../wt-d` | Creates a branch named `wt-d` from the current `HEAD` |
Starting a new branch
-b <name> creates a new branch. The commit-ish after the path is its starting point. If you leave the starting point out, Git uses the HEAD of the worktree where you ran the command. Name the starting point explicitly, as the example does with main. This keeps the result independent of whatever you happened to have checked out.
If the branch already exists, -b fails with an error such as a branch named 'hotfix/login-crash' already exists. Use -B only if you intend to reset an existing branch to the starting point. That discards the branch's current position, so it is rarely what you want.
Checking out an existing branch
Without -b, a branch name as the commit-ish checks that branch out:
git worktree add ../myproject-feature feature/searchThis works only if feature/search is not already checked out in another worktree. Otherwise Git refuses, and the next chapter explains why.
Detached HEAD
A detached HEAD means the worktree points at a commit directly, not at a branch. That suits read-only review, because no branch can be moved by accident:
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchRun git fetch first so that origin/feature/search is current. A remote-tracking branch is a local record of where the remote branch was at your last fetch. Git does not update it when you add a worktree.
Leaving out the commit-ish
If you give no commit-ish and no -b, -B or --detach, Git uses the last part of the path as a branch name. For example, git worktree add ../experiment creates a branch called experiment from the current HEAD. If a branch with that name already exists, Git checks it out instead. If no local branch exists but exactly one remote has a branch of that name, recent Git versions can set up a local branch that tracks it. Because the result depends on the path and on which branches exist, prefer to state -b or the branch name explicitly while you are learning.
Rules for the path
- The path must not exist, or it must be an empty directory. Otherwise Git stops with a message like
'../myproject-hotfix' already exists. - It can be inside or outside the main working directory. Place it outside. A worktree inside the main one shows up as untracked content in
git statusunless you ignore it, and tools that scan the tree will find it. - Pick a name that shows the repository and the purpose, such as
myproject-hotfix. The directory name is what you see in your file manager and terminal prompt, and it is also used for the administrative entry. - Use paths that you can reach from a terminal. Git records the absolute location. If you move the directory by hand later, the link breaks, and a later chapter covers repairing it.
Common mistakes
- Using a path relative to the repository root. Git uses your current directory. If you run the command from a subdirectory,
../xlands somewhere you did not intend. Check the printed path, or rungit worktree listafterwards. - Expecting uncommitted work to follow you. The new worktree has only committed content. Your edits stay in the original directory.
- Running
cdinto the new directory and forgetting where you are. Terminal prompts that show the branch help. Without one, rungit branch --show-currentandpwdbefore you commit. - Trying to reuse a branch that is already checked out. Create a new branch from it with
-b, or use--detach. - Deleting the directory with
rm -rfinstead ofgit worktree remove. This leaves stale metadata behind. The cleanup chapter shows the safe way.
Review
You can now create a linked worktree with git worktree add. It takes a path and, optionally, a starting point. Git builds a new directory with its own working files, index and HEAD, linked to the one shared repository by a .git file. A new branch needs -b, an existing branch is named directly, and --detach gives a commit without a branch. Commits made in any worktree are visible in all of them straight away.
Exercise
You are in ~/src/app, on the branch main, with uncommitted edits. You run git worktree add ../app-fix -b fix/typo main. Where do your uncommitted edits appear, and where is the .git data for the new worktree stored?
Show answer
The uncommitted edits stay only in ~/src/app. The new worktree ../app-fix is a clean checkout of main. Its .git is a file that points to a private directory under ~/src/app/.git/worktrees/app-fix. That directory holds its HEAD and index. Objects, branches and config stay in the shared ~/src/app/.git.
Exercise
You want to review the remote branch origin/feature/search without any risk of committing to a branch by accident. Write the commands, and say why --detach is used.
Show answer
Run git fetch origin and then git worktree add --detach ../app-review origin/feature/search. The fetch brings the remote-tracking branch up to date. --detach checks out the commit itself, so no local branch is attached to the worktree and no branch can be moved by committing there.
Exercise
You run git worktree add ../experiment with no other arguments from a worktree whose HEAD is on main, and no branch named experiment exists. What does Git do?
Show answer
Git creates a new branch named experiment (the last part of the path) starting at the current HEAD, and checks it out in ../experiment. This is the same as running with -b experiment.