{"thread":{"id":"65063","subject":"bug: for-each-repo malfunctions in worktree due to GIT_DIR","startedAt":"2026-02-23T22:58:45Z","lastAt":"2026-02-24T01:52:12Z","messageCount":2,"participants":["Matthew Gabeler-Lee","Derrick Stolee"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"536889","messageId":"CABpCjbY=wpStuhxqRJ5TSNV3A-CmN-g-xZGJOQGSSv3GYhs2fQ@mail.gmail.com","threadId":"65063","inReplyTo":null,"subject":"bug: for-each-repo malfunctions in worktree due to GIT_DIR","fromName":"Matthew Gabeler-Lee","fromEmail":"fastcat@gmail.com","sentAt":"2026-02-23T22:58:07Z","receivedAt":"2026-02-23T22:58:45Z","isPatch":false,"sender":{"key":"fastcat@gmail.com","avatar":null},"body":"I noticed some scripts I have that utilize `git for-each-repo` don't\nwork if I run them from within a git worktree, but they do work fine\nfrom within other directories, including the parent git clone of those\nworktrees. The symptom is that the command I pass to `for-each-repo`\nis run as if from within the worktree each time, instead of the repos\nfetched from the corresponding config entry.\n\nAfter a bit of sleuthing with strace, I think I identified the cause:\n$GIT_DIR is set in the `git for-each-repo` process, and gets passed to\nthe child git invocations it launches.\n\nAnd thus, from within that worktree, `git for-each-repo\n--config=foo.bar -- branch` becomes the equivalent of:\n\nGIT_DIR=/worktree-parent/.git git -C /unrelated-repo branch\n\nWhich of course is not what the `for-each-repo` invocation would be\nexpected to do.\n\nObserved with git 2.51.0 from Ubuntu 25.10. I haven't tested\nexplicitly with latest, but I took a quick look at git.c and\nfor-each-repo.c changes between there and master and didn't see\nanything that looked like it would affect this behavior.\n\nPS: please CC me on replies\n"},{"id":"536900","messageId":"1d4d795d-3a0b-47c5-864f-56f3c7601d39@gmail.com","threadId":"65063","inReplyTo":"CABpCjbY=wpStuhxqRJ5TSNV3A-CmN-g-xZGJOQGSSv3GYhs2fQ@mail.gmail.com","subject":"Re: bug: for-each-repo malfunctions in worktree due to GIT_DIR","fromName":"Derrick Stolee","fromEmail":"stolee@gmail.com","sentAt":"2026-02-24T01:52:10Z","receivedAt":"2026-02-24T01:52:12Z","isPatch":false,"sender":{"key":"stolee@gmail.com","avatar":"https://avatars.githubusercontent.com/u/570044?v=4"},"body":"On 2/23/26 5:58 PM, Matthew Gabeler-Lee wrote:\n> I noticed some scripts I have that utilize `git for-each-repo` don't\n> work if I run them from within a git worktree, but they do work fine\n> from within other directories, including the parent git clone of those\n> worktrees. The symptom is that the command I pass to `for-each-repo`\n> is run as if from within the worktree each time, instead of the repos\n> fetched from the corresponding config entry.\n> \n> After a bit of sleuthing with strace, I think I identified the cause:\n> $GIT_DIR is set in the `git for-each-repo` process, and gets passed to\n> the child git invocations it launches.\n> \n> And thus, from within that worktree, `git for-each-repo\n> --config=foo.bar -- branch` becomes the equivalent of:\n> \n> GIT_DIR=/worktree-parent/.git git -C /unrelated-repo branch\n> \n> Which of course is not what the `for-each-repo` invocation would be\n> expected to do.\nYou're absolutely right on this. It's a subtle oversight, as it's not\none of the worktree-specific variables, but GIT_DIR is being set by\nthe worktree logic.\n\nI have a test and a fix on the way in [1]. You'll be CC'd when I send\nthe patches after a CI run.\n\n[1] https://github.com/gitgitgadget/git/pull/2056\n\nThanks,\n-Stolee\n\n"}]}