你如何重命名 Git 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028649/
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 you rename a Git tag?
提问by Brandon Fosdick
Today I was looking through the logs for a project and realized that I fat fingered a tag name some time ago. Is there some way to rename the tag? Google hasn't turned up anything useful.
今天我正在查看一个项目的日志,并意识到前段时间我胖了一个标签名称。有什么方法可以重命名标签吗?谷歌没有发现任何有用的东西。
I realize I could check out the tagged version and make a new tag, I even tried that. But that seems to create a tag object that isn't quite right. For one,
我意识到我可以查看标记版本并创建一个新标记,我什至尝试过。但这似乎创建了一个不太正确的标签对象。一方面,
git tag -l
lists it out of order relative to all of the other tags. I have no idea if that's significant, but it leads me to believe that the new tag object isn't quite what I want. I can live with that, because I really only care that the tag name matches the documentation, but I'd rather do it "right", assuming there is a right way to do this.
相对于所有其他标签不按顺序列出它。我不知道这是否重要,但它让我相信新的标签对象并不是我想要的。我可以接受,因为我真的只关心标签名称是否与文档匹配,但我宁愿“正确”这样做,假设有正确的方法可以做到这一点。
回答by Casey Watson
Here is how I rename a tag old
to new
:
这是我如何将标签重命名old
为new
:
git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags
The colon in the push command removes the tag from the remote repository. If you don't do this, Git will create the old tag on your machine when you pull.
push 命令中的冒号从远程存储库中删除标签。如果你不这样做,当你拉取时,Git 会在你的机器上创建旧标签。
Finally, make sure that the other users remove the deleted tag. Please tell them (co-workers) to run the following command:
最后,确保其他用户删除了已删除的标签。请告诉他们(同事)运行以下命令:
git pull --prune --tags
Note that if you are changing an annotated tag, you need ensure that the new tag name is referencing the underlying commit and not the old annotated tag object that you're about to delete. Therefore, use git tag -a new old^{}
instead of git tag new old
(this is because annotated tags are objects while lightweight tags are not, more info in this answer).
请注意,如果您要更改带注释的标签,则需要确保新标签名称引用底层提交,而不是您将要删除的旧带注释标签对象。因此,使用git tag -a new old^{}
而不是git tag new old
(这是因为带注释的标签是对象,而轻量级标签不是,这个答案中有更多信息)。
回答by Greg McGary
The original question was how to rename a tag, which is easy: first create NEW as an alias of OLD: git tag NEW OLD
then delete OLD: git tag -d OLD
.
最初的问题是如何重命名标签,这很简单:首先创建 NEW 作为 OLD: 的别名,git tag NEW OLD
然后删除 OLD: git tag -d OLD
。
The quote regarding "the Git way" and (in)sanity is off base, because it's talking about preserving a tag name, but making it refer to a different repository state.
关于“Git 方式”和 (in)sanity 的引用是不合理的,因为它在谈论保留标签名称,但使其引用不同的存储库状态。
回答by kaiser
In addition to the other answers:
除了其他答案:
First you need to build an aliasof the oldtag name, pointing to the original commit:
首先,你需要建立一个别名了的旧标签名称,指向原来的承诺:
git tag new old^{}
Then you need to delete the old one locally:
然后你需要在本地删除旧的:
git tag -d old
Then delete the tag on you remote location(s):
然后删除远程位置上的标签:
# 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
Finally you need to add your new tag to the remote location. Until you have done this, the new tag(s) will notbe added:
最后,您需要将新标签添加到远程位置。在您完成此操作之前,不会添加新标签:
git push origin --tags
Iterate this for every remote location.
对每个远程位置进行迭代。
Be aware, of the implications that a Git Tag change hasto consumers of a package!
请注意,Git 标签更改对包的消费者的影响!
回答by Robert Munteanu
If it's published, you can't delete it (without risking being tarred and feathered, that is). The 'Git way' is to do:
如果它已发布,则无法删除它(即不会冒着被涂上焦油和羽毛的风险)。“Git 方式”是这样做的:
The sane thing. Just admit you screwed up, and use a different name. Others have already seen one tag-name, and if you keep the same name, you may be in the situation that two people both have "version X", but they actually have different "X"'s. So just call it "X.1" and be done with it.
理智的事情。承认你搞砸了,并使用不同的名字。其他人已经看到了一个标签名,如果你保持相同的名字,你可能会遇到两个人都有“版本X”的情况,但他们实际上有不同的“X”。因此,只需将其称为“X.1”即可完成。
Alternatively,
或者,
The insane thing. You really want to call the new version "X" too, even though others have already seen the old one. So just use git-tag -f again, as if you hadn't already published the old one.
疯狂的事情。即使其他人已经看过旧版本,您也确实想将新版本称为“X”。所以只需再次使用 git-tag -f ,就好像你还没有发布旧的一样。
It's so insane because:
这太疯狂了,因为:
Git does not (and it should not) change tags behind users back. So if somebody already got the old tag, doing a git-pull on your tree shouldn't just make them overwrite the old one.
If somebody got a release tag from you, you cannot just change the tag for them by updating your own one. This is a big security issue, in that people MUST be able to trust their tag-names. If you really want to do the insane thing, you need to just fess up to it, and tell people that you messed up.
Git 不会(也不应该)将用户背后的标签改回来。因此,如果有人已经获得了旧标签,那么在您的树上执行 git-pull 不应该只是让他们覆盖旧标签。
如果有人从您那里获得了发布标签,您不能通过更新自己的标签来更改他们的标签。这是一个很大的安全问题,因为人们必须能够信任他们的标签名。如果你真的想做疯狂的事情,你只需要坦白承认,告诉人们你搞砸了。
All courtesy of the man pages.
全部由手册页提供。
回答by VonC
This wiki pagehas this interesting one-liner, which reminds us that we can push several refs:
这个 wiki 页面有一个有趣的单行,它提醒我们我们可以推送几个 refs:
git push origin <refs/tags/old-tag>:<refs/tags/new-tag> :<refs/tags/old-tag> && git tag -d <old-tag>
and ask other cloners to do
git pull --prune --tags
并要求其他克隆人做
git pull --prune --tags
So the idea is to push:
所以这个想法是推动:
<new-tag>
for every commits referenced by<old-tag
>:<refs/tags/old-tag>:<refs/tags/new-tag>
,- the deletion of
<old-tag>
::<refs/tags/old-tag>
<new-tag>
对于<old-tag
>:引用的每个提交<refs/tags/old-tag>:<refs/tags/new-tag>
,- 删除
<old-tag>
::<refs/tags/old-tag>
See as an example "Change naming convention of tags inside a git repository?".
请参阅示例“更改 git 存储库内标签的命名约定?”。
回答by Jared Knipp
As an add on to the other answers, I added an alias to do it all in one step, with a more familiar *nix move command feel. Argument 1 is the old tag name, argument 2 is the new tag name.
作为对其他答案的补充,我添加了一个别名以一步完成所有操作,并具有更熟悉的 *nix move 命令感觉。参数 1 是旧标签名称,参数 2 是新标签名称。
[alias]
renameTag = "!sh -c 'set -e;git tag ; git tag -d ;git push origin :refs/tags/;git push --tags' -"
Usage:
用法:
git renametag old new
回答by vikas pachisia
Follow the 3 step approach for a one or a few number of tags.
对于一个或几个标签,请遵循 3 步方法。
Step 1: Identify the commit/object ID of the commit the current tag is pointing to
步骤 1:确定当前标记指向的提交的提交/对象 ID
command: git rev-parse <tag name>
example: git rev-parse v0.1.0-Demo
example output: db57b63b77a6bae3e725cbb9025d65fa1eabcde
Step 2: Delete the tag from the repository
第 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)
Step 3: Create a new tag pointing to the same commit id as the old tag was pointing to
第 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>
Once the local git is ready with the tag name change, these changes can be pushed back to the origin for others to take these.
一旦本地 git 准备好标记名称更改,这些更改可以推送回原点以供其他人使用。
回答by wolfc
For the adventurous it can be done in one command:
对于喜欢冒险的人,它可以通过一个命令完成:
mv .git/refs/tags/OLD .git/refs/tags/NEW
回答by zionyx
You can also rename remote tags without checking them out, by duplicate the old tag/branch to a new name and delete the old one, in a single git push
command.
您还可以通过将旧标签/分支复制为新名称并在单个git push
命令中删除旧标签而无需检出即可重命名远程标签。
Remote tag rename / Remote branch → tag conversion:(Notice: :refs/tags/
)
远程标签重命名/远程分支→标签转换:(注意::refs/tags/
)
git push <remote_name> <old_branch_or_tag>:refs/tags/<new_tag> :<old_branch_or_tag>
Remote branch rename / Remote tag → branch conversion:(Notice: :refs/heads/
)
远程分支重命名/远程标签→分支转换:(注意::refs/heads/
)
git push <remote_name> <old_branch_or_tag>:refs/heads/<new_branch> :<old_branch_or_tag>
Output renaming a remote 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
回答by Olivier
Regardless of the issues dealing with pushing tags and renaming tags that have already been pushed, in case the tag to rename is an annotatedone, you could first copy it thanks to the following single-line command line:
无论处理推送标签和重命名已推送标签的问题如何,如果要重命名的标签是带注释的标签,您可以先复制它,这要归功于以下单行命令行:
git tag -a -m "`git cat-file -p old_tag | tail -n +6`" new_tag old_tag^{}
Then, you just need to delete the old tag:
然后,您只需要删除旧标签:
git tag -d old_tag
I found this command line thanks to the following two answers:
由于以下两个答案,我找到了这个命令行:
- https://stackoverflow.com/a/26132640/7009806(second comment)
- https://stackoverflow.com/a/49286861/7009806
Edit:
Having encountered problems using automatic synchronisation of tags setting fetch.pruneTags=true
(as described in https://stackoverflow.com/a/49215190/7009806), I personally suggest to firstcopy the new tag on the server and thendelete the old one. That way, the new tag does not get randomly deleted when deleting the old tag and a synchronisation of the tags would like to delete the new tag that is not yet on the server. So, for instance, all together we get:
编辑:
在使用标签设置自动同步时遇到问题fetch.pruneTags=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