{"thread":{"id":"45472","subject":"How do I copy a branch & its config to a new name?","startedAt":"2017-03-22T22:47:02Z","lastAt":"2017-03-24T21:17:15Z","messageCount":7,"participants":["Ævar Arnfjörð Bjarmason","Jeff King","Jonathan Nieder"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"315017","messageId":"CACBZZX7NDa5o1xSu4HgZ4=kG3mx3U6ja7f3E4yAkFOHDsLdMjA@mail.gmail.com","threadId":"45472","inReplyTo":null,"subject":"How do I copy a branch & its config to a new name?","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-03-22T22:46:14Z","receivedAt":"2017-03-22T22:47:02Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"When I start work on a new series I have e.g. avar/various-tag-2 with\nits upstream info set to origin/master.\n\nWhen I start avar/various-tag-3 just:\n\n    git checkcout -b avar/various-tag-3 avar/various-tag-2\n\nI don't get the correct tracking info. As far as I can tell there's no\nway to do this in a single command, either you do:\n\n    git checkout -b avar/various-tag-4 avar/various-tag-3 && git\nbranch --set-upstream-to origin/master\n\nOr:\n\n    git checkout -b avar/various-tag-3 -t origin/master && git reset\n--hard avar/various-tag-2\n\nBut both of these are really just a limited special case for what I'd\nreally like, which is given branch \"foo\", copy it and all its\nconfiguration to a new name \"bar\". I.e. both of the hacks above only\nset up the correct tracking info, but none of the other branch.*\nvariables I may have set.\n\nSo I have this relative horror-show as an alias:\n\n    $ git config alias.cp-branch\n    !f() { git checkout -b $2 $1 && for line in $(git config --list\n--file .git/config | grep \"^branch\\.$1\"); do key=$(echo $line | cut\n-d= -f1); newkey=$(echo $key | sed \"s!$1!$2!\"); value=$(echo $line |\ncut -d= -f2); git config $newkey $value; done; }; f\n\nTurned into a more readable tiny shell-script you can call git-cp-branch that's:\n\n    #!/bin/sh -e\n    git checkout -b $2 $1\n    for line in $(git config --list --file $(git rev-parse\n--git-dir)/config | grep \"^branch\\.$1\");\n    do\n        key=$(echo $line | cut -d= -f1)\n        newkey=$(echo $key | sed \"s!$1!$2!\")\n        value=$(echo $line | cut -d= -f2)\n        git config $newkey $value\n    done\n\nI couldn't find any previous list discussion about this, but if not I\nthink something like:\n\n    git [checkout|branch] --copy src dest\n\nWould make sense as an interface for this.\n"},{"id":"315020","messageId":"20170322225800.3h2mnnmmj2hbo36c@sigill.intra.peff.net","threadId":"45472","inReplyTo":"CACBZZX7NDa5o1xSu4HgZ4=kG3mx3U6ja7f3E4yAkFOHDsLdMjA@mail.gmail.com","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-22T22:58:00Z","receivedAt":"2017-03-22T22:58:12Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Mar 22, 2017 at 11:46:14PM +0100, Ævar Arnfjörð Bjarmason wrote:\n\n> But both of these are really just a limited special case for what I'd\n> really like, which is given branch \"foo\", copy it and all its\n> configuration to a new name \"bar\". I.e. both of the hacks above only\n> set up the correct tracking info, but none of the other branch.*\n> variables I may have set.\n\nI thought that's what \"git branch -m\" was for, though I don't know how\nthorough it is about finding config that refers _to_ your branch (I know\nit handles config _for_ your branch).\n\nThere might be room for improvement there.\n\n-Peff\n"},{"id":"315024","messageId":"CACBZZX47euh-K8-DUjDKr6i=g8MzmivOzA5fFTyPQjZhkzM2uA@mail.gmail.com","threadId":"45472","inReplyTo":"20170322225800.3h2mnnmmj2hbo36c@sigill.intra.peff.net","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-03-22T23:54:48Z","receivedAt":"2017-03-22T23:55:15Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Wed, Mar 22, 2017 at 11:58 PM, Jeff King <peff@peff.net> wrote:\n> On Wed, Mar 22, 2017 at 11:46:14PM +0100, Ævar Arnfjörð Bjarmason wrote:\n>\n>> But both of these are really just a limited special case for what I'd\n>> really like, which is given branch \"foo\", copy it and all its\n>> configuration to a new name \"bar\". I.e. both of the hacks above only\n>> set up the correct tracking info, but none of the other branch.*\n>> variables I may have set.\n>\n> I thought that's what \"git branch -m\" was for, though I don't know how\n> thorough it is about finding config that refers _to_ your branch (I know\n> it handles config _for_ your branch).\n>\n> There might be room for improvement there.\n\n-m can only rename \"foo\" -> \"bar\", but I'd like to end up with a new\n\"bar\" with the same config as \"foo\"\n"},{"id":"315025","messageId":"20170322235754.wver7y6hpfyotwzg@sigill.intra.peff.net","threadId":"45472","inReplyTo":"CACBZZX47euh-K8-DUjDKr6i=g8MzmivOzA5fFTyPQjZhkzM2uA@mail.gmail.com","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-22T23:57:55Z","receivedAt":"2017-03-22T23:58:09Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 23, 2017 at 12:54:48AM +0100, Ævar Arnfjörð Bjarmason wrote:\n\n> On Wed, Mar 22, 2017 at 11:58 PM, Jeff King <peff@peff.net> wrote:\n> > On Wed, Mar 22, 2017 at 11:46:14PM +0100, Ævar Arnfjörð Bjarmason wrote:\n> >\n> >> But both of these are really just a limited special case for what I'd\n> >> really like, which is given branch \"foo\", copy it and all its\n> >> configuration to a new name \"bar\". I.e. both of the hacks above only\n> >> set up the correct tracking info, but none of the other branch.*\n> >> variables I may have set.\n> >\n> > I thought that's what \"git branch -m\" was for, though I don't know how\n> > thorough it is about finding config that refers _to_ your branch (I know\n> > it handles config _for_ your branch).\n> >\n> > There might be room for improvement there.\n> \n> -m can only rename \"foo\" -> \"bar\", but I'd like to end up with a new\n> \"bar\" with the same config as \"foo\"\n\nAh, I see. You really want \"copy\". In that case, yeah, I think you'd\nneed a new command. It might make sense for it to share implementation\nwith rename (and just do the \"add new config\" part, skipping the \"drop\nold config\" half).\n\n-Peff\n"},{"id":"315216","messageId":"20170324005249.GF20794@aiede.mtv.corp.google.com","threadId":"45472","inReplyTo":"CACBZZX7NDa5o1xSu4HgZ4=kG3mx3U6ja7f3E4yAkFOHDsLdMjA@mail.gmail.com","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2017-03-24T00:52:49Z","receivedAt":"2017-03-24T00:53:07Z","isPatch":false,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Hi,\n\nÆvar Arnfjörð Bjarmason wrote:\n\n> I couldn't find any previous list discussion about this, but if not I\n> think something like:\n>\n>     git [checkout|branch] --copy src dest\n>\n> Would make sense as an interface for this.\n\nSounds good to me. :)\n\nThanks,\nJonathan\n"},{"id":"315235","messageId":"CACBZZX6U0PAAK8vJxBW2VSzbtfttFUNTZhDGk=BzLxkaTn64gw@mail.gmail.com","threadId":"45472","inReplyTo":"20170324005249.GF20794@aiede.mtv.corp.google.com","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-03-24T11:10:49Z","receivedAt":"2017-03-24T11:11:19Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Fri, Mar 24, 2017 at 1:52 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Hi,\n>\n> Ęvar Arnfjörš Bjarmason wrote:\n>\n>> I couldn't find any previous list discussion about this, but if not I\n>> think something like:\n>>\n>>     git [checkout|branch] --copy src dest\n>>\n>> Would make sense as an interface for this.\n>\n> Sounds good to me. :)\n\nActually this is a bit confusing, but I think reversing the arguments\nmakes sense, i.e.:\n\n    git branch -c dest [src]\n\nAnd similarly:\n\n    git checkout -c dest [<src>]\n\nThis is confusing in that it reverses the arguments, but more useful\nin that it allows you to omit src like the other invocations of these\ncommands, and you can e.g. do:\n\n    git branch -c avar/topic-2\n\nWhile on avar/topic to start working on avar/topic-2, which would copy\navar/topic's state & config.\n\nI'll put this on my TODO list, but unless someone comes up with a\ncompelling argument against the above I plan to make the interface\nlook like that.\n"},{"id":"315330","messageId":"20170324211701.vvmp6n4c77o2dvbz@sigill.intra.peff.net","threadId":"45472","inReplyTo":"CACBZZX6U0PAAK8vJxBW2VSzbtfttFUNTZhDGk=BzLxkaTn64gw@mail.gmail.com","subject":"Re: How do I copy a branch & its config to a new name?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-24T21:17:01Z","receivedAt":"2017-03-24T21:17:15Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Mar 24, 2017 at 12:10:49PM +0100, Ævar Arnfjörð Bjarmason wrote:\n\n> Actually this is a bit confusing, but I think reversing the arguments\n> makes sense, i.e.:\n> \n>     git branch -c dest [src]\n> \n> And similarly:\n> \n>     git checkout -c dest [<src>]\n> \n> This is confusing in that it reverses the arguments, but more useful\n> in that it allows you to omit src like the other invocations of these\n> commands, and you can e.g. do:\n> \n>     git branch -c avar/topic-2\n> \n> While on avar/topic to start working on avar/topic-2, which would copy\n> avar/topic's state & config.\n> \n> I'll put this on my TODO list, but unless someone comes up with a\n> compelling argument against the above I plan to make the interface\n> look like that.\n\nI think it probably should match \"git branch -m\", which uses:\n\n  git branch -c [<oldbranch>] <newbranch>\n\nThat's the closest operation we currently have, and it still allows\nomitting the src. It _is_ different than:\n\n  git branch <branchname> [<start-point>]\n\nI wondered if you could advertise \"-c\" not as a new \"copy mode\", but\nrather as an option for normal branch creation. Sort of a \"by the way,\ncopy the config\". But I don't think that's a great mental model.\n<start-point> doesn't have to be a branch at all.\n\n-Peff\n"}]}