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

Re: Strange checkout with GIT_WORK_TREE

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 21, 2022, 23:09 UTC
Message-ID
<xmqqv8yceldw.fsf@gitster.g>
In-Reply-To
<CAHqym3xs_M7BvvFDq2pHM-+DgK_nJcBakVEBL-GiNwnCRzMwWA@mail.gmail.com>
Lauri Rooden <lauri@rooden.ee> writes:
> echo "Checkout to other work-tree: $GIT_COPY"
> GIT_WORK_TREE=$GIT_COPY git checkout HEAD~1

In this particular case, it happens that it does not make a difference, but it is discouraged to set GIT_WORK_TREE without setting GIT_DIR.

    GIT_DIR was invented as a way to say "I am at the top of the
    working tree but I do not have .git/ directory here---it is at
    that other place, i.e. $GIT_DIR".  And this was OK if you worked
    only at the top of the working tree.  GIT_WORK_TREE was then
    invented to help folks who do not always work at the top of the
    working tree.  With that, they can still set GIT_DIR to point at
    the location of the repository proper, use GIT_WORK_TREE to
    point at the top of the working tree, and chdir freely to any
    subdirectory of $GIT_WORK_TREE and expect things to work.
So, the more kosher way to do the above is
	echo "checkout to other work-tree: $GIT_COPY"
	cd "$GIT_COPY"
	GIT_DIR="$GIT_ROOT/.git" git checkout HEAD~

but what you wrote also happens to the same thing. We auto-discover the git directory (i.e. the current directory has .git/ directory in it, and that is used as the GIT_DIR).

Now, what you observed is totally expected.

Because HEAD~ in "git checkout HEAD~" is *not* a branch name, it is a short-hand of "git checkout --detach HEAD~". It is "I want to switch to the named commit, not on any branch. Note that I may have local changes, and please carry them when you switch to the commit".

And when you run the above "git checkout", do you have local changes? Yes, you do. Since

 * Your GIT_DIR is "$GIT_ROOT/.git"; your HEAD has two files, a.txt
   and b.txt, with 1 and 2 in it, respectively.  They match the
   index in that GIT_DIR.
 * Your working tree is "$GIT_COPY".  You do not have a.txt and
   b.txt.  I.e. your local change is to remove a.txt and b.txt

Now you want to move to the state of HEAD~, where each of a.txt and b.txt has 1 in it. So, if there weren't any local change, switching to this commit would change a.txt and b.txt to have 1 in them.

Now between HEAD and HEAD~, there was no change to a.txt; so we honor your local change to REMOVE a.txt. That is why you do not have "$GIT_COPY/a.txt" in the result.

But b.txt is different between HEAD and HEAD~. Pedantically, this *should* result in "the difference between two commits and your local change conflicts", and make the "checkout" command fail without doing anything. But there is an ancient special case to help working in a sparsely populated working tree that allows Git to treat "missing" file as the same as "unchanged" file, and I think that is what is kicking in. Instead of treating your "deletion" as something that conflicts with the change between HEAD and HEAD~ to turn 2 to 1, the command pretends as if you left b.txt as-is, and let you switch to HEAD~, instread of failing.

In any case, if you wanted to make a copy of a different commit into a separate directory, use of GIT_DIR/GIT_WORK_TREE is totally a wrong way to do so. If you go and look at $GIT_ROOT after your switching to "HEAD~", $GIT_ROOT/.git/HEAD and $GIT_ROOT/.git/index have moved to the "Initial" commit, but your working tree files in $GIT_ROOT are left as before, which would totally be confusing.

Using "git worktree" to create $GIT_COPY as an additional worktree, or "git clone" to create $GIT_COPY as an additional repository, may be what you are looking for.

Previous: brian m. carlsonNext: Lauri Rooden
Message 3 of 4 in “Strange checkout with GIT_WORK_TREE”
  1. Lauri RoodenJan 21, 2022
  2. brian m. carlsonJan 21, 2022
  3. Junio C HamanoJan 21, 2022
  4. Lauri RoodenJan 22, 2022

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.