Sub topic 4
Seeing what worktrees exist
This chapter assumes you can commit, branch and fetch, and that you know the difference between the working directory, the index and the repository history. You should also have created at least one linked worktree with git worktree add.
Once you have a few worktrees, you need a quick way to answer three questions. Where are they? What is checked out in each? Which one am I standing in?
Listing worktrees
Run this from any worktree, including the main one:
git worktree listSample output:
/home/sam/myproject 3f9a1c2 [main]
/home/sam/myproject-hotfix 8b7d0e4 [hotfix/login-crash]
/home/sam/myproject-review c41e9aa (detached HEAD)Each line has three parts:
- The absolute path of the worktree.
- The abbreviated hash of the commit that worktree's
HEADpoints to. - The branch name in square brackets, or
(detached HEAD)when no branch is checked out.
The first line is always the main worktree, the one that holds the .git directory. The other lines are linked worktrees. The output is the same no matter which worktree you run it from, because every worktree reads the same list.
Git can also add notes to a line. A worktree marked locked is protected from pruning. A worktree marked prunable has a directory that no longer exists. A repository cloned with --bare shows (bare) for its central directory.
Script-friendly output
The default format is meant for people, and column spacing can change. For scripts, use the porcelain format:
git worktree list --porcelainworktree /home/sam/myproject
HEAD 3f9a1c2d5e6b7a8091b2c3d4e5f60718293a4b5c
branch refs/heads/main
worktree /home/sam/myproject-hotfix
HEAD 8b7d0e4f1a2b3c4d5e6f708192a3b4c5d6e7f809
branch refs/heads/hotfix/login-crash
worktree /home/sam/myproject-review
HEAD c41e9aa0b1c2d3e4f5061728394a5b6c7d8e9f01
detachedEach worktree is one block, and blocks are separated by a blank line. Each line is a label followed by a value. The porcelain format gives the full hash and the full ref name (refs/heads/...), and it marks detached worktrees with a bare detached line. Git promises to keep this format stable across versions.
A small script can use it. This one prints the path of every worktree that has a given branch checked out:
git worktree list --porcelain | awk -v ref="refs/heads/main" '
$1 == "worktree" { path = $2 }
$1 == "branch" && $2 == ref { print path }
'Paths that contain spaces are printed as-is, so $2 only captures the first word. If your paths may contain spaces, use git worktree list --porcelain -z, which separates fields with NUL characters instead of newlines.
Finding out where you are
git worktree list does not mark the current worktree. To find out which one you are in, ask Git for the top-level directory:
git rev-parse --show-toplevelTo tell a linked worktree from the main one, compare two other values:
git rev-parse --git-dir
git rev-parse --git-common-dirIn the main worktree both commands print the same directory, usually .git. In a linked worktree, --git-dir prints a private directory such as /home/sam/myproject/.git/worktrees/myproject-hotfix, while --git-common-dir prints the shared /home/sam/myproject/.git. If the two differ, you are in a linked worktree.
Reading status inside a worktree
Plain git status already tells you what you need for the current worktree. Its first line shows the branch, or HEAD detached at <hash>. The list of worktrees shows only where each one is and what it points to. It does not show whether a worktree has uncommitted changes. To check that, run git status inside it, or use -C to run it without changing directory:
git -C ../myproject-hotfix status --shortCommon mistakes
- Reading the first line as "the current worktree". The first line is always the main worktree. It has no relation to where you ran the command.
- Parsing the default output in a script. Use
--porcelain. - Assuming a listed worktree still exists. If someone deleted a directory by hand, Git still lists it, and marks it
prunable. Cleaning that up is covered in the chapter on removing worktrees.
Exercise
You run git worktree list --porcelain and see a block with a worktree line and a HEAD line, but no branch line. It has a line that says detached. What does this tell you, and how could you confirm it from inside that worktree?
Show answer
That worktree has a detached HEAD, so it points at a commit rather than a branch. You can confirm it by running git status there, which shows "HEAD detached at <hash>", or by running git branch --show-current, which prints nothing.
Exercise
Inside an unknown directory, git rev-parse --git-dir prints /home/sam/myproject/.git/worktrees/review and git rev-parse --git-common-dir prints /home/sam/myproject/.git. Is this the main worktree or a linked one, and why?
Show answer
It is a linked worktree. The private directory (--git-dir) differs from the shared directory (--git-common-dir). In the main worktree both would print the same path.