git/list[1] front-page[2] threads[3] people[4] search[5] about
 

GIT_WORK_TREE is not exported for post-checkout-hook

From
PSPhil Sainty <psainty@orcon.net.nz>
Date
Sep 15, 2026, 08:24 UTC
Message-ID
<17f331aef833b34fbddbefd66c330465@orcon.net.nz>

I have a post-checkout-hook shell script which indirectly calls "git rev-parse --show-toplevel", and I've found that when that hook runs for a checkout within a worktree directory, GIT_DIR is set but GIT_WORK_TREE is not set, which leads to the CWD being returned instead of the genuine toplevel value.

The script is seeing a GIT_DIR value like this:
     GIT_DIR=/path/to/repo/.git/worktrees/name

The only other git variables in the environment are GIT_EXEC_PATH and GIT_PREFIX.

The outcome I'm seeing is explained by https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#_repository_locations which says:

     "If --git-dir or GIT_DIR is specified but none of --work-tree,
     GIT_WORK_TREE or core.worktree is specified, the current working
     directory is regarded as the top level of your working tree."
The current githooks(5) man page talks about this somewhat:
     "Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are
     exported so that Git commands run by the hook can correctly locate
     the repository.  If your hook needs to invoke Git commands in a
     foreign repository or in a different working tree of the same
     repository, then it should clear these environment variables so
     they do not interfere with Git operations at the foreign
     location."

Unsetting GIT_DIR certainly fixes the problem, but I don't think that ought to be necessary in my case, as my hook script is calling git in the *same* working copy where the checkout happens. It is not dealing with "a foreign" repository [or] a different working tree".

My impression is that GIT_WORK_TREE should also be set for this hook when dealing with a worktree. I have tested this in git version 2.34 and 2.55 (maint) and also 2.56.0.rc0.355.g945ea7012d (next).

I have only tested post-checkout-hook, so I do not know if this applies to other hooks as well.

[System Info] git version: git version 2.34.1 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh uname: Linux 6.8.0-138-generic #138~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 7 13:43:15 UTC x86_64 compiler info: gnuc: 11.4 libc info: glibc: 2.35 $SHELL (typically, interactive shell): /bin/bash

Next: Junio C Hamano
Message 1 of 3 in “GIT_WORK_TREE is not exported for post-checkout-hook”
  1. Phil SaintySep 15, 2026
  2. Junio C HamanoSep 16, 2026
  3. Phil SaintySep 25, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.