{"thread":{"id":"39943","subject":"git branch command is incompatible with bash","startedAt":"2015-07-27T12:12:54Z","lastAt":"2015-07-29T00:23:33Z","messageCount":9,"participants":["Anatol Rudolph","Johannes Sixt","Junio C Hamano","Jakub Narębski","Jeff King","Scott Schmit"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"266825","messageId":"20150727121253.GC17338@2vizcon.com","threadId":"39943","inReplyTo":null,"subject":"git branch command is incompatible with bash","fromName":"Anatol Rudolph","fromEmail":"a.rudolph@2vizcon.com","sentAt":"2015-07-27T12:12:54Z","receivedAt":"2015-07-27T12:12:54Z","isPatch":false,"sender":{"key":"a.rudolph@2vizcon.com","avatar":null},"body":"Hello!\n\nI hope posting this to this mailing list is okay, this is the first ever\nmail that I submit to a technical mailing list. \n\nWhen using the git branch command, git uses a '*' to denote the current\nbranch. Therefore, in bash this:\n\n\t$ branchName=$(git branch -q)\n\t$ echo $branchName\n\nproduces a directory listing, because the '*' is interpreded by the\nshell. \n\nWhile an (unwieldly) workaround exists:\n\n\t$ branchName=$(git symbolic-ref -q HEAD)                                                                            \n\t$ branchName=${branch##refs/heads/}\n\nit would still be nice, if there were a --current flag, that returned\nonly the current branch name, omitting the star:\n\n\t$ branchName=$(git branch --current -q)\n\t$ echo $branchName\n\tmaster\n"},{"id":"266862","messageId":"55B69E68.90306@kdbg.org","threadId":"39943","inReplyTo":"20150727121253.GC17338@2vizcon.com","subject":"Re: git branch command is incompatible with bash","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2015-07-27T21:11:04Z","receivedAt":"2015-07-27T21:11:04Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 27.07.2015 um 14:12 schrieb Anatol Rudolph:\n> When using the git branch command, git uses a '*' to denote the current\n> branch. Therefore, in bash this:\n>\n> \t$ branchName=$(git branch -q)\n> \t$ echo $branchName\n>\n> produces a directory listing, because the '*' is interpreded by the\n> shell.\n\nOf course. You would write the last line as\n\n   echo \"$branchName\"\n\nThese are shell fundamentals.\n\n> While an (unwieldly) workaround exists:\n>\n> \t$ branchName=$(git symbolic-ref -q HEAD)\n> \t$ branchName=${branch##refs/heads/}\n\nIf you want to do that in a script, this is not a work-around, but it is \nhow you should do it. But you may want to use option --short to save the \nsecond line.\n\n> it would still be nice, if there were a --current flag, that returned\n> only the current branch name, omitting the star:\n>\n> \t$ branchName=$(git branch --current -q)\n> \t$ echo $branchName\n> \tmaster\n\nTry\n\n   branchName=$(git rev-parse --abbrev-ref HEAD)\n\n-- Hannes\n"},{"id":"266868","messageId":"xmqqh9opgtjz.fsf@gitster.dls.corp.google.com","threadId":"39943","inReplyTo":"55B69E68.90306@kdbg.org","subject":"Re: git branch command is incompatible with bash","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-07-27T21:49:20Z","receivedAt":"2015-07-27T21:49:20Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Sixt <j6t@kdbg.org> writes:\n\n> Try\n>\n>   branchName=$(git rev-parse --abbrev-ref HEAD)\n\nHmm, interesting.\n\n    $ git checkout --orphan notyet\n    $ git rev-parse --abbrev-ref HEAD\n    $ git symbolic-ref --short HEAD\n"},{"id":"266904","messageId":"55B72F09.3030000@kdbg.org","threadId":"39943","inReplyTo":"xmqqh9opgtjz.fsf@gitster.dls.corp.google.com","subject":"Re: git branch command is incompatible with bash","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2015-07-28T07:28:09Z","receivedAt":"2015-07-28T07:28:09Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 27.07.2015 um 23:49 schrieb Junio C Hamano:\n> Johannes Sixt <j6t@kdbg.org> writes:\n>\n>> Try\n>>\n>>    branchName=$(git rev-parse --abbrev-ref HEAD)\n>\n> Hmm, interesting.\n>\n>      $ git checkout --orphan notyet\n>      $ git rev-parse --abbrev-ref HEAD\n>      $ git symbolic-ref --short HEAD\n>\n\nPlease don't scare newcomers with these corner cases ;-)\n\nI see this:\n\n$ git rev-parse --abbrev-ref HEAD\nHEAD\nfatal: ambiguous argument 'HEAD': unknown revision or path not in the \nworking tree.\nUse '--' to separate paths from revisions, like this:\n'git <command> [<revision>...] -- [<file>...]'\n$ git symbolic-ref --short HEAD\nnotyet\n\nAre you trying to say that the result of 'rev-parse --abbrev-ref HEAD' \nis suboptimal and that of 'symbolic-ref --short HEAD' is OK?\n\n-- Hannes\n"},{"id":"266917","messageId":"55B752E0.7020901@gmail.com","threadId":"39943","inReplyTo":"55B72F09.3030000@kdbg.org","subject":"Re: git branch command is incompatible with bash","fromName":"Jakub Narębski","fromEmail":"jnareb@gmail.com","sentAt":"2015-07-28T10:01:04Z","receivedAt":"2015-07-28T10:01:04Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"W dniu 2015-07-28 o 09:28, Johannes Sixt pisze:\n> Am 27.07.2015 um 23:49 schrieb Junio C Hamano:\n>> Johannes Sixt <j6t@kdbg.org> writes:\n>>\n>>> Try\n>>>\n>>>    branchName=$(git rev-parse --abbrev-ref HEAD)\n>>\n>> Hmm, interesting.\n>>\n>>      $ git checkout --orphan notyet\n>>      $ git rev-parse --abbrev-ref HEAD\n>>      $ git symbolic-ref --short HEAD\n>>\n>\n> Please don't scare newcomers with these corner cases ;-)\n\n:-P\n\nYet another corner case:\n\n   $ git checkout origin/master # or v1.0.0\n   $ git rev-parse --abbrev-ref HEAD\n   $ git symbolic-ref --short HEAD\n\n> I see this:\n>\n> $ git rev-parse --abbrev-ref HEAD\n> HEAD\n> fatal: ambiguous argument 'HEAD': unknown revision or path not in the\n> working tree.\n> Use '--' to separate paths from revisions, like this:\n> 'git <command> [<revision>...] -- [<file>...]'\n\nErrr... this error message is errorneous (well, at least somewhat\nmisleading). Git should know that HEAD is not a path, and that\n--abbrev-ref doe not need no revision.\n\n> $ git symbolic-ref --short HEAD\n> notyet\n>\n> Are you trying to say that the result of 'rev-parse --abbrev-ref HEAD'\n> is suboptimal and that of 'symbolic-ref --short HEAD' is OK?\n\n-- \nJakub Narębski\n"},{"id":"266927","messageId":"xmqq4mkogvb7.fsf@gitster.dls.corp.google.com","threadId":"39943","inReplyTo":"55B72F09.3030000@kdbg.org","subject":"Re: git branch command is incompatible with bash","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-07-28T15:23:40Z","receivedAt":"2015-07-28T15:23:40Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Sixt <j6t@kdbg.org> writes:\n\n> Are you trying to say that the result of 'rev-parse --abbrev-ref HEAD'\n> is suboptimal and that of 'symbolic-ref --short HEAD' is OK?\n\nMy \"Interesting\" was primarily about that I wasn't aware of the\n\"--abbrev-ref\" option.\n\nYes, I am sure some time ago I accepted a patch to add it, but I\nsimply do not see the point, especially because the \"--short\" option\nto symbolic-ref feels much more superiour.  \"What branch am I on?\"\nis about symbolic refs, rev-parse is about revisions.\n\nI can see that \"symbolic-ref --short\" is much newer than the other\none, so addition of \"--abbrev-ref\" to \"rev-parse\" may have been a\nmistake made while being desperate (i.e. not having a way to do so\nwith plumbing, we wanted \"some\" way to do so and chose poorly).\n"},{"id":"266936","messageId":"20150728162516.GA28031@peff.net","threadId":"39943","inReplyTo":"xmqq4mkogvb7.fsf@gitster.dls.corp.google.com","subject":"Re: git branch command is incompatible with bash","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-07-28T16:25:17Z","receivedAt":"2015-07-28T16:25:17Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Jul 28, 2015 at 08:23:40AM -0700, Junio C Hamano wrote:\n\n> I can see that \"symbolic-ref --short\" is much newer than the other\n> one, so addition of \"--abbrev-ref\" to \"rev-parse\" may have been a\n> mistake made while being desperate (i.e. not having a way to do so\n> with plumbing, we wanted \"some\" way to do so and chose poorly).\n\nI think --abbrev-ref can handle much more than just shortening symrefs,\nthough:\n\nE.g. resolving other symbolic names:\n\n  $ git rev-parse --abbrev-ref @{u}\n  origin/master\n\nOr resolving any arbitrary name you happen to have:\n\n  $ git rev-parse --abbrev-ref refs/heads/master\n  master\n\nEven ones that aren't fully qualified (which would be tough to do with\nsimple pattern substitution):\n\n  $ git rev-parse --abbrev-ref remotes/origin/master\n  origin/master\n\nOr handling ambiguities during abbreviation:\n\n  $ git tag master\n  $ git rev-parse --abbrev-ref HEAD\n  heads/master\n\n-Peff\n"},{"id":"266947","messageId":"55B7BB92.30006@kdbg.org","threadId":"39943","inReplyTo":"xmqq4mkogvb7.fsf@gitster.dls.corp.google.com","subject":"Re: git branch command is incompatible with bash","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2015-07-28T17:27:46Z","receivedAt":"2015-07-28T17:27:46Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 28.07.2015 um 17:23 schrieb Junio C Hamano:\n> Johannes Sixt <j6t@kdbg.org> writes:\n>\n>> Are you trying to say that the result of 'rev-parse --abbrev-ref HEAD'\n>> is suboptimal and that of 'symbolic-ref --short HEAD' is OK?\n>\n> My \"Interesting\" was primarily about that I wasn't aware of the\n> \"--abbrev-ref\" option.\n>\n> Yes, I am sure some time ago I accepted a patch to add it, but I\n> simply do not see the point, especially because the \"--short\" option\n> to symbolic-ref feels much more superiour.  \"What branch am I on?\"\n> is about symbolic refs, rev-parse is about revisions.\n>\n> I can see that \"symbolic-ref --short\" is much newer than the other\n> one, so addition of \"--abbrev-ref\" to \"rev-parse\" may have been a\n> mistake made while being desperate (i.e. not having a way to do so\n> with plumbing, we wanted \"some\" way to do so and chose poorly).\n\nHeh. Originially, I was about to suggest\n\n    git symbolic-ref -q --short HEAD || git rev-parse --abbrev HEAD\n\nin order to handle the detached HEAD case by printing the abbreviated \ncommit name, only to learn that this doesn't do what I expected. Looking \nat the man page of rev-parse, I discovered --abbrev-ref. And learned \nthat I actually should have used --short instead of --abbrev in the \nabove rev-parse command.\n\nConfusing...\n\n-- Hannes\n"},{"id":"267018","messageId":"20150729002333.GB23501@odin.ulthar.us","threadId":"39943","inReplyTo":"xmqq4mkogvb7.fsf@gitster.dls.corp.google.com","subject":"Re: git branch command is incompatible with bash","fromName":"Scott Schmit","fromEmail":"i.grok@comcast.net","sentAt":"2015-07-29T00:23:33Z","receivedAt":"2015-07-29T00:23:33Z","isPatch":false,"sender":{"key":"i.grok@comcast.net","avatar":null},"body":"On Tue, Jul 28, 2015 at 08:23:40AM -0700, Junio C Hamano wrote:\n> Johannes Sixt <j6t@kdbg.org> writes:\n> > Are you trying to say that the result of 'rev-parse --abbrev-ref HEAD'\n> > is suboptimal and that of 'symbolic-ref --short HEAD' is OK?\n> \n> My \"Interesting\" was primarily about that I wasn't aware of the\n> \"--abbrev-ref\" option.\n> \n> Yes, I am sure some time ago I accepted a patch to add it, but I\n> simply do not see the point, especially because the \"--short\" option\n> to symbolic-ref feels much more superiour.  \"What branch am I on?\"\n> is about symbolic refs, rev-parse is about revisions.\n\nSometimes my question is \"what branch am I on?\" -- in which case\nsymbolic-ref is adequate.\n\nOther times, my question is \"where am I/what did I check out?\" which is\nusually a branch, sometimes a tag, and sometimes a commit hash.  We\ndon't have a one-stop-shop command to answer that question in all cases,\nwhich is unfortunate.\n\ngit status answers, but that's not plumbing.  git-prompt manages to do a\nfairly good job, but the logic is by no means straightforward.\n\n-- \nScott Schmit\n"}]}