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

Re: Seemingly inconsistent checkout behaviour

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 11, 2022, 12:14 UTC
Message-ID
<221011.86tu4ao8im.gmgdl@evledraar.gmail.com>
In-Reply-To
<DU2P194MB1584B11245DF0DE7515297B6E3239@DU2P194MB1584.EURP194.PROD.OUTLOOK.COM>
On Tue, Oct 11 2022, Bart Kuster wrote:
Show 17 quoted lines
> 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...
Previous: Bart KusterNext: Bart Kuster
Message 2 of 3 in “Seemingly inconsistent checkout behaviour”
  1. Bart KusterOct 11, 2022
  2. Ævar Arnfjörð BjarmasonOct 11, 2022
  3. Bart KusterOct 11, 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.