如何在 Git 中编辑现有标签消息?

发布于 2024-12-10 12:21:42 字数 221 浏览 0 评论 0 原文

我们的 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 消息。我们该怎么做呢?

We have several annotated tags in our Git repository. The older tags have bogus messages that we would like to update to be in our new style.

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

In this example, we would like to make v1.x messages look like the v2.0 message. How would we do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

℉絮湮 2024-12-17 12:21:42
git tag <tag name> <tag name>^{} -f -m "<new message>"

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

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

This will create a new tag with the same name (by overwriting the original).

风尘浪孓 2024-12-17 12:21:42

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

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

这将打开一个编辑器,其中包含以下内容您的旧标签消息。

To update a complex message, just specify the annotated tag option with -a or the signed tag option with -s:

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

This will open an editor with the contents of your old tag message.

酒与心事 2024-12-17 12:21:42

git tag <标签名称>; <标签名称>^{} -f -a

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

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

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

This is an improvement: without ^{} it will create a new tag object that reference the old tag object, where both of them will have the same tag name.

<tag name>^{} will resolve the tag/reference until it finds the first commit hash.

—━☆沉默づ 2024-12-17 12:21:42

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. 接受的答案

这是对 安迪胡志明的答案。
他们的答案将创建一个引用旧标签对象的新标签对象,并且两者将具有相同的名称。

为了说明这一点,请考虑以下内容:

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

使用 ^{} 作为 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]

这里[可选]是可选字段; 是必填字段。
当然,您可以像平常一样在 git tag 命令后添加任何标志。

TL;DR

You can do this by deleting your tag and recreating it while spoofing the date and author:

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

Whole story:

Building on Sungram's answer (originally proposed as an edit):

1. Accepted answer

This is an improvement over Andy and Eric Hu's answers.
Their answers will create a new tag object that references the old tag object and both are going to have the same name.

To illustrate this, consider the following:

> 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

Using <tag name>^{} as the second argument of git tag will instead delete all previous tags with the same name.

Consider the continuation of the previous terminal session:

> 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

Lastly, if you want to keep the date of the original tag as the date of the updated tag, use some awk (or similar) magic or just paste the date you want instead. The following is a substitute for the second example (otherwise the original date would be lost due to overriding):

> 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]

References:

4. DIY

Alternatively to updating the tags, you can just delete them and create them again. As it turns out updating just adds a new tag and makes it point to the old one, or alternatively, just implicitly deletes the old one and creates a new one to point to the same commit anyway.

You can achieve this by issuing:

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

Here [optional] is an optional field; <required> is a required field.
Of course, you can add any flags after the git tag command that you normally would.

余厌 2024-12-17 12:21:42

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

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

错误。之后,使用

git show

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

它在提交时添加具有相同标签名称的新标签和新消息。但它不会删除旧标签。这是此命令的特例:

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

但只是 相同。


正确的解决办法很简单,只需更新tag就可以了。

git tag <tag-name> -f -a

请记住,这里只有一个

如果我们想要更改标签(不是 HEAD),我们需要一个额外的 参数。

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

@Andy's original solution from 2016 (which was eventually updated)

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

is wrong. After it, with

git show

command, we will see stack tags with same name.

It add a new tag with same tag name and new message at commit <tag-name>. But it don't remove old tag. It's a special case of this command:

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

But just <old-tag> is same with <tag-name>.


Correct solution is simple, just update tag is OK.

git tag <tag-name> -f -a

Remember, only ONE here.

If we want change tag, which isn't HEAD, we need an extra <commit> argument.

git tag <commit> <tag-name> -f -a
醉城メ夜风 2024-12-17 12:21:42

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

使用 Git 2.17(2018 年第 2 季度),将有另一种方法可以使用 git tag 新标签名称> <标签名称> -f -m "",因为“git tag”学习了显式“--edit”选项 允许进一步编辑通过“-m”和“-F”给出的消息。

请参阅 提交 9eed6e4(2018 年 2 月 6 日),作者:尼古拉斯莫雷-蔡斯马丁 (nmorey)
(由 Junio C Hamano -- gitster -- 合并于 提交 05d290e,2018 年 3 月 6 日)

标签:添加--edit选项

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

we would like to make v1.x messages look like the v2.0 message

With Git 2.17 (Q2 2018), there will be an alternative to creating a new tag with git tag <tag name> <tag name> -f -m "<new message>", since "git tag" learned an explicit "--edit" option that allows the message given via "-m" and "-F" to be further edited.

See commit 9eed6e4 (06 Feb 2018) by Nicolas Morey-Chaisemartin (nmorey).
(Merged by Junio C Hamano -- gitster -- in commit 05d290e, 06 Mar 2018)

tag: add --edit option

Add a --edit option which allows modifying the messages provided by -m or -F, the same way git commit --edit does.

伴梦长久 2024-12-17 12:21:42

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

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

You will have to tag again, using the -f force flag.

git tag v1.0 -f -m "actual message"
小忆控 2024-12-17 12:21:42

使用上面的答案(尤其是Sungam的),这是我的.gitconfig的别名one-liner 。替换现有标签并保留提交日期。

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

改进?

Using the answers above (especially Sungam's), this is my alias one-liner for .gitconfig. Replaces existing tag and preserves the commit date.

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

Improvements?

初见你 2024-12-17 12:21:42

这就是我在 Github 标签页面中使用“RAMDOM 描述”成功更新标签消息号 54.4 的方法:

git tag "54.4" "54.4^{}" -f -m "RAMDOM DESCRIPTION"
git push origin main --tags -f

This is how I updated successfully a tag's message number 54.4 with a "RAMDOM DESCRIPTION" in the Github tags page:

git tag "54.4" "54.4^{}" -f -m "RAMDOM DESCRIPTION"
git push origin main --tags -f
山川志 2024-12-17 12:21:42

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

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

If you are using a GUI like smartgit just

  1. create the same tag again at the same position with the new message
  2. choose "overwrite existing tag"
  3. force-push the tag to the upstream repository
千鲤 2024-12-17 12:21:42

这里有一组别名,应该根据现有的答案(特别是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>"

支持轻量级标签

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

Here's a set of aliases that should do it for you based on the existing answers (especially stanm's) here:

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

These aliases accept a single tag name and any other flags to git tag and can be modified to support name changes pretty easily as well.

Usage:

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

Support for light-weight tags

Use creatordate, creatorname, and creatoremail instead of the tagger... variants. The creator... shortcuts will use tagger... if it exists and fallback to committer....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文