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

Re: behaviour of git diff, GIT_DIR & checked out tree

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 24, 2007, 16:47 UTC
Message-ID
<7v6420au43.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<Pine.LNX.4.64.0709241400410.28395@racer.site>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 14 quoted lines
> On Mon, 24 Sep 2007, David Tweed wrote:
>
>> ... In
>> a different directory, OUTSIDE of $HOME/V, I tried
>> 
>> env GIT_DIR=$HOME/V/.git git diff master@{midnight}
>> 
>> to get the same effect but, whilst I do get a diff output, it
>> looks like a diff of the commit against an empty tree.
>
> Yes, this is fully expected.
>
> The @{time} notation accesses the _reflogs_, which are purely local 
> beasts.  They are not transmitted when cloning.

Yeah, but my reading of the problem description suggests the two cases refer to the same repository (hence the same reflogs).

The issue is that the second case runs "git diff <one-tree>" in a random directory. This form is about comparing the part of work tree you are in with a given tree, and does not make _any_ sense when outside the work tree. Usually without GIT_DIR environment, the command would give you an error message.

With $GIT_DIR, but without $GIT_WORK_TREE, the user is telling the command that it is being run at the top level of the work tree and the repository metadata is not in the usual ".git" subdirectory of the top level of the work tree (in this case, by definition that is "$PWD/.git") but elsewhere where $GIT_DIR specifies. Because it is very likely that the files under the random location does not share much resemblance to what are in $HOME/V, it is not surprising that the output consisted of many deletions.

It would be illustrative if David did the following.
	$ cd $HOME
        $ cp -a V W
        $ rm -fr W/.git
        $ cd W
        $ GIT_DIR=$HOME/V/.git git diff @{midnight}

Now we are in a random place outside of the work tree (i.e. "W"), and we drive "git diff" with GIT_DIR specified, telling it to pretend that we are at the top level of the work tree. So ~/W/frotz is compared with "frotz" at the top level of the commit (which usually is compared with ~/V/frotz), etc. But the directory we happen to be in very much resembles the work tree, so it would give identical results to

	$ cd $HOME/V
        $ git diff @{midnight}
Previous: David TweedNext: David Tweed
Message 4 of 6 in “behaviour of git diff, GIT_DIR & checked out tree”
  1. David TweedSep 24, 2007
  2. Johannes SchindelinSep 24, 2007
  3. David TweedSep 24, 2007
  4. Junio C HamanoSep 24, 2007
  5. David TweedSep 24, 2007
  6. Junio C HamanoSep 24, 2007

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.