{"thread":{"id":"65709","subject":"[BUG] broken behaviour when running cd in a hook in a secondary worktree","startedAt":"2026-05-28T21:03:42Z","lastAt":"2026-05-28T21:03:42Z","messageCount":1,"participants":["Baptiste Jean-Louis"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"544245","messageId":"CAOtV6-95tzd-iBW+DcUHBTXvQF0TU+-pimATw1aYyo7nhDEofA@mail.gmail.com","threadId":"65709","inReplyTo":null,"subject":"[BUG] broken behaviour when running cd in a hook in a secondary worktree","fromName":"Baptiste Jean-Louis","fromEmail":"baptiste.geek98@gmail.com","sentAt":"2026-05-28T21:03:29Z","receivedAt":"2026-05-28T21:03:42Z","isPatch":false,"body":"Hello,\nThank you for your great work\nhere is a bug I encountered\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n#!/bin/bash\n\nmkdir topfolder\ncd topfolder\nmkdir main-worktree\ncd main-worktree\ngit init\nmkdir dir\ntouch file-a dir/file-b\ngit add .\ngit commit -m \"Initial commit\":\n\ncat >.git/hooks/cd-bug << EOF\n#!/bin/bash\n\necho -e \"\\n\\nrunning cd-bug hook\"\n# Redirect output to stderr.\nexec 1>&2\n\necho \"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\"\ngit describe\necho pwd : $(pwd)\necho pwd_var : $PWD\necho git_prefix: $GIT_PREFIX\necho git_dir: $GIT_DIR\necho git_work_tree: $GIT_WORK_TREE\ngit status\necho \"========================================================\"\ncd dir\necho pwd : $(pwd)\necho pwd_var : $PWD\necho git_prefix: $GIT_PREFIX\necho git_dir: $GIT_DIR\necho git_work_tree: $GIT_WORK_TREE\ngit status\necho \">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\"\necho -e \"cd-bug hook done\\n\\n\"\n\nEOF\nchmod u+x .git/hooks/cd-bug\n\n# behave as expected\ngit hook run cd-bug\n\ngit branch branch_b\ngit worktree add ../second-worktree branch_b\ncd ../second-worktree\n\n# broken behaviour here\ngit hook run cd-bug\n\n# end of script\n\n\n\nWhat did you expect to happen? (Expected behavior)\nOn branch branch_b\nnothing to commit, working tree clean\n\n\nWhat happened instead? (Actual behavior)\nOn branch branch_b\nChanges not staged for commit:\n  (use \"git add/rm <file>...\" to update what will be committed)\n  (use \"git restore <file>...\" to discard changes in working directory)\n        deleted:    dir/file-b\n        deleted:    file-a\n\nUntracked files:\n  (use \"git add <file>...\" to include in what will be committed)\n        file-b\n\n\nWhat's different between what you expected and what actually happened?\n-> Running `git status` after `cd` in a pre-commit hook lists all\nrepo's tracked file as deleted.\n   However when running the hook from the main-worktree, I have no issue.\n\n   In my final use case, I'm doing something that goes like below\n   cd subfolder\n   `git stash --keep-index`\n   `./update-generated-files` which updates some files in subfolder\nand subfolder/*/\n   `git stash pop`\n\n\n\n[System Info]\ngit version:\ngit version 2.51.1.windows.1\ncpu: x86_64\nbuilt from commit: 1454f0a9c4a3a22fb3fd7cc33f76f88cd65ced41\nsizeof-long: 4\nsizeof-size_t: 8\nshell-path: D:/git-sdk-64-build-installers/usr/bin/sh\nfeature: fsmonitor--daemon\nzlib: 1.3.1\nSHA-1: SHA1_DC\nSHA-256: SHA256_BLK\ndefault-ref-format: files\ndefault-hash: sha1\nuname: Windows 10.0 22631\ncompiler info: gnuc: 15.2\nlibc info: no libc information available\n$SHELL (typically, interactive shell): /bin/bash\n\n\n\n[System Info 2]\ngit version:\ngit version 2.47.3\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nzlib: 1.3.1\nuname: Linux 6.12.74+deb13+1-amd64 #1 SMP PREEMPT_DYNAMIC Debian\n6.12.74-2 (2026-03-08) x86_64\ncompiler info: gnuc: 14.2\nlibc info: glibc: 2.41\n$SHELL (typically, interactive shell): /bin/bash\n\n[System Info 3]\nUbuntu 20.04LTS\ngit version : 2.25.1\n\n\n\n[Enabled Hooks]\nyes , see reproduction script\n\nBest Regards,\nBaptiste JEAN-LOUIS\n"}]}