threads / bug / 65063

bug: for-each-repo malfunctions in worktree due to GIT_DIR

Subject: bug: for-each-repo malfunctions in worktree due to GIT_DIR

## tl;dr

2 messages between Feb 23, 2026 and Feb 24, 2026.

replies: 1people: 2as markdown or json

Matthew Gabeler-Lee· Feb 23, 2026, 22:58 UTC · lore

I noticed some scripts I have that utilize `git for-each-repo` don't work if I run them from within a git worktree, but they do work fine from within other directories, including the parent git clone of those worktrees. The symptom is that the command I pass to `for-each-repo` is run as if from within the worktree each time, instead of the repos fetched from the corresponding config entry.

After a bit of sleuthing with strace, I think I identified the cause: $GIT_DIR is set in the `git for-each-repo` process, and gets passed to the child git invocations it launches.

And thus, from within that worktree, `git for-each-repo --config=foo.bar -- branch` becomes the equivalent of:

GIT_DIR=/worktree-parent/.git git -C /unrelated-repo branch

Which of course is not what the `for-each-repo` invocation would be expected to do.

Observed with git 2.51.0 from Ubuntu 25.10. I haven't tested explicitly with latest, but I took a quick look at git.c and for-each-repo.c changes between there and master and didn't see anything that looked like it would affect this behavior.

PS: please CC me on replies
Derrick Stolee· Feb 24, 2026, 01:52 UTC · re: Matthew Gabeler-Lee · lore

Re: bug: for-each-repo malfunctions in worktree due to GIT_DIR

On 2/23/26 5:58 PM, Matthew Gabeler-Lee wrote:
Show 18 quoted lines
> I noticed some scripts I have that utilize `git for-each-repo` don't
> work if I run them from within a git worktree, but they do work fine
> from within other directories, including the parent git clone of those
> worktrees. The symptom is that the command I pass to `for-each-repo`
> is run as if from within the worktree each time, instead of the repos
> fetched from the corresponding config entry.
> 
> After a bit of sleuthing with strace, I think I identified the cause:
> $GIT_DIR is set in the `git for-each-repo` process, and gets passed to
> the child git invocations it launches.
> 
> And thus, from within that worktree, `git for-each-repo
> --config=foo.bar -- branch` becomes the equivalent of:
> 
> GIT_DIR=/worktree-parent/.git git -C /unrelated-repo branch
> 
> Which of course is not what the `for-each-repo` invocation would be
> expected to do.

You're absolutely right on this. It's a subtle oversight, as it's not one of the worktree-specific variables, but GIT_DIR is being set by the worktree logic.

I have a test and a fix on the way in [1]. You'll be CC'd when I send the patches after a CI run.

[1] https://github.com/gitgitgadget/git/pull/2056

Thanks, -Stolee

← back to recent threads