{"thread":{"id":"58600","subject":"Seemingly inconsistent checkout behaviour","startedAt":"2022-10-11T11:39:15Z","lastAt":"2022-10-11T13:03:40Z","messageCount":3,"participants":["Bart Kuster","Ævar Arnfjörð Bjarmason"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"464601","messageId":"DU2P194MB1584B11245DF0DE7515297B6E3239@DU2P194MB1584.EURP194.PROD.OUTLOOK.COM","threadId":"58600","inReplyTo":null,"subject":"Seemingly inconsistent checkout behaviour","fromName":"Bart Kuster","fromEmail":"bart.kuster@sidn.nl","sentAt":"2022-10-11T11:39:07Z","receivedAt":"2022-10-11T11:39:15Z","isPatch":false,"sender":{"key":"bart.kuster@sidn.nl","avatar":null},"body":"Dear People of Git,\n\nI 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.\n\nI’d expect the behaviour of the commands below to be equivalent:\n\n   git checkout origin/main\n\nvs\n\n   git config checkout.defaultRemote origin\n   git checkout main\n\nBut 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.\n\nCould someone clarify what's going on?\n\nA bit more elaborate transcript of what I did is below.\n\nThanks in advance!\n\nKind regards,\nBart Kuster\n\n---\n\n# Checkout a commit to get into detached HEAD state\n$ git checkout aac02b81d735d5f15b3b3c61a84ccb79a8a90145\nHEAD is now at aac02b8 Merge branch 'main' of https://repo into main\n$ git checkout origin/main\nHEAD is now at aac02b8 Merge branch 'main' of https://repo into main\n$ git status\nHEAD detached at aac02b8\n(...)\n$ git config checkout.defaultRemote origin\n$ git checkout main\nOn branch main\n(...)\n$ git status\nOn branch main\n$\n"},{"id":"464602","messageId":"221011.86tu4ao8im.gmgdl@evledraar.gmail.com","threadId":"58600","inReplyTo":"DU2P194MB1584B11245DF0DE7515297B6E3239@DU2P194MB1584.EURP194.PROD.OUTLOOK.COM","subject":"Re: Seemingly inconsistent checkout behaviour","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-10-11T12:14:37Z","receivedAt":"2022-10-11T12:22:05Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Oct 11 2022, Bart Kuster wrote:\n\n> Dear People of Git,\n>\n> 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.\n>\n> I’d expect the behaviour of the commands below to be equivalent:\n>\n>    git checkout origin/main\n>\n> vs\n>\n>    git config checkout.defaultRemote origin\n>    git checkout main\n>\n> But they are not; the former leaves the HEAD detached while the latter\n> sets it to main. I failed to find an explanation in the git-checkout\n> documentation, which seems to indicate that checkout always updates\n> HEAD.\n\nThey're not equivalent, the documentation that describes it is the\ndiscussion of the --guess option.\n\nI.e. these are equivalent:\n\n\tgit checkout main\n\tgit checkout --guess main\n\nAnd the latter of those resolves to (in this case):\n\n\tgit checkout -b main -t origin/main\n\nThe \"in this case\" being that it's actually (pseudocode):\n\n\tgit checkout -b main -t $(find-the-only-remote-that-has-a-branch-named main)/main\n\nAnd the \"checkout.defaultRemote=origin\" is a setting that disambiguates\nthat \"find the only\" down to \"origin\", if you happen to have multiple\nsuch remotes.\n\nBut I'm still unclear on the \"I had some trouble getting out of a\ndetached HEAD state using git checkout\" part of your question, isn't the:\n\n\tgit config checkout.defaultRemote origin\n\tgit checkout main\n\nExactly what you want then?\n\nIf you're trying to get a \"non-detached remote tracking branch\", i.e. to\nhave this somehow checkout a branch:\n\n\tgit checkout origin/main\n\nThen there's no such thing. We treat them specially, and don't allow you\nto check them out as a normal branch, so when you do so you end up with\na detached HEAD pointing to wherever that branch points to. IOW:\n\n\tgit checkout origin/main\n\nIs always the same as:\n\n\tgit checkout origin/main^{}\n\nGiven that you have an \"origin\" remote, a \"main\" branch, it's not\nambiguous, or the right config blah blah.\n\nThat's also documented somewhere, but I didn't dig up where that is...\n"},{"id":"464604","messageId":"DU2P194MB15841850A17436C7AE34C149E3239@DU2P194MB1584.EURP194.PROD.OUTLOOK.COM","threadId":"58600","inReplyTo":"221011.86tu4ao8im.gmgdl@evledraar.gmail.com","subject":"RE: Seemingly inconsistent checkout behaviour","fromName":"Bart Kuster","fromEmail":"bart.kuster@sidn.nl","sentAt":"2022-10-11T13:03:20Z","receivedAt":"2022-10-11T13:03:40Z","isPatch":false,"sender":{"key":"bart.kuster@sidn.nl","avatar":null},"body":"\nOn Tue, Oct 11 2022, Ævar Arnfjörð Bjarmason wrote:\n\n>On Tue, Oct 11 2022, Bart Kuster wrote:\n>\n>> Dear People of Git,\n>>\n>> 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.\n>>\n>> I’d expect the behaviour of the commands below to be equivalent:\n>>\n>>    git checkout origin/main\n>>\n>> vs\n>>\n>>    git config checkout.defaultRemote origin\n>>    git checkout main\n>>\n>> But they are not; the former leaves the HEAD detached while the latter \n>> sets it to main. I failed to find an explanation in the git-checkout \n>> documentation, which seems to indicate that checkout always updates \n>> HEAD.\n>\n>They're not equivalent, the documentation that describes it is the discussion of the --guess option.\n>\n>I.e. these are equivalent:\n>\n>\tgit checkout main\n>\tgit checkout --guess main\n>\n>And the latter of those resolves to (in this case):\n>\n>\tgit checkout -b main -t origin/main\n>\n>The \"in this case\" being that it's actually (pseudocode):\n>\n>\tgit checkout -b main -t $(find-the-only-remote-that-has-a-branch-named main)/main\n>\n>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.\n>\n>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:\n>\n>\tgit config checkout.defaultRemote origin\n>\tgit checkout main\n>\n>Exactly what you want then?\n>\n>If you're trying to get a \"non-detached remote tracking branch\", i.e. to have this somehow checkout a branch:\n>\n>\tgit checkout origin/main\n>\n>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:\n>\n>\tgit checkout origin/main\n>\n>Is always the same as:\n>\n>\tgit checkout origin/main^{}\n>\n>Given that you have an \"origin\" remote, a \"main\" branch, it's not ambiguous, or the right config blah blah.\n>\n>That's also documented somewhere, but I didn't dig up where that is...\n>\n\nThanks for your quick reply! I understand the situation now.\n\nWhat 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!\n"}]}