{"thread":{"id":"40501","subject":"Branch information (git branch/status) inconsistent when in detached HEAD state","startedAt":"2015-10-06T13:03:51Z","lastAt":"2015-10-07T14:41:43Z","messageCount":4,"participants":["Stijn De Ruyck","Michael J Gruber"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"271215","messageId":"247ABEF3B84FB7499C074A0FE8058694153CB176@ONWATER51M.ad.onsemi.com","threadId":"40501","inReplyTo":null,"subject":"Branch information (git branch/status) inconsistent when in detached HEAD state","fromName":"Stijn De Ruyck","fromEmail":"stijn.deruyck@onsemi.com","sentAt":"2015-10-06T13:03:51Z","receivedAt":"2015-10-06T13:03:51Z","isPatch":false,"sender":{"key":"stijn.deruyck@onsemi.com","avatar":null},"body":"Hello,\n\nConsider a repository with a develop branch tracking origin/develop and where HEAD = 545a36f = develop = origin/develop.\nTested with Git 2.4.3 and 1.8.3.4 on Linux.\n\n1) git checkout develop\n2) git branch | head -1 (or git status)\n* develop\n3) git checkout origin/develop\nNote: checking out 'origin/develop'.\nYou are in 'detached HEAD' state.\n4) git branch | head -1\n* (HEAD detached at origin/develop)\n5) git checkout 545a36f\n6) git branch | head -1\n* (HEAD detached at origin/develop)\n7) git checkout develop\n8) git branch | head -1 (or git status)\n* develop\n9) git checkout 545a36f\nNote: checking out '545a36f'.\nYou are in 'detached HEAD' state.\n10) git branch | head -1 (or git status)\n* (HEAD detached at 545a36f)\n11) git checkout origin/develop\n12) git branch | head -1 (or git status)\n* (HEAD detached at 545a36f)\n\nAs you can see, checking out a revision by SHA1 after checking out a local branch tells you you are detached at that SHA1. Ok.\nBut, checking out a revision by SHA1 after checking out a remote tracking branch tells you you are detached at that remote tracking branch.\nAnd checking out a remote tracking branch after checking out a revision by SHA1 tells you you are detached at that SHA1.\n\nThis is confusing. Is this by design?\nFor a script I am writing, it would be great if the output would be consistent. Checkout origin/develop should always say \"detached at origin/develop\". Checkout 545a36f should always say \"detached at 545a36f\".\n\nIt looks like the detached head information is only updated when a detached head state is entered and not anymore when a checkout occurs \"within\" a detached head state.\n\nBest regards,\n\nStijn\n"},{"id":"271255","messageId":"5614E0C0.7060805@drmicha.warpmail.net","threadId":"40501","inReplyTo":"247ABEF3B84FB7499C074A0FE8058694153CB176@ONWATER51M.ad.onsemi.com","subject":"Re: Branch information (git branch/status) inconsistent when in detached HEAD state","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2015-10-07T09:07:12Z","receivedAt":"2015-10-07T09:07:12Z","isPatch":false,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"Stijn De Ruyck venit, vidit, dixit 06.10.2015 15:03:\n> Hello,\n> \n> Consider a repository with a develop branch tracking origin/develop and where HEAD = 545a36f = develop = origin/develop.\n> Tested with Git 2.4.3 and 1.8.3.4 on Linux.\n> \n> 1) git checkout develop\n> 2) git branch | head -1 (or git status)\n> * develop\n> 3) git checkout origin/develop\n> Note: checking out 'origin/develop'.\n> You are in 'detached HEAD' state.\n> 4) git branch | head -1\n> * (HEAD detached at origin/develop)\n> 5) git checkout 545a36f\n> 6) git branch | head -1\n> * (HEAD detached at origin/develop)\n> 7) git checkout develop\n> 8) git branch | head -1 (or git status)\n> * develop\n> 9) git checkout 545a36f\n> Note: checking out '545a36f'.\n> You are in 'detached HEAD' state.\n> 10) git branch | head -1 (or git status)\n> * (HEAD detached at 545a36f)\n> 11) git checkout origin/develop\n> 12) git branch | head -1 (or git status)\n> * (HEAD detached at 545a36f)\n> \n> As you can see, checking out a revision by SHA1 after checking out a local branch tells you you are detached at that SHA1. Ok.\n> But, checking out a revision by SHA1 after checking out a remote tracking branch tells you you are detached at that remote tracking branch.\n> And checking out a remote tracking branch after checking out a revision by SHA1 tells you you are detached at that SHA1.\n> \n> This is confusing. Is this by design?\n> For a script I am writing, it would be great if the output would be consistent. Checkout origin/develop should always say \"detached at origin/develop\". Checkout 545a36f should always say \"detached at 545a36f\".\n> \n> It looks like the detached head information is only updated when a detached head state is entered and not anymore when a checkout occurs \"within\" a detached head state.\n> \n> Best regards,\n\nDoes 545a36f happen to be the tip (value) of origin/develop?\n\nFor a sha1 different from the tip, I can't reproduce the effect that you\ndescribe. There have been some changes in related code recently, but I\ndon't think they should affect this scenario.\n\nMichael\n"},{"id":"271256","messageId":"247ABEF3B84FB7499C074A0FE8058694153CB1BE@ONWATER51M.ad.onsemi.com","threadId":"40501","inReplyTo":"5614E0C0.7060805@drmicha.warpmail.net","subject":"RE: Branch information (git branch/status) inconsistent when in detached HEAD state","fromName":"Stijn De Ruyck","fromEmail":"stijn.deruyck@onsemi.com","sentAt":"2015-10-07T09:57:31Z","receivedAt":"2015-10-07T09:57:31Z","isPatch":false,"sender":{"key":"stijn.deruyck@onsemi.com","avatar":null},"body":"Yes it is the tip. \nSo all I'm doing is checking out the exact same commit (the tip of a branch), but in different ways and in different sequences. The result on disk is the same, but the issue is git branch/status doesn't always reflect what I just did...\n\nIf I do git checkout origin/develop, git branch might still say \"detached at 545a36f\", which, while true, feels wrong. How can I know afterwards what branch I actually tried to check out? \"Detached at origin/develop\" would make that clear.\n(I use this information in a build script to embed version information (including the branch being built) into the binary.)\n\nBest regards,\n\nStijn De Ruyck\n\n\n-----Original Message-----\nFrom: Michael J Gruber [mailto:git@drmicha.warpmail.net] \nSent: Wednesday, October 07, 2015 11:07 AM\nTo: Stijn De Ruyck; git@vger.kernel.org\nSubject: Re: Branch information (git branch/status) inconsistent when in detached HEAD state\n\nStijn De Ruyck venit, vidit, dixit 06.10.2015 15:03:\n> Hello,\n> \n> Consider a repository with a develop branch tracking origin/develop and where HEAD = 545a36f = develop = origin/develop.\n> Tested with Git 2.4.3 and 1.8.3.4 on Linux.\n> \n> 1) git checkout develop\n> 2) git branch | head -1 (or git status)\n> * develop\n> 3) git checkout origin/develop\n> Note: checking out 'origin/develop'.\n> You are in 'detached HEAD' state.\n> 4) git branch | head -1\n> * (HEAD detached at origin/develop)\n> 5) git checkout 545a36f\n> 6) git branch | head -1\n> * (HEAD detached at origin/develop)\n> 7) git checkout develop\n> 8) git branch | head -1 (or git status)\n> * develop\n> 9) git checkout 545a36f\n> Note: checking out '545a36f'.\n> You are in 'detached HEAD' state.\n> 10) git branch | head -1 (or git status)\n> * (HEAD detached at 545a36f)\n> 11) git checkout origin/develop\n> 12) git branch | head -1 (or git status)\n> * (HEAD detached at 545a36f)\n> \n> As you can see, checking out a revision by SHA1 after checking out a local branch tells you you are detached at that SHA1. Ok.\n> But, checking out a revision by SHA1 after checking out a remote tracking branch tells you you are detached at that remote tracking branch.\n> And checking out a remote tracking branch after checking out a revision by SHA1 tells you you are detached at that SHA1.\n> \n> This is confusing. Is this by design?\n> For a script I am writing, it would be great if the output would be consistent. Checkout origin/develop should always say \"detached at origin/develop\". Checkout 545a36f should always say \"detached at 545a36f\".\n> \n> It looks like the detached head information is only updated when a detached head state is entered and not anymore when a checkout occurs \"within\" a detached head state.\n> \n> Best regards,\n\nDoes 545a36f happen to be the tip (value) of origin/develop?\n\nFor a sha1 different from the tip, I can't reproduce the effect that you describe. There have been some changes in related code recently, but I don't think they should affect this scenario.\n\nMichael\n"},{"id":"271257","messageId":"56152F27.5070907@drmicha.warpmail.net","threadId":"40501","inReplyTo":"247ABEF3B84FB7499C074A0FE8058694153CB1BE@ONWATER51M.ad.onsemi.com","subject":"Re: Branch information (git branch/status) inconsistent when in detached HEAD state","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2015-10-07T14:41:43Z","receivedAt":"2015-10-07T14:41:43Z","isPatch":false,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"[Please make sure to bottom-post or reply-inline on the git-ml.]\nStijn De Ruyck venit, vidit, dixit 07.10.2015 11:57:\n> Yes it is the tip. \n> So all I'm doing is checking out the exact same commit (the tip of a branch), but in different ways and in different sequences. The result on disk is the same, but the issue is git branch/status doesn't always reflect what I just did...\n> \n> If I do git checkout origin/develop, git branch might still say \"detached at 545a36f\", which, while true, feels wrong. How can I know afterwards what branch I actually tried to check out? \"Detached at origin/develop\" would make that clear.\n> (I use this information in a build script to embed version information (including the branch being built) into the binary.)\n\nWell, you think you do, but you don't. That is: You're not 'moving'; the\nvalue of HEAD stays the same, and if you check the reflog, you see that\nthe second \"checkout\" (which in fact is a no-op) is not recorded in the\nreflog at all - because the HEAD ref doesn't change at all, and the HEAD\nreflog is all about changes to the HEAD ref.\n\nIn that sense everything is perfectly correct, in the same way as a fast\nforward merge is not \"recorded\" as a merge.\n\nThe way you think about the checkout may change, of course. I'm not sure\nwhether \"no-op\" entries in the reflog would do any harm or whether we\nshould create them. (And I'm not sure whether the HEAD reflog is the\nright tool for your purpose.)\n\nMichael\n\n> \n> -----Original Message-----\n> From: Michael J Gruber [mailto:git@drmicha.warpmail.net] \n> Sent: Wednesday, October 07, 2015 11:07 AM\n> To: Stijn De Ruyck; git@vger.kernel.org\n> Subject: Re: Branch information (git branch/status) inconsistent when in detached HEAD state\n> \n> Stijn De Ruyck venit, vidit, dixit 06.10.2015 15:03:\n>> Hello,\n>>\n>> Consider a repository with a develop branch tracking origin/develop and where HEAD = 545a36f = develop = origin/develop.\n>> Tested with Git 2.4.3 and 1.8.3.4 on Linux.\n>>\n>> 1) git checkout develop\n>> 2) git branch | head -1 (or git status)\n>> * develop\n>> 3) git checkout origin/develop\n>> Note: checking out 'origin/develop'.\n>> You are in 'detached HEAD' state.\n>> 4) git branch | head -1\n>> * (HEAD detached at origin/develop)\n>> 5) git checkout 545a36f\n>> 6) git branch | head -1\n>> * (HEAD detached at origin/develop)\n>> 7) git checkout develop\n>> 8) git branch | head -1 (or git status)\n>> * develop\n>> 9) git checkout 545a36f\n>> Note: checking out '545a36f'.\n>> You are in 'detached HEAD' state.\n>> 10) git branch | head -1 (or git status)\n>> * (HEAD detached at 545a36f)\n>> 11) git checkout origin/develop\n>> 12) git branch | head -1 (or git status)\n>> * (HEAD detached at 545a36f)\n>>\n>> As you can see, checking out a revision by SHA1 after checking out a local branch tells you you are detached at that SHA1. Ok.\n>> But, checking out a revision by SHA1 after checking out a remote tracking branch tells you you are detached at that remote tracking branch.\n>> And checking out a remote tracking branch after checking out a revision by SHA1 tells you you are detached at that SHA1.\n>>\n>> This is confusing. Is this by design?\n>> For a script I am writing, it would be great if the output would be consistent. Checkout origin/develop should always say \"detached at origin/develop\". Checkout 545a36f should always say \"detached at 545a36f\".\n>>\n>> It looks like the detached head information is only updated when a detached head state is entered and not anymore when a checkout occurs \"within\" a detached head state.\n>>\n>> Best regards,\n> \n> Does 545a36f happen to be the tip (value) of origin/develop?\n> \n> For a sha1 different from the tip, I can't reproduce the effect that you describe. There have been some changes in related code recently, but I don't think they should affect this scenario.\n> \n> Michael\n> \n> \n"}]}