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。接受的答案
这是对Andy 和Eric 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。自己动手做
除了更新标签之外,您也可以删除它们并重新创建它们。事实证明,更新只是添加一个新标签并使其指向旧标签,或者,只是隐式删除旧标签并创建一个新标签以指向同一个提交。
您可以通过发出:
> 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 命令之后添加任何标志。