git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Seemingly inconsistent checkout behaviour
@ 2022-10-11 11:39 Bart Kuster
  2022-10-11 12:14 ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Kuster @ 2022-10-11 11:39 UTC (permalink / raw)
  To: git@vger.kernel.org

Dear People of Git,

I had some trouble getting out of a detached HEAD state using git checkout. I’ve solved it now but the behaviour of checkout when doing so leaves me a bit puzzled.

I’d expect the behaviour of the commands below to be equivalent:

   git checkout origin/main

vs

   git config checkout.defaultRemote origin
   git checkout main

But they are not; the former leaves the HEAD detached while the latter sets it to main. I failed to find an explanation in the git-checkout documentation, which seems to indicate that checkout always updates HEAD.

Could someone clarify what's going on?

A bit more elaborate transcript of what I did is below.

Thanks in advance!

Kind regards,
Bart Kuster

---

# Checkout a commit to get into detached HEAD state
$ git checkout aac02b81d735d5f15b3b3c61a84ccb79a8a90145
HEAD is now at aac02b8 Merge branch 'main' of https://repo into main
$ git checkout origin/main
HEAD is now at aac02b8 Merge branch 'main' of https://repo into main
$ git status
HEAD detached at aac02b8
(...)
$ git config checkout.defaultRemote origin
$ git checkout main
On branch main
(...)
$ git status
On branch main
$

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Seemingly inconsistent checkout behaviour
  2022-10-11 11:39 Seemingly inconsistent checkout behaviour Bart Kuster
@ 2022-10-11 12:14 ` Ævar Arnfjörð Bjarmason
  2022-10-11 13:03   ` Bart Kuster
  0 siblings, 1 reply; 3+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-10-11 12:14 UTC (permalink / raw)
  To: Bart Kuster; +Cc: git@vger.kernel.org


On Tue, Oct 11 2022, Bart Kuster wrote:

> Dear People of Git,
>
> I had some trouble getting out of a detached HEAD state using git checkout. I’ve solved it now but the behaviour of checkout when doing so leaves me a bit puzzled.
>
> I’d expect the behaviour of the commands below to be equivalent:
>
>    git checkout origin/main
>
> vs
>
>    git config checkout.defaultRemote origin
>    git checkout main
>
> But they are not; the former leaves the HEAD detached while the latter
> sets it to main. I failed to find an explanation in the git-checkout
> documentation, which seems to indicate that checkout always updates
> HEAD.

They're not equivalent, the documentation that describes it is the
discussion of the --guess option.

I.e. these are equivalent:

	git checkout main
	git checkout --guess main

And the latter of those resolves to (in this case):

	git checkout -b main -t origin/main

The "in this case" being that it's actually (pseudocode):

	git checkout -b main -t $(find-the-only-remote-that-has-a-branch-named main)/main

And the "checkout.defaultRemote=origin" is a setting that disambiguates
that "find the only" down to "origin", if you happen to have multiple
such remotes.

But I'm still unclear on the "I had some trouble getting out of a
detached HEAD state using git checkout" part of your question, isn't the:

	git config checkout.defaultRemote origin
	git checkout main

Exactly what you want then?

If you're trying to get a "non-detached remote tracking branch", i.e. to
have this somehow checkout a branch:

	git checkout origin/main

Then there's no such thing. We treat them specially, and don't allow you
to check them out as a normal branch, so when you do so you end up with
a detached HEAD pointing to wherever that branch points to. IOW:

	git checkout origin/main

Is always the same as:

	git checkout origin/main^{}

Given that you have an "origin" remote, a "main" branch, it's not
ambiguous, or the right config blah blah.

That's also documented somewhere, but I didn't dig up where that is...

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: Seemingly inconsistent checkout behaviour
  2022-10-11 12:14 ` Ævar Arnfjörð Bjarmason
@ 2022-10-11 13:03   ` Bart Kuster
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Kuster @ 2022-10-11 13:03 UTC (permalink / raw)
  To: git@vger.kernel.org


On Tue, Oct 11 2022, Ævar Arnfjörð Bjarmason wrote:

>On Tue, Oct 11 2022, Bart Kuster wrote:
>
>> Dear People of Git,
>>
>> I had some trouble getting out of a detached HEAD state using git checkout. I’ve solved it now but the behaviour of checkout when doing so leaves me a bit puzzled.
>>
>> I’d expect the behaviour of the commands below to be equivalent:
>>
>>    git checkout origin/main
>>
>> vs
>>
>>    git config checkout.defaultRemote origin
>>    git checkout main
>>
>> But they are not; the former leaves the HEAD detached while the latter 
>> sets it to main. I failed to find an explanation in the git-checkout 
>> documentation, which seems to indicate that checkout always updates 
>> HEAD.
>
>They're not equivalent, the documentation that describes it is the discussion of the --guess option.
>
>I.e. these are equivalent:
>
>	git checkout main
>	git checkout --guess main
>
>And the latter of those resolves to (in this case):
>
>	git checkout -b main -t origin/main
>
>The "in this case" being that it's actually (pseudocode):
>
>	git checkout -b main -t $(find-the-only-remote-that-has-a-branch-named main)/main
>
>And the "checkout.defaultRemote=origin" is a setting that disambiguates that "find the only" down to "origin", if you happen to have multiple such remotes.
>
>But I'm still unclear on the "I had some trouble getting out of a detached HEAD state using git checkout" part of your question, isn't the:
>
>	git config checkout.defaultRemote origin
>	git checkout main
>
>Exactly what you want then?
>
>If you're trying to get a "non-detached remote tracking branch", i.e. to have this somehow checkout a branch:
>
>	git checkout origin/main
>
>Then there's no such thing. We treat them specially, and don't allow you to check them out as a normal branch, so when you do so you end up with a detached HEAD pointing to wherever that branch points to. IOW:
>
>	git checkout origin/main
>
>Is always the same as:
>
>	git checkout origin/main^{}
>
>Given that you have an "origin" remote, a "main" branch, it's not ambiguous, or the right config blah blah.
>
>That's also documented somewhere, but I didn't dig up where that is...
>

Thanks for your quick reply! I understand the situation now.

What put me on the wrong track, is that git checkout <remote>/<branch> is not explicitly discussed in the git-checkout documentation, leading me to believe the default use case would apply to it while it does not. The last part of your explanation clarifies what's really going on. Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-11 13:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-11 11:39 Seemingly inconsistent checkout behaviour Bart Kuster
2022-10-11 12:14 ` Ævar Arnfjörð Bjarmason
2022-10-11 13:03   ` Bart Kuster

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).