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

时间:2011-10-18 20:21:12

标签: git git-tag

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

10 个答案:

答案 0 :(得分:219)

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

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

答案 1 :(得分:72)

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

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

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

答案 2 :(得分:37)

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

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

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

答案 3 :(得分: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]

全文:

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

1。接受的答案

这比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的改进

使用<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。保存日期

最后,如果您想将原始代码的日期保留为更新代码的日期,请使用一些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命令之后添加任何标志。

答案 4 :(得分:10)

@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

请记住,此处仅 ONE

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

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

答案 5 :(得分:5)

  

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

使用Git 2.17(2018年第二季度),可以选择使用git tag <tag name> <tag name> -f -m "<new message>"创建标记,因为&#34; git tag&#34;学会了明确&#34; --edit&#34;选项,允许通过&#34; -m&#34;和&#34; -F&#34;待进一步编辑。

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相同。

答案 6 :(得分:4)

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

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

改进?

答案 7 :(得分:3)

您必须使用-f强制标记再次标记。

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

答案 8 :(得分:1)

这是一组别名,应根据此处的现有答案为您完成此操作:

# 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>"

支持轻型标签

使用creatordatecreatornamecreatoremail而不是tagger...变体。 creator...快捷键将使用tagger...(如果存在)并回退到committer...

答案 9 :(得分:0)

如果您使用的是 smartgit 这样的GUI,只是

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