Volume XXII, number 280Wednesday, October 7, 2026Latest message 52 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

[BUG] broken behaviour when running cd in a hook in a secondary worktree

1 messages between May 28, 2026 and May 28, 2026, from Baptiste Jean-Louis.

Plain Markdown or JSON for tools and agents.

Baptiste Jean-LouisMay 28, 2026, 21:03 UTC on lore

Hello, Thank you for your great work here is a bug I encountered

What did you do before the bug happened? (Steps to reproduce your issue) #!/bin/bash

mkdir topfolder cd topfolder mkdir main-worktree cd main-worktree git init mkdir dir touch file-a dir/file-b git add . git commit -m "Initial commit":

cat >.git/hooks/cd-bug << EOF #!/bin/bash

echo -e "\n\nrunning cd-bug hook" # Redirect output to stderr. exec 1>&2

echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" git describe echo pwd : $(pwd) echo pwd_var : $PWD echo git_prefix: $GIT_PREFIX echo git_dir: $GIT_DIR echo git_work_tree: $GIT_WORK_TREE git status echo "========================================================" cd dir echo pwd : $(pwd) echo pwd_var : $PWD echo git_prefix: $GIT_PREFIX echo git_dir: $GIT_DIR echo git_work_tree: $GIT_WORK_TREE git status echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" echo -e "cd-bug hook done\n\n"

EOF chmod u+x .git/hooks/cd-bug

# behave as expected git hook run cd-bug

git branch branch_b git worktree add ../second-worktree branch_b cd ../second-worktree

# broken behaviour here git hook run cd-bug

# end of script

What did you expect to happen? (Expected behavior) On branch branch_b nothing to commit, working tree clean

What happened instead? (Actual behavior)
On branch branch_b
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    dir/file-b
        deleted:    file-a
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-b
What's different between what you expected and what actually happened?
-> Running `git status` after `cd` in a pre-commit hook lists all
repo's tracked file as deleted.
   However when running the hook from the main-worktree, I have no issue.
   In my final use case, I'm doing something that goes like below
   cd subfolder
   `git stash --keep-index`
   `./update-generated-files` which updates some files in subfolder
and subfolder/*/
   `git stash pop`
[System Info]
git version:
git version 2.51.1.windows.1
cpu: x86_64
built from commit: 1454f0a9c4a3a22fb3fd7cc33f76f88cd65ced41
sizeof-long: 4
sizeof-size_t: 8
shell-path: D:/git-sdk-64-build-installers/usr/bin/sh
feature: fsmonitor--daemon
zlib: 1.3.1
SHA-1: SHA1_DC
SHA-256: SHA256_BLK
default-ref-format: files
default-hash: sha1
uname: Windows 10.0 22631
compiler info: gnuc: 15.2
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/bash

[System Info 2] git version: git version 2.47.3 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh zlib: 1.3.1 uname: Linux 6.12.74+deb13+1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.74-2 (2026-03-08) x86_64 compiler info: gnuc: 14.2 libc info: glibc: 2.41 $SHELL (typically, interactive shell): /bin/bash

[System Info 3] Ubuntu 20.04LTS git version : 2.25.1

[Enabled Hooks] yes , see reproduction script

Best Regards, Baptiste JEAN-LOUIS

Back to recent threads

[BUG] broken behaviour when running cd in a hook in a secondary worktree | The Git List