threads / discuss / 57282

Strange checkout with GIT_WORK_TREE

Subject: Strange checkout with GIT_WORK_TREE

## tl;dr

4 messages between Jan 21, 2022 and Jan 22, 2022.

replies: 3people: 3as markdown or json

Lauri Rooden· Jan 21, 2022, 16:37 UTC · lore

Does the GIT_WORK_TREE get lost on the middle of process or I am misunderstand the git checkout?

What did you do before the bug happened? (Steps to reproduce your issue)
- I wrote a shell script to reproduce

```git-test.sh GIT_ROOT=$(mktemp -d) GIT_COPY=$(mktemp -d)

echo "Create git repo with two commits: $GIT_ROOT" cd $GIT_ROOT git init echo 1 > a.txt echo 1 > b.txt git add *.txt git commit -m "Initial commit" echo 2 > b.txt git add b.txt git commit -m "Second commit"

echo "Checkout to other work-tree: $GIT_COPY" GIT_WORK_TREE=$GIT_COPY git checkout HEAD~1 git status

echo "ORIGIN $GIT_ROOT" ls -la $GIT_ROOT echo "COPY $GIT_COPY" ls -la $GIT_COPY ```

What did you expect to happen? (Expected behavior)
- a.txt and b.txt checkouted to $GIT_COPY both with content `1`
- current folder unchanged
What happened instead? (Actual behavior)
- only b.txt checkouted to $GIT_COPY
- HEAD~1 checkouted in current folder but folder content remains HEAD
(modified:   b.txt)

[System Info] git version: git version 2.34.1 cpu: x86_64 no commit associated with this build sizeof-long: 8 sizeof-size_t: 8 shell-path: /bin/sh uname: Linux 5.16.0-arch1-1 #1 SMP PREEMPT Mon, 10 Jan 2022 20:11:47 +0000 x86_64 compiler info: gnuc: 11.1 libc info: glibc: 2.33 $SHELL (typically, interactive shell): /usr/bin/bash

[Enabled Hooks] prepare-commit-msg

brian m. carlson· Jan 21, 2022, 22:09 UTC · re: Lauri Rooden · lore

Re: Strange checkout with GIT_WORK_TREE

On 2022-01-21 at 16:37:58, Lauri Rooden wrote:
Show 38 quoted lines
> Does the GIT_WORK_TREE get lost on the middle of process
> or I am misunderstand the git checkout?
> 
> What did you do before the bug happened? (Steps to reproduce your issue)
> - I wrote a shell script to reproduce
> 
> ```git-test.sh
> GIT_ROOT=$(mktemp -d)
> GIT_COPY=$(mktemp -d)
> 
> echo "Create git repo with two commits: $GIT_ROOT"
> cd $GIT_ROOT
> git init
> echo 1 > a.txt
> echo 1 > b.txt
> git add *.txt
> git commit -m "Initial commit"
> echo 2 > b.txt
> git add b.txt
> git commit -m "Second commit"
> 
> echo "Checkout to other work-tree: $GIT_COPY"
> GIT_WORK_TREE=$GIT_COPY git checkout HEAD~1
> git status
> 
> echo "ORIGIN $GIT_ROOT"
> ls -la $GIT_ROOT
> echo "COPY $GIT_COPY"
> ls -la $GIT_COPY
> ```
> 
> What did you expect to happen? (Expected behavior)
> - a.txt and b.txt checkouted to $GIT_COPY both with content `1`
> - current folder unchanged
> 
> What happened instead? (Actual behavior)
> - only b.txt checkouted to $GIT_COPY
> - HEAD~1 checkouted in current folder but folder content remains HEAD

Here's what I believe is happening here. When you run "git checkout HEAD~1", Git notices that the only file that's stale in the index compared to what's already present is b.txt; a.txt is up to date. As such, it only writes one file into the working tree, since only one file needs to be updated. This is an optimization, since for large working trees, writing out every file every time would be extremely expensive.

I don't know what our official position is on switching working trees like this. I would generally recommend you pick one and stick to it, but if you want to do this, you'll need to update the index for the working tree first. You can do that by something like this in place of your checkout:

  GIT_WORK_TREE=$GIT_COPY git update-index --refresh
  GIT_WORK_TREE=$GIT_COPY git checkout -f HEAD~1

That will inform Git that your working copy is stale and then the -f flag to checkout will force Git to overwrite the missing files.

If your goal is to have multiple worktrees for one repository, you can do that with "git worktree", which will keep separate indices for your separate directories, provided they're on separate branches. If you just want to create a detached copy of the files for a repository from a commit, you can do this:

  git archive HEAD~1 | tar -C "$GIT_COPY" -xf -

Hopefully that explains what's going on. If you tell us a little bit more about what you wanted to accomplish, we may be able to help you find a way to do it that provides results that are at least less surprising and more predictable.

-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA
Junio C Hamano· Jan 21, 2022, 23:09 UTC · re: Lauri Rooden · lore

Re: Strange checkout with GIT_WORK_TREE

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.

Lauri Rooden· Jan 22, 2022, 09:06 UTC · re: Junio C Hamano · lore

Re: Strange checkout with GIT_WORK_TREE

Thank you Brian and Junio for the extensive explanation!
Now all makes sense and the new (for me) "git worktree" feature seems promising.

I wanted to have $GIT_COPY without .git folder but a .git file does not bother me.

Previously I have checkouted one file this way and then it seemed to work, confusion raised when I tried to switsh to full repo checkout.

← back to recent threads