git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Lauri Rooden <lauri@rooden.ee>
Cc: git@vger.kernel.org
Subject: Re: Strange checkout with GIT_WORK_TREE
Date: Fri, 21 Jan 2022 15:09:47 -0800	[thread overview]
Message-ID: <xmqqv8yceldw.fsf@gitster.g> (raw)
In-Reply-To: <CAHqym3xs_M7BvvFDq2pHM-+DgK_nJcBakVEBL-GiNwnCRzMwWA@mail.gmail.com> (Lauri Rooden's message of "Fri, 21 Jan 2022 18:37:58 +0200")

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.






  parent reply	other threads:[~2022-01-21 23:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21 16:37 Strange checkout with GIT_WORK_TREE Lauri Rooden
2022-01-21 22:09 ` brian m. carlson
2022-01-21 23:09 ` Junio C Hamano [this message]
2022-01-22  9:06   ` Lauri Rooden

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqv8yceldw.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=lauri@rooden.ee \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).