{"thread":{"id":"62194","subject":"Confusing error message for git switch -d","startedAt":"2024-09-25T22:30:30Z","lastAt":"2024-09-25T23:11:35Z","messageCount":2,"participants":["Anselm Schüler","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"503499","messageId":"ce9f0440-e724-4116-b63a-0e62c46c170d@anselmschueler.com","threadId":"62194","inReplyTo":null,"subject":"Confusing error message for git switch -d","fromName":"Anselm Schüler","fromEmail":"mail@anselmschueler.com","sentAt":"2024-09-25T22:25:16Z","receivedAt":"2024-09-25T22:30:30Z","isPatch":false,"sender":{"key":"mail@anselmschueler.com","avatar":"https://gravatar.com/avatar/fb911b2f7cf9596f7bbc61cd9799da5fe87733c0394d6ca79531e07368fd757f?d=mp&s=160"},"body":"Hi,\n\nI just ran into a confusing error message when using git switch -d, \nspecifically in a clone of the CPython code repository.\n\nI wanted to get the code for CPython 3.12, so I tried running git switch \n-d 3.12, expecting 3.12 to be a tag (which it wasn’t, and it was a \nremote branch).\n\nGit produced this error message:\n\nfatal: '--detach' cannot be used with '-b/-B/--orphan'\n\nThis is confusing since I didn’t pass any of these options.\n\nI believe I have determined that this error message occurs when using \ngit switch -d to switch to a remote branch that hasn’t yet had a local \nbranch that tracks it created for it.\n\nWhy is Git producing this error specifically?\nIt also seems that, whatever the reason, a user probably doesn’t know, \nso a better error message might be a good idea.\n\nTo clarify, if I’d known that my assumption of it being a tag was wrong, \nI’d’ve still expected Git to act differently, specifically, it could’ve \nswitched to the commit the branch is pointing to in detached HEAD mode \n(as it does when using -d with other branches).\n\nThanks\nAnselm\n\nPS: Are there no mailing list servers that support HTML messages? Why \nare they not allowed, even with plaintext fallback?\n\n"},{"id":"503500","messageId":"xmqqr097miqq.fsf@gitster.g","threadId":"62194","inReplyTo":"ce9f0440-e724-4116-b63a-0e62c46c170d@anselmschueler.com","subject":"Re: Confusing error message for git switch -d","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-09-25T23:11:25Z","receivedAt":"2024-09-25T23:11:35Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Anselm Schüler <mail@anselmschueler.com> writes:\n\n> I wanted to get the code for CPython 3.12, so I tried running git\n> switch -d 3.12, expecting 3.12 to be a tag (which it wasn’t, and it\n> was a remote branch).\n>\n> Git produced this error message:\n>\n> fatal: '--detach' cannot be used with '-b/-B/--orphan'\n>\n> This is confusing since I didn’t pass any of these options.\n\nTrue.  FWIW, this is shared between \"checkout\" and \"switch', and I\nagree that there is a huge room for improvement.\n\nWith (arguably overly aggressive for its own worth) the \"--guess\"\noption being default, giving the name of a unique remote-tracking\nbranch (that is, \"3.12\" appears as a remote-tracking branch of only\none remote, among possibly multiple remotes you have defined for the\nrepository) would behave as if what you said, which is \"3.12\", is\nequivalent to \"-b 3.12 --track origin/3.12\" (where the \"origin\"\nthing is the \"only one\" remote for which 3.12 exists as a\nremote-tracking branch).  So\n\n    $ git switch --detach 3.12\n\nbehaves as if you said\n\n    $ git switch --detach -b 3.12 --track origin/3.12\n\nand the error message is given.\n\nI am merely saying that the mechanism from which this error message\nis given can be _explained_.  I do not think it is well designed, as\nit does not give a great end-user experience.\n\nDisabling checkout.guess configuration would force you into the\nhabit of being a bit more explicit.  Even with checkout.guess\nconfigured to false, you can still say\n\n    $ git switch --detach origin/3.12\n\nand because this does not blindly translate to -b/--track, you would\nnot see such an error.\n\nA more appropriate design for \"--guess\" would be to notice the\npresence of \"--detach\" and act differently, i.e. not rewriting\nblindly 3.12 to \"-b 3.12 -t origin/3.12\" unconditionally, instead\nrewrite \"--detacu 3.12\" to \"--detach origin/3.12\" if \"--detach\" is\nthere.\n\nPatches welcome ;-)\n\n"}]}