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

Re: Bug report between git hooks and git worktree

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jan 8, 2023, 06:32 UTC
Message-ID
<CAPig+cScBFJCBN97g12v9Eg1-7+m8_EzAnpT2uSSdzTNwqjjVQ@mail.gmail.com>
In-Reply-To
<CAC-j02OVoFDFjeGUjcs0ZMwPSXKLL_GBme2m2-ttzGTbxXNP-w@mail.gmail.com>

On Sat, Jan 7, 2023 at 2:20 PM Preston Tunnell Wilson <prestontunnellwilson@gmail.com> wrote:

Show 10 quoted lines
> What did you do before the bug happened? (Steps to reproduce your issue)
>
> Add a git hook that executes git commands inside of another folder. Let's
> call this other folder `FolderA`.
> Add a git worktree branch/folder, `cd` to it, and commit something there.
> Let's call this git worktree folder `FolderW`.
>
> I would expect the git command to output details from `FolderA`.
> It's confusing to me that there is a difference in behavior between
> the "main" branch and `FolderW` in git hooks.

This issue comes up from time to time[1][2]. What is happening is that Git is setting environment variables pointing at the original repository, so even though you invoke a Git command in a different repository, it's picking up the environment variables and consulting the original repository instead. When writing hooks which invoke Git commands in a foreign repository, the "best practice" approach is to clear the environment variables before running the Git command in the other repository[3]. If you add this to the top of your hook script, it works as expected:

    unset $(git rev-parse --local-env-vars)
Unfortunately, this "fix" isn't actually documented anywhere.

[1]: https://lore.kernel.org/git/CAPig+cQEC6CAV-Es9Ok96s8Cj=Dj76PRyOt4qKQus+rppswuyA@mail.gmail.com/ [2]: https://lore.kernel.org/git/CAJFQqN+Z9eX6onaj8vVSqpvf-nOC7-Y0Un4NLUie6x6bGfmvZA@mail.gmail.com/ [3]: https://lore.kernel.org/git/20190516221702.GA11784@sigill.intra.peff.net/

Previous: Preston Tunnell Wilson
Message 3 of 3 in “Bug report between git hooks and git worktree”
  1. Preston Tunnell WilsonJan 7, 2023
  2. Preston Tunnell WilsonJan 7, 2023
  3. Eric SunshineJan 8, 2023

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.