【问题标题】:Change naming convention of tags inside a git repository?更改 git 存储库中标签的命名约定?
【发布时间】:2014-08-06 08:23:59
【问题描述】:

我有一个 git 存储库,其中包含许多格式为“v1.2.3-rc-4”的标签,我想将其自动重命名为“1.2.3-rc4”。标签存在于本地和远程存储库中。

我应该澄清一下,版本号中的数字组件应该被视为变量。以上数值只是为了说明标签的格式。

有没有办法自动进行这种更改?

【问题讨论】:

标签: git


【解决方案1】:

要列出所有标签,我建议使用 git for-each-ref--shell 选项 to eval refs
将其与one-liner to rename/delete a tag 结合使用。

#!/bin/sh

git for-each-ref --shell --format="ref=%(refname:short)" refs/tags | \
while read entry
do

    # assign tag name to $ref variable
    eval "$entry"

    # test if $ref starts with v
    ref2="${ref#v}"
    if [[ "${ref}" != "${ref2}" ]]; then

        # rename/delete tag
        git push origin refs/tags/${ref}:refs/tags/${ref2} :refs/tags/${ref}
        git tag -d ${ref}
    fi

done

【讨论】:

  • 谢谢,这非常有帮助:D
【解决方案2】:

按照 3 步方法处理一个或几个标签。对于大量标签,可以修改上述脚本以使用以下 3 个步骤作为替代解决方案。

第 1 步:识别当前标记指向的提交的提交/对象 ID

     command: git rev-parse <tag name>
     example: git rev-parse v0.1.0-Demo
     example output: db57b63b77a6bae3e725cbb9025d65fa1eabcde

第 2 步:从存储库中删除标签

     command: git tag -d <tag name>
     example: git tag -d v0.1.0-Demo
     example output: Deleted tag 'v0.1.0-Demo' (was abcde)

第 3 步:创建一个新标签,指向与旧标签所指向的提交 id 相同的提交 id

     command: git tag -a <tag name>  -m "appropriate message" <commit id>
     example: git tag -a v0.1.0-full  -m "renamed from v0.1.0-Demo" db57b63b77a6bae3e725cbb9025d65fa1eabcde
     example output: Nothing or basically <No error>

一旦本地 git 准备好更改标签名称,这些更改就可以推送回原点以供其他人使用。

【讨论】:

    猜你喜欢
    • 2012-08-10
    • 2021-08-04
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多