{"thread":{"id":"41825","subject":"How to use @{-1} with push?","startedAt":"2016-03-25T14:40:40Z","lastAt":"2016-03-28T14:53:15Z","messageCount":6,"participants":["Robert Dailey","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"281815","messageId":"CAHd499AM-OzqiB1hOF=0BTesFxrxNj=+jr1wH6vpQXfgoXd8Ug@mail.gmail.com","threadId":"41825","inReplyTo":null,"subject":"How to use @{-1} with push?","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-03-25T14:40:40Z","receivedAt":"2016-03-25T14:40:40Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"I have an alias that I'm working on to do a push and delete of a topic branch:\n\n\n    # Push HEAD, delete branch local & remote\n    #\n    # $1 = remote name\n    # $2 = branch name\n    pushdel = \"!f() { : git push ; git push \\\"$1\\\" HEAD \\\":$2\\\" && git\nbranch -d \\\"$2\\\" ; }; f\"\n\n\nI use it after I merge a topic branch, like so:\n\n$ git pushdel origin my-topic-branch\n\nWhat would be nice is if I could do one of the two:\n\n$ git pushdel origin -\n$ git pushdel origin @{-1}\n\nBoth should refer to the last branch, but I know that these can't be\nused verbatim in a push command because it doesn't read it as a branch\nname normally like `git checkout` would. I'm not sure why, as I do not\nhave knowledge of the underlying mechanics of these commands.\n\nIs there a way I can use these \"relative\" branch shorthands in my push\ncommand? If not directly, perhaps there is a way I can update my alias\nto do an intermediate translation?\n\nThanks in advance.\n"},{"id":"281839","messageId":"xmqq8u164fjv.fsf@gitster.mtv.corp.google.com","threadId":"41825","inReplyTo":"CAHd499AM-OzqiB1hOF=0BTesFxrxNj=+jr1wH6vpQXfgoXd8Ug@mail.gmail.com","subject":"Re: How to use @{-1} with push?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-03-25T17:45:40Z","receivedAt":"2016-03-25T17:45:40Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robert Dailey <rcdailey.lists@gmail.com> writes:\n\n> Both should refer to the last branch, but I know that these can't be\n> used verbatim in a push command because it doesn't read it as a branch\n> name normally like `git checkout` would.\n\nYou can ask rev-parse to give you --symbolic-full-name, error out if\nit is empty (i.e. detached HEAD), and otherwise use the result, no?\n\n    $ git checkout next\n    $ git checkout master\n    $ git rev-parse --symbolic-full-name @{-1}\n    refs/heads/master\n    $ git checkout HEAD^0\n    $ git checkout master\n    $ git rev-parse --symbolic-full-name @{-1}\n    $ exit\n\nAnd\n\n    $ git push origin :refs/heads/master\n\nwould be the fully-spelled out way to remove that branch.\n"},{"id":"281847","messageId":"CAHd499B3Z58hj--Pa0uM36A2H3Xpmayrb+RiLeOBkmnu70yW1A@mail.gmail.com","threadId":"41825","inReplyTo":"xmqq8u164fjv.fsf@gitster.mtv.corp.google.com","subject":"Re: How to use @{-1} with push?","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-03-25T18:02:27Z","receivedAt":"2016-03-25T18:02:27Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"On Fri, Mar 25, 2016 at 12:45 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> You can ask rev-parse to give you --symbolic-full-name, error out if\n> it is empty (i.e. detached HEAD), and otherwise use the result, no?\n>\n>     $ git checkout next\n>     $ git checkout master\n>     $ git rev-parse --symbolic-full-name @{-1}\n>     refs/heads/master\n>     $ git checkout HEAD^0\n>     $ git checkout master\n>     $ git rev-parse --symbolic-full-name @{-1}\n>     $ exit\n>\n> And\n>\n>     $ git push origin :refs/heads/master\n>\n> would be the fully-spelled out way to remove that branch.\n\nThanks Junio, I figured there was a command to do that. I tried to do\nthis using '-' shorthand, but that didn't work. I guess because that's\nnot really a revision, but a special function of git checkout only.\n"},{"id":"281850","messageId":"CAHd499BCciG5Udd_sj550jPKg_ahZmWOi-zUOU-cr_wYmri=Rg@mail.gmail.com","threadId":"41825","inReplyTo":"CAHd499B3Z58hj--Pa0uM36A2H3Xpmayrb+RiLeOBkmnu70yW1A@mail.gmail.com","subject":"Re: How to use @{-1} with push?","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-03-25T18:11:36Z","receivedAt":"2016-03-25T18:11:36Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"On Fri, Mar 25, 2016 at 1:02 PM, Robert Dailey <rcdailey.lists@gmail.com> wrote:\n> On Fri, Mar 25, 2016 at 12:45 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>> You can ask rev-parse to give you --symbolic-full-name, error out if\n>> it is empty (i.e. detached HEAD), and otherwise use the result, no?\n>>\n>>     $ git checkout next\n>>     $ git checkout master\n>>     $ git rev-parse --symbolic-full-name @{-1}\n>>     refs/heads/master\n>>     $ git checkout HEAD^0\n>>     $ git checkout master\n>>     $ git rev-parse --symbolic-full-name @{-1}\n>>     $ exit\n>>\n>> And\n>>\n>>     $ git push origin :refs/heads/master\n>>\n>> would be the fully-spelled out way to remove that branch.\n>\n> Thanks Junio, I figured there was a command to do that. I tried to do\n> this using '-' shorthand, but that didn't work. I guess because that's\n> not really a revision, but a special function of git checkout only.\n\nSo the push works with the fully-qualified ref, but not git branch:\n\n$ git rev-parse --symbolic-full-name foo\nrefs/heads/foo\n\n$ git branch -d refs/heads/foo\nerror: branch 'refs/heads/foo' not found.\n\nAny reason for this? I'm using git 2.7.4 on windows\n"},{"id":"281867","messageId":"xmqqoaa22xmg.fsf@gitster.mtv.corp.google.com","threadId":"41825","inReplyTo":"CAHd499BCciG5Udd_sj550jPKg_ahZmWOi-zUOU-cr_wYmri=Rg@mail.gmail.com","subject":"Re: How to use @{-1} with push?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-03-25T18:58:15Z","receivedAt":"2016-03-25T18:58:15Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robert Dailey <rcdailey.lists@gmail.com> writes:\n\n> So the push works with the fully-qualified ref, but not git branch:\n\nI thought these are clear from their documentation.  \"push\" works on\nrefnames, \"branch\" works on branch names.  \"push\" takes an branch\nname as a short-hand and adds refs/heads/ when it makes sense, but\nbecause it does not make any sense for \"git branch\" to create a\n\"branch\" in a random place in refs/ (e.g. \"refs/tags/foo\" is not a\nbranch), it takes \"foo\" (i.e. the name of the branch, whose\nunderlying ref is \"refs/heads/foo\").\n\nSo\n\n\tref=$(git rev-parse --symbolic-full-name \"$2\") &&\n        case \"$ref\" in\n        '') echo >&2 \"No such thing $2\"; exit 1 ;;\n        refs/heads/*) ref=${ref#refs/heads/} ;;\n        *) echo >&2 \"That's not a branch $2\"; exit 2 ;;\n\tesac &&\n\tgit push \"$1\" \"refs/heads/$ref\" &&\n        git branch -D \"$ref\"\n\nor something?\n\n>\n> $ git rev-parse --symbolic-full-name foo\n> refs/heads/foo\n>\n> $ git branch -d refs/heads/foo\n> error: branch 'refs/heads/foo' not found.\n>\n> Any reason for this? I'm using git 2.7.4 on windows\n"},{"id":"281961","messageId":"CAHd499A-8iajchG7hUMKNnCN9riarU4G3S-HZxXusOdewwaVCg@mail.gmail.com","threadId":"41825","inReplyTo":"xmqqoaa22xmg.fsf@gitster.mtv.corp.google.com","subject":"Re: How to use @{-1} with push?","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-03-28T14:53:15Z","receivedAt":"2016-03-28T14:53:15Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"On Fri, Mar 25, 2016 at 1:58 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> I thought these are clear from their documentation.  \"push\" works on\n> refnames, \"branch\" works on branch names.  \"push\" takes an branch\n> name as a short-hand and adds refs/heads/ when it makes sense, but\n> because it does not make any sense for \"git branch\" to create a\n> \"branch\" in a random place in refs/ (e.g. \"refs/tags/foo\" is not a\n> branch), it takes \"foo\" (i.e. the name of the branch, whose\n> underlying ref is \"refs/heads/foo\").\n>\n> So\n>\n>         ref=$(git rev-parse --symbolic-full-name \"$2\") &&\n>         case \"$ref\" in\n>         '') echo >&2 \"No such thing $2\"; exit 1 ;;\n>         refs/heads/*) ref=${ref#refs/heads/} ;;\n>         *) echo >&2 \"That's not a branch $2\"; exit 2 ;;\n>         esac &&\n>         git push \"$1\" \"refs/heads/$ref\" &&\n>         git branch -D \"$ref\"\n>\n> or something?\n\nReading the git documentation feels a lot like reading the C++\nstandard. Not the best place to go to learn something. Some of the\nterminology is very detail-oriented. For example, until you explained\nthe differences between push & branch, I always thought \"ref\" and\n\"branch\" were interchangeable. But now it's clear to me that a branch\nis just a type of ref, but refs are not branches. Also as a Windows\ndeveloper, I am not as well-versed in bash scripting as I'd like to\nbe.\n\nSo thanks for your explanation, it is clear now.\n"}]}