Sub topic 4
Seeing what worktrees exist
This chapter assumes you can already create a linked worktree with git worktree add. It covers how to see which worktrees a repository has and what state each one is in. It also shows how to read the answer from inside a worktree and how to feed it to scripts.
Why listing matters
With one working directory, git status tells you where you are. With several worktrees, you also need to know:
- which directories exist
- which branch or commit each one has checked out
- which ones are locked or have lost their directory
git worktree list answers these questions. It reads the repository's own records, so it gives the same answer from any worktree in the repository.
The default output
git worktree listExample output:
/home/dev/myproject 3f9a1c2 [main]
/home/dev/myproject-hotfix 8d41b7e [hotfix/login-crash]
/home/dev/myproject-review 5c0e9aa (detached HEAD)Each line is one worktree. It has three columns:
| Column | Meaning |
|---|---|
| Path | Absolute path of the worktree directory. |
| Commit | Abbreviated hash of the commit that `HEAD` points to. |
| Branch | The checked-out branch in brackets, or a marker such as `(detached HEAD)` or `(bare)`. |
Three things are worth knowing about this output:
- The first line is always the main worktree. This is the directory that holds the real
.gitdirectory. In a bare repository setup, that first line is the bare repository and shows(bare). - The list is the same from every worktree. Running it in
myproject-hotfixprints the same three lines. - The paths are absolute, so you can copy them into
cdorgit -C.
A worktree with a branch shows the short branch name. A worktree on a detached HEAD shows only the commit, because no branch is involved. Detached worktrees are common for review, as covered in the previous chapter on starting points.
Verbose output
The plain listing hides two kinds of status: locked worktrees and prunable ones. Add -v to see them:
git worktree list -v/home/dev/myproject 3f9a1c2 [main]
/home/dev/myproject-hotfix 8d41b7e [hotfix/login-crash]
/home/dev/myproject-review 5c0e9aa (detached HEAD)
locked: on removable drive
/home/dev/work/old-task a1b2c3d [old-task]
prunable: gitdir file points to non-existent location- Locked means someone ran
git worktree lock. Git will not prune, move or remove it without extra force. The reason, if one was given, appears on an indented line. A lock without a reason shows only the wordlockedafter the branch. - Prunable means the worktree's directory is gone, for example because you deleted it with
rm -rfor moved it by hand. Git still has an administrative entry for it.git worktree pruneremoves such entries, and the next chapter covers it.
If you use -v as a habit, you will notice stale entries before they cause the "already checked out" error for a branch you thought was free.
Machine-readable output with --porcelain
The default format is meant for people. Column widths change with path length, and the wording may change between Git versions. Scripts should use the porcelain format, which is stable:
git worktree list --porcelainworktree /home/dev/myproject
HEAD 3f9a1c2e5b7d4a60918c2f3e7b5d1a8c4e6f0b92
branch refs/heads/main
worktree /home/dev/myproject-hotfix
HEAD 8d41b7e0c3a95f12d6e7b08a4c19f35e2a7d6c41
branch refs/heads/hotfix/login-crash
worktree /home/dev/myproject-review
HEAD 5c0e9aa17b2d4f6830e1a9c7d5b3f2480e6a1c9d
detached
locked on removable driveThe rules of the format:
- Each worktree is a block of lines. A blank line ends the block.
- Every line is a label, then a space, then a value. Some labels have no value.
worktreeis the path and is always present.HEADis the full 40-character hash.branchholds the full ref name, such asrefs/heads/main. It does not appear for a detached worktree.detachedandbareare labels without values.lockedandprunableappear only when they apply. Each may be followed by a reason.
The porcelain format shows the lock and prunable labels even without -v.
Porcelain output contains the full ref name and the full hash. Strip refs/heads/ yourself if you want a short name.
A path containing a newline character would break the line-based format. For that rare case, add -z, which ends each line with a NUL character and ends each block with an extra NUL. This option exists only together with --porcelain and needs a reasonably recent Git version (2.36 or later).
Using the output in a script
This command prints each worktree path with its branch:
git worktree list --porcelain | awk '
/^worktree / { path = substr($0, 10) }
/^branch / { sub("refs/heads/", "", $2); print path " -> " $2 }
/^detached/ { print path " -> (detached)" }
'substr($0, 10) takes everything after the 9-character prefix worktree , so paths with spaces stay intact. A bare repository entry prints nothing, because it has neither a branch nor a detached line.
To find which worktree holds a particular branch:
git worktree list --porcelain | grep -B2 'branch refs/heads/feature/search'The worktree line comes two lines before the branch line in each block, so -B2 prints the path along with the match. If nothing prints, the branch is free to check out.
To run a command in every worktree, loop over the paths:
git worktree list --porcelain \
| awk '/^worktree / { print substr($0, 10) }' \
| while IFS= read -r wt; do
echo "== $wt"
git -C "$wt" status --short --branch
donegit -C <path> runs Git as if started in that directory. This is the easiest way to survey the dirty state of every worktree, which git worktree list does not show. A bare repository entry has no working directory, so git status fails for it. In a bare layout, filter that line out first.
Inspecting from inside a worktree
The list tells you what exists. Two git rev-parse queries tell you how the current directory fits in:
git rev-parse --git-dir
git rev-parse --git-common-dirRun in the main worktree, both print .git (a relative path). Run in a linked worktree, they differ:
/home/dev/myproject/.git/worktrees/myproject-hotfix
/home/dev/myproject/.git--git-diris the private administrative directory for this worktree. It holds the things that belong to one checkout: itsHEAD, its index, and any lock marker.--git-common-diris the shared repository directory. It holds objects, branches, tags, remotes and configuration.
If you want absolute paths regardless of where you are, add --path-format=absolute, available from Git 2.31:
git rev-parse --path-format=absolute --git-dir --git-common-dirIf the two paths are equal, you are in the main worktree. This is a reliable test in scripts.
How Git finds the repository
A linked worktree does not contain a .git directory. It contains a .git file with one line:
gitdir: /home/dev/myproject/.git/worktrees/myproject-hotfixThat line points to the worktree's private directory. Inside that directory, a file named gitdir points back to the worktree's .git file, and a file named commondir points to the shared repository. The list command walks the worktrees/ directory in the main repository and reads each gitdir file to get the paths.
This two-way link explains the "prunable" state. If you delete myproject-hotfix/ by hand, the administrative folder still exists, and its gitdir file now points to a path that is missing. git worktree list notices this and marks the entry prunable.
The folder name under worktrees/ comes from the last component of the worktree path. If two worktrees would get the same name, Git adds a number, for example myproject-hotfix1. Do not rely on that name. Use git worktree list for paths.
Seeing worktrees from the branch side
Branch commands also show where branches are checked out. In a recent Git version, git branch marks a branch that is checked out in another worktree with a +, and the current branch with *:
git branch* main
+ hotfix/login-crash
feature/searchHere feature/search has no marker, so it can be checked out anywhere. hotfix/login-crash carries +, so git switch hotfix/login-crash in this worktree will fail. This is the quickest way to anticipate the one-branch-per-worktree error before you meet it.
For scripts, git for-each-ref can print the path through the %(worktreepath) field name, which is empty for branches not checked out anywhere:
git for-each-ref --format='%(refname:short) %(worktreepath)' refs/headsCommon errors and surprises
- A worktree you deleted still appears. You removed the directory with
rm -rf. The list shows it as prunable with-v. Rungit worktree prune, or usegit worktree removenext time. - A worktree on removable storage disappears from use but stays listed. If the drive is not mounted, Git sees a missing directory and may mark it prunable. Locking the worktree prevents pruning. A later chapter covers
lock. - The list looks different in a script after a Git upgrade. You parsed the default format. Switch to
--porcelain. git worktree listshows a branch butgit statusin that directory says something else. The list reads the worktree'sHEADat the moment you run it. If you switched branches inside that worktree a minute ago, the list reflects that, so recheck withgit -C <path> status.- Your script breaks on the first line. The first block is the main worktree, or the bare repository. Do not assume every block has a
branchline, because detached and bare entries do not. - You expected the list to show uncommitted changes. It does not. Use the
git -Cloop above.
Exercises
Exercise
You run git worktree list --porcelain and one block has the lines worktree /srv/build, HEAD 5c0e9aa17b2d4f6830e1a9c7d5b3f2480e6a1c9d and detached, with no branch line. What does that tell you, and what does a script that expects a branch line in every block need to handle?
Show answer
The worktree at /srv/build has a detached HEAD, so it has no branch checked out and is not holding any branch. A script must also handle detached (and bare) blocks. Otherwise it will skip or mislabel these worktrees.
Exercise
In a linked worktree, git rev-parse --git-dir prints /home/dev/myproject/.git/worktrees/myproject-hotfix and git rev-parse --git-common-dir prints /home/dev/myproject/.git. Which of the two paths holds the objects and branches, and which holds this worktree's own index?
Show answer
The common directory, /home/dev/myproject/.git, holds the shared objects, branches, tags and configuration. The git-dir, /home/dev/myproject/.git/worktrees/myproject-hotfix, holds this worktree's own HEAD and index.
Exercise
A teammate deleted the directory ../myproject-review with rm -rf. Which command shows that Git still tracks it and gives the reason, and which command clears the entry?
Show answer
git worktree list -v shows the worktree marked prunable with the reason "gitdir file points to non-existent location". git worktree prune removes the stale administrative entry.