如何在 git 中编辑现有标签消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7813194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I edit an existing tag message in git?
提问by jared
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 存储库中有几个带注释的标签。旧标签包含虚假消息,我们希望将其更新为新样式。
% 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. Anyone know how we would do this?
在这个例子中,我们想让 v1.x 消息看起来像 v2.0 消息。有谁知道我们将如何做到这一点?
回答by Andy
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).
这将创建一个具有相同名称的新标签(通过覆盖原始标签)。
回答by Eric Hu
To update a complex message, just specify the annotated tag option with -a
or the signed tag option with -s
:
要更新复杂的消息,只需指定带注释的标签选项-a
或带签名的标签选项-s
:
git tag <tag name> <tag name>^{} -f -a
This will open an editor with the contents of your old tag message.
这将打开一个带有旧标签消息内容的编辑器。
回答by Sungam
git tag <tag name> <tag name>^{} -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.
<tag name>^{}
将解析标签/引用,直到找到第一个提交哈希。
回答by stanm
TL;DR
TL; 博士
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):
基于Sungram的回答(最初提出作为编辑):
1. Accepted answer
1. 接受的答案
This is an improvement over Andyand 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.
这是对Andy和Eric Hu的回答的改进。他们的答案将创建一个引用旧标签对象的新标签对象,并且两者都将具有相同的名称。
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
2. Sungram 的改进
Using <tag name>^{}
as the second argument of git tag
will instead delete all previous tags with the same name.
使用will<tag name>^{}
的第二个参数git tag
代替删除所有先前具有相同名称的标签。
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
3. 保存日期
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 substitutefor the second example (otherwise the original date would be lost due to overriding):
最后,如果您想将原始标签的日期保留为更新标签的日期,请使用一些 awk(或类似的)魔法,或者只是粘贴您想要的日期。以下是第二个示例的替代品(否则原始日期将因覆盖而丢失):
> GIT_COMMITTER_DATE="$(git show tag1 | # get info about the tag cascade including the date original of the original tag
> awk '{
> if ( == "Date:") {
> print substr(> git tag -d <tag-name>
> [GIT_COMMITTER_DATE=<original-commit-date>] \
> [GIT_AUTHOR_NAME=<original-author-name>] \
> git tag <tag-name> [commit]
, index(git tag <tag-name> <tag-name> -f -a
,))
> }
> }' | # 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
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 show
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.
这[optional]
是一个可选字段;<required>
是必填字段。当然,您可以像git tag
通常那样在命令后添加任何标志。
回答by liuyang1
@Andy 's solution
@Andy 的解决方案
git tag [<commit> | <old-tag>] <tag-name>
is wrong. After it, with
是错误的。之后,与
git tag <tag-name> -f -a
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:
它在 commit 时添加一个具有相同标签名称和新消息的新标签<tag-name>
。但它不会删除旧标签。这是这个命令的一个特例:
git tag <commit> <tag-name> -f -a
But just <old-tag>
is same with <tag-name>
.
但<old-tag>
与<tag-name>
.
Correct solution is simple, just update tag is OK.
正确的解决方案很简单,只需更新标签即可。
git tag v1.0 -f -m "actual message"
Remember, only ONEhere.
记住, 这里只有一个。
If we want change tag, which isn't HEAD
, we need an extra <commit>
argument.
如果我们想要更改标签,但不是HEAD
,我们需要一个额外的<commit>
参数。
[alias]
tm = "!sh -c 'f() { export GIT_COMMITTER_DATE=$(git log -1 --format=%ci ##代码##); git tag -f -a ##代码## ##代码##^{}; }; f '"
回答by VonC
we would like to make v1.x messages look like the v2.0 message
我们想让 v1.x 消息看起来像 v2.0 消息
With Git 2.17 (Q2 2018), there will be an alternative to creating a newtag with git tag <tag name> <tag name> -f -m "<new message>"
, since "git tag
" learned an explicit "--edit
" optionthat allows the message given via "-m
" and "-F
" to be further edited.
在 Git 2.17(2018 年第二季度)中,将有一种替代方法可以使用 来创建新标签git tag <tag name> <tag name> -f -m "<new message>"
,因为“ git tag
”学习了一个显式的“ --edit
”选项,允许进一步编辑通过“ -m
”和“ -F
”给出的消息。
See commit 9eed6e4(06 Feb 2018) by Nicolas Morey-Chaisemartin (nmorey
).
(Merged by Junio C Hamano -- gitster
--in commit 05d290e, 06 Mar 2018)
请参阅Nicolas Morey-Chaisemartin ( ) 的提交 9eed6e4(2018 年 2 月 6 日)。(由Junio C Hamano合并-- --in commit 05d290e, 06 Mar 2018)nmorey
gitster
tag
: add--edit
option
tag
: 添加--edit
选项
Add a --edit
option which allows modifying the messages provided by -m
or -F
, the same way git commit --edit
does.
添加一个--edit
选项,允许修改由-m
或提供的消息-F
,方式相同git commit --edit
。
回答by manojlds
You will have to tag again, using the -f
force flag.
您将不得不使用-f
force 标志再次标记。
回答by h0tw1r3
Using the answers above, this is my alias one-liner for .gitconfig
. Replaces existing tag and preserves the commit date.
使用上面的答案,这是我的别名单行.gitconfig
. 替换现有标签并保留提交日期。
Improvements?
改进?
回答by rubo77
If you are using a GUI like smartgitjust
如果您使用的是像一个GUI smartgit刚
- create the same tag again at the same position with the new message
- choose "overwrite existing tag"
- force-push the tag to the upstream repository
- 使用新消息在相同位置再次创建相同的标签
- 选择“覆盖现有标签”
- 将标签强制推送到上游存储库