如何在git中编辑现有的标记消息?

问题描述 投票:183回答:8

我们的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消息。有谁知道我们会怎么做?

git git-tag
8个回答
217
投票

git tag <tag name> <tag name>^{} -f -m "<new message>"

这将创建一个具有相同名称的新标记(通过覆盖原始标记)。


72
投票

要更新复杂消息,只需使用-a指定带注释的标记选项,或使用-s指定带签名的标记选项:

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

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


37
投票

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

这是对AndyEric Hu's答案的改进。他们的答案将创建一个引用旧标记对象的新标记对象,其中两个标记对象将具有相同的标记名称。

<tag name>^{}将解析标记/引用,直到找到第一个提交哈希。


29
投票

TL;DR

您可以通过删除标记并在欺骗日期和作者的同时重新创建它来执行此操作:

> git tag -d <tag-name>
> [GIT_COMMITTER_DATE=<original-commit-date>] \
> [GIT_AUTHOR_NAME=<original-author-name>] \
> git tag <tag-name> [commit]

Whole story:

基于Sungram的答案(最初建议编辑):

1. Accepted answer

这是对AndyEric Hu答案的改进。他们的答案将创建一个引用旧标记对象的新标记对象,两者将具有相同的名称。

为了说明这一点,请考虑以下事项:

> git tag tag1 tag1 -f -a  # accepted answer
> git rev-list --objects -g --no-walk --all
[ example output: ]
6bdcc347fca041a5138f89fdf5276b3ebf9095d5
260ab7928d986472895b8c55e54569b3f3cb9517 tag1
a5797673f610914a45ef7ac051e3ee831a6e7c25 tag1
f22d6308c3cd330a3b0d86b9bf05562faf6b6f17

> git show tag1
tag tag1
Tagger: [tagger]
Date:   [date of updated tag]
[Updated description]

tag tag1
Tagger: [tagger]
Date:   [date of original tag]
[Original description]

[tagged commit details]

2. Sungram's improvement

使用<tag name>^{}作为git tag的第二个参数将改为删除所有以前具有相同名称的标签。

考虑前一个终端会话的继续:

> git tag tag1 tag1^{} -f -a  # suggested improvement
> git rev-list --objects -g --no-walk --all
[ example output: ]
6bdcc347fca041a5138f89fdf5276b3ebf9095d5
75f02acacfd7d91d55b5bcfdfb1f00aebeed15e3 tag1
f22d6308c3cd330a3b0d86b9bf05562faf6b6f17 

> git show tag1
tag tag1
Tagger: [tagger]
Date:   [date of updated tag]
[Updated description]

[tagged commit details]

3. Save the date

最后,如果您想将原始标记的日期保留为更新标记的日期,请使用一些awk(或类似)魔法,或者只是粘贴您想要的日期。以下是第二个示例的替代(否则原始日期将因覆盖而丢失):

> GIT_COMMITTER_DATE="$(git show tag1 |                              # get info about the tag cascade including the date original of the original tag
> awk '{
>     if ($1 == "Date:") {
>         print substr($0, index($0,$3))
>     }
> }' |                                                               # extract all the dates from the info
> tail -2 | head -1)"                                               `# get the second to last date, as the last one is the commit date` \
> git tag tag1 tag1^{} -a -f                                         # finally, update the tag message, but save the date of the old one
>
> git rev-list --objects -g --no-walk --all
6bdcc347fca041a5138f89fdf5276b3ebf9095d5
e18c178f2a548b37799b100ab90ca785af1fede0 tag1
f22d6308c3cd330a3b0d86b9bf05562faf6b6f17
> git show tag1
tag tag1
Tagger: [tagger]
Date:   [date of original tag]
[Updated description]

[tagged commit details]

参考文献:

4. DIY

或者更新标签,您可以删除它们并再次创建它们。事实证明,更新只会添加一个新标记并使其指向旧标记,或者只是隐式删除旧标记并创建一个新标记以指向同一个提交。

你可以通过发布:

> git tag -d <tag-name>
> [GIT_COMMITTER_DATE=<original-commit-date>] \
> [GIT_AUTHOR_NAME=<original-author-name>] \
> git tag <tag-name> [commit]

这里[optional]是一个可选字段; <required>是必填字段。当然,您可以在git tag命令之后添加任何标志。


9
投票

@Andy的解决方案

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

记住,这里只有一个。

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

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

4
投票

使用上面的答案,这是.gitconfig的别名单行。替换现有标记并保留提交日期。

[alias]
    tm = "!sh -c 'f() { export GIT_COMMITTER_DATE=$(git log -1 --format=%ci $0); git tag -f -a $0 $0^{}; }; f '"

改进?


4
投票

我们想让v1.x消息看起来像v2.0消息

使用Git 2.17(2018年第二季度),将有一个替代方法用git tag <tag name> <tag name> -f -m "<new message>"创建一个新标签,因为“git tag”学会了一个明确的“--edit”选项,允许通过“-m”和“-F”给出的消息进一步编辑。

commit 9eed6e4Nicolas Morey-Chaisemartin (nmorey)(2018年2月6日)。 (由Junio C Hamano -- gitster --合并于commit 05d290e,2018年3月6日)

tag:添加--edit选项

添加一个--edit选项,允许修改-m-F提供的消息,与git commit --edit的方式相同。


3
投票

您将不得不使用-f force flag再次标记。

git tag v1.0 -f -m "actual message"
© www.soinside.com 2019 - 2024. All rights reserved.