{"thread":{"id":"50795","subject":"Bug: git checkout @{u} breaks git branch if the upstream isn't a local branch","startedAt":"2019-03-21T15:43:14Z","lastAt":"2019-03-22T05:02:47Z","messageCount":2,"participants":["Leszek Swirski","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"372140","messageId":"CAGRskv__Qe-F-grBtq5v-uhHE+BQAcAcOQtX-GKB+MNOrxs0hQ@mail.gmail.com","threadId":"50795","inReplyTo":null,"subject":"Bug: git checkout @{u} breaks git branch if the upstream isn't a local branch","fromName":"Leszek Swirski","fromEmail":"leszeks@google.com","sentAt":"2019-03-21T15:43:01Z","receivedAt":"2019-03-21T15:43:14Z","isPatch":false,"sender":{"key":"leszeks@google.com","avatar":null},"body":"Consider a repository with branch master, with upstream origin/master.\nIf one is on master, and runs \"git checkout @{u}\" to get the upstream,\nthen git branch stops working. The reason is that we end up on a\ndetached HEAD, and git branch searches recent reflog entries to figure\nout where the HEAD was detached from (wt-status.c:\nwt_status_get_detached_from). Since @{u} is not a local branch, the\ncheckout is inserted into the reflog as \"moving from master to @{u}\"\nrather than \"moving from master to origin/master\". This is because\nsetup_branch_path (checkout.c) only updates the branch name for local\nbranches, keeping the input name (here, \"@{u}\") otherwise. So, git\nbranch sees \"@{u}\", decides that we are detached from \"@{u}\", tries to\nlookup \"HEAD@{u}\" and fails with \"fatal: HEAD does not point to a\nbranch\"\n\nI assume that either the reflog entry should be amended to print\nsomething more informative (moving from master to origin/master, or at\nleast master@{u}, or the SHA...), or the reflog search in\nwt_status_get_detached_from should do something cleverer with relative\nrefs.\n\n- Leszek\n"},{"id":"372201","messageId":"xmqq7ecrqz65.fsf@gitster-ct.c.googlers.com","threadId":"50795","inReplyTo":"CAGRskv__Qe-F-grBtq5v-uhHE+BQAcAcOQtX-GKB+MNOrxs0hQ@mail.gmail.com","subject":"Re: Bug: git checkout @{u} breaks git branch if the upstream isn't a local branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-03-22T05:02:42Z","receivedAt":"2019-03-22T05:02:47Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Leszek Swirski <leszeks@google.com> writes:\n\n> I assume that either the reflog entry should be amended to print\n> something more informative (moving from master to origin/master, or at\n> least master@{u}, or the SHA...), or the reflog search in\n> wt_status_get_detached_from should do something cleverer with relative\n> refs.\n\nNicely analyzed. A record \"moving to @{u}\" in the reflog may not\nleave sufficient information for a later operation to tell what\nbranch was refered to with the @{u} reference back when \"checkout\"\nwas run.  I suspect that checking out @{4}, @{1.day.ago}, etc. may\nshare the same issue.\n\n\"git checkout @{-1}\" aka \"git checkout -\" may not have any problem\nafter such a checkout, as it only uses \"moving from\" side of the\nreflog entry.\n\n"}]}