git/list[1] front-page[2] threads[3] people[4] search[5] about
 

How do I copy a branch & its config to a new name?

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Mar 22, 2017, 22:46 UTC
Message-ID
<CACBZZX7NDa5o1xSu4HgZ4=kG3mx3U6ja7f3E4yAkFOHDsLdMjA@mail.gmail.com>

When I start work on a new series I have e.g. avar/various-tag-2 with its upstream info set to origin/master.

When I start avar/various-tag-3 just:
    git checkcout -b avar/various-tag-3 avar/various-tag-2

I don't get the correct tracking info. As far as I can tell there's no way to do this in a single command, either you do:

    git checkout -b avar/various-tag-4 avar/various-tag-3 && git
branch --set-upstream-to origin/master
Or:
    git checkout -b avar/various-tag-3 -t origin/master && git reset
--hard avar/various-tag-2

But both of these are really just a limited special case for what I'd really like, which is given branch "foo", copy it and all its configuration to a new name "bar". I.e. both of the hacks above only set up the correct tracking info, but none of the other branch.* variables I may have set.

So I have this relative horror-show as an alias:
    $ git config alias.cp-branch
    !f() { git checkout -b $2 $1 && for line in $(git config --list
--file .git/config | grep "^branch\.$1"); do key=$(echo $line | cut
-d= -f1); newkey=$(echo $key | sed "s!$1!$2!"); value=$(echo $line |
cut -d= -f2); git config $newkey $value; done; }; f
Turned into a more readable tiny shell-script you can call git-cp-branch that's:
    #!/bin/sh -e
    git checkout -b $2 $1
    for line in $(git config --list --file $(git rev-parse
--git-dir)/config | grep "^branch\.$1");
    do
        key=$(echo $line | cut -d= -f1)
        newkey=$(echo $key | sed "s!$1!$2!")
        value=$(echo $line | cut -d= -f2)
        git config $newkey $value
    done

I couldn't find any previous list discussion about this, but if not I think something like:

    git [checkout|branch] --copy src dest
Would make sense as an interface for this.
Next: Jeff King
Message 1 of 7 in “How do I copy a branch & its config to a new name?”
  1. Ævar Arnfjörð BjarmasonMar 22, 2017
  2. Jeff KingMar 22, 2017
  3. Ævar Arnfjörð BjarmasonMar 22, 2017
  4. Jeff KingMar 22, 2017
  5. Jonathan NiederMar 24, 2017
  6. Ævar Arnfjörð BjarmasonMar 24, 2017
  7. Jeff KingMar 24, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.