今天,我在查看一个项目的日志,发现我在前段时间发现了一个标签名。有什么方法可以重命名标记吗?谷歌没有发现任何有用的东西。

我意识到我可以检查标记的版本并制作一个新的标记,我甚至尝试过。但这似乎创建了一个不太正确的标记对象。例如,

git tag -l

列出了它相对于所有其他标记的顺序。我不知道这是否有意义,但这让我相信新的标记对象并不是我想要的。我可以接受这一点,因为我真的只关心标记名是否与文档匹配,但我宁愿“正确”地做,假设有正确的方法可以做到这一点。


当前回答

对于冒险者,只需一个命令即可完成:

mv .git/refs/tags/OLD .git/refs/tags/NEW

其他回答

除其他答案外:

首先,您需要构建旧标记名的别名,指向原始提交:

git tag new old^{}

然后您需要在本地删除旧的:

git tag -d old

然后删除远程位置上的标记:

# Check your remote sources:
git remote -v
# The argument (3rd) is your remote location,
# the one you can see with `git remote`. In this example: `origin`
git push origin :refs/tags/old

最后,您需要将新标记添加到远程位置。在完成此操作之前,不会添加新标记:

git push origin --tags

对每个远程位置重复此操作。

请注意,Git标签更改对包的消费者的影响!

您还可以在不签出的情况下重命名远程标记,方法是在单个git push命令中将旧标记/分支复制为新名称并删除旧标记。

远程标记重命名/远程分支→ 标记转换:(注意::refs/tags/)

git push <remote_name> <old_branch_or_tag>:refs/tags/<new_tag> :<old_branch_or_tag>

远程分支重命名/远程标记→ 分支转换:(注意::refs/heads/)

git push <remote_name> <old_branch_or_tag>:refs/heads/<new_branch> :<old_branch_or_tag>

输出重命名远程标记:

D:\git.repo>git push gitlab App%2012.1%20v12.1.0.23:refs/tags/App_12.1_v12.1.0.23 :App%2012.1%20v12.1.0.23

Total 0 (delta 0), reused 0 (delta 0)
To https://gitlab.server/project/repository.git
 - [deleted]               App%2012.1%20v12.1.0.23
 * [new tag]               App%2012.1%20v12.1.0.23 -> App_12.1_v12.1.0.23

对于冒险者,只需一个命令即可完成:

mv .git/refs/tags/OLD .git/refs/tags/NEW

对于一个或几个标记,请遵循三步方法。

步骤1:确定当前标记所指向的提交的提交/对象ID

command: git rev-parse <tag name>
example: git rev-parse v0.1.0-Demo
example output: db57b63b77a6bae3e725cbb9025d65fa1eabcde

步骤2:从存储库中删除标记

command: git tag -d <tag name>
example: git tag -d v0.1.0-Demo
example output: Deleted tag 'v0.1.0-Demo' (was abcde)

步骤3:创建一个新标记,该标记指向与旧标记所指向的提交id相同的提交id

command: git tag -a <tag name>  -m "appropriate message" <commit id>
example: git tag -a v0.1.0-full  -m "renamed from v0.1.0-Demo" db57b63b77a6bae3e725cbb9025d65fa1eabcde
example output: Nothing or basically <No error>

一旦本地git准备好标记名更改,这些更改就可以被推回到原始位置,以便其他人接受:

command: git push origin :<old tag name> <new tag name>
example: git push origin :v0.1.0-Demo v0.1.0-full
example output: <deleted & new tags>

无论如何处理推送标记和重命名已推送的标记,如果要重命名的标记是带注释的标记,您可以首先通过以下单行命令行复制它:

git tag -a -m "`git cat-file -p old_tag | tail -n +6`" new_tag old_tag^{}

然后,您只需删除旧标记:

git tag -d old_tag

由于以下两个答案,我找到了此命令行:

https://stackoverflow.com/a/26132640/7009806(第二条评论)https://stackoverflow.com/a/49286861/7009806

编辑:使用自动同步标记设置fetch时遇到问题。prunTags=true(如中所述https://stackoverflow.com/a/49215190/7009806),我个人建议首先在服务器上复制新标签,然后删除旧标签。这样,在删除旧标签时,新标签不会被随机删除,并且标签的同步将希望删除服务器上尚未出现的新标签。例如,我们一起得到:

git tag -a -m "`git cat-file -p old_tag | tail -n +6`" new_tag old_tag^{}
git push --tags
git tag -d old_tag
git push origin :refs/tags/old_tag