Is there a simple command to convert a branch to a tag?

后端 未结 3 1136

I am about to complete a tedious process of converting \"dumb snapshots\" to git. This process has been going very well (thanks to this rename process), but now I realized that

相关标签:
3条回答
  • 2021-02-02 09:28

    The answers given are basically correct.

    As tags and branches are just names for objects, there is a simpler way without touching current work area:

    git tag <name_for_tag> refs/heads/<branch_name> # or just git tag <name_for_tag> <branch_name>
    git branch -d <branch_name>
    

    Or even do it to remote server without touching local repository at all:

    git push origin origin/<branch_name>:refs/tags/<tag_name>
    git push origin :refs/heads/<branch_name>
    
    0 讨论(0)
  • 2021-02-02 09:37

    Per Andy's answer, I've made an alias that can also be used for the same thing:

    [alias]
    branch2tag = "!sh -c 'set -e;git tag $1 refs/heads/$1;git branch -D $1' -"
    

    Usage

    If you want to convert branch bug-2483 to a tag (while your main branch is master) write:

    git branch2tag bug-2483 master
    

    UPDATE 1

    Changed to reflect the solution proposed by kauppi.

    0 讨论(0)
  • 2021-02-02 09:39

    Was there separate development on these branches? (the post you linked to, doesn't appear to have development on those branches) If there was no development, you could:

    1. Checkout the branch git checkout branchName.
    2. Tag it with git tag tagName.
    3. Switch back to master git checkout master.
    4. Finally, delete original branch with git branch branchName -d.

    This can also be done if there was development on the branch, but you will need to use -D instead of -d. I'm not a git pro though, so not sure if that is an "acceptable" way to leave a branch.

    0 讨论(0)
提交回复
热议问题