我们的Git存储库中有几个带注释的标记。旧的标记具有虚假的消息,我们希望将其更新为新样式。

% git tag -n1
v1.0 message
v1.1 message
v1.2 message
v2.0 Version 2.0 built on 15 October 2011.

在这个例子中,我们想让v1。X消息看起来像v2.0消息。我们该怎么做呢?


当前回答

如果你使用的是像smartgit这样的GUI

在新消息的相同位置再次创建相同的标记 选择“覆盖现有标签” 强制将标记推送到上游存储库

其他回答

@安迪2016年的原始解决方案(最终更新)

git tag <tag-name> <tag-name> -f -a

是错误的。之后,用

git show

命令时,我们将看到具有相同名称的堆栈标记。

它在commit <tag-name>处添加了一个带有相同标签名的新标签和新消息。但它不删除旧标签。这是该命令的特殊情况:

git tag [<commit> | <old-tag>] <tag-name>

但是<old-tag>和<tag-name>是一样的。


正确的解决方法很简单,只要更新标签就可以了。

git tag <tag-name> -f -a

记住,这里只有一个。

如果我们想要change标签,而不是HEAD,我们需要一个额外的<commit>参数。

git tag <commit> <tag-name> -f -a

Git标签<标签名> <标签名>^{}-f -a

这是一种改进:如果没有^{},它将创建一个引用旧标记对象的新标记对象,其中它们将具有相同的标记名称。

<标签名称>^{}将解析标签/引用,直到找到第一个提交散列。

要更新一个复杂的消息,只需用-a指定带注释的标记选项或用-s指定带符号的标记选项:

git tag <tag name> <tag name>^{} -f -a

这将打开一个包含旧标记消息内容的编辑器。

下面是一组别名,可以根据现有的答案(特别是stanm的答案)为您提供:

# Edit an existing tag, preserving the date and tagger
tag-amend = "!f() { : git tag ;\
    eval \"`git x-tag-environment-string`\";\
    git tag -a -f --edit -m \"$(git x-tag-message \"$1\")\" \"$1\" \"$1^{}\" \"${@:2}\";\
}; f"

# Rewrite an existing tag, preserving the date and tagger (accepts -m and -F)
tag-rewrite = "!f() { : git tag ;\
    eval \"`git x-tag-environment-string`\";\
    git tag -a -f \"$1\" \"$1^{}\" \"${@:2}\";\
}; f"

# Helpers to Extract the Tag Data
x-tag-data = tag -l --format
x-tag-message = x-tag-data '%(contents)'
x-tagger-name = x-tag-data '%(taggername)'
x-tagger-email = x-tag-data '%(taggeremail)'
x-tag-date = x-tag-data '%(taggerdate:rfc2822)'
x-tag-environment-string = "!f() { echo '\
    export GIT_COMMITTER_DATE=${GIT_COMMITTER_DATE-`git x-tag-date \"$1\"`};\
    export GIT_COMMITTER_NAME=${GIT_COMMITTER_NAME-`git x-tagger-name \"$1\"`};\
    export GIT_COMMITTER_EMAIL=${GIT_COMMITTER_EMAIL-`git x-tagger-email \"$1\"`};\
';}; f"

这些别名接受单个标记名和git标记的任何其他标记,并且可以很容易地进行修改以支持名称更改。

用法:

# opens editor to edit existing message
git tag-amend <tag name>

# add a new paragraph to the existing message
git tag-amend <tag name> -m "<new paragraph>"

# replace the message with a new one
git tag-rewrite <tag name> -m "<new message>"

支持轻量级标签

使用creatordate, creatorname和creatoremail代替标记器…变体。创造者……快捷方式将使用标签…如果存在,则回退到提交者....

您将不得不再次标记,使用-f力标志。

git tag v1.0 -f -m "actual message"