如何在 Git 中重命名带注释的标签 [英] How to rename an annotated tag in Git

查看:31
本文介绍了如何在 Git 中重命名带注释的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Git 中重命名现有的注释标签?

How can an existing annotated tag be renamed in Git?

我有近百个标签表示存储库上的版本号,每个标签都带有关于该版本更改内容的有用描述.我想更改我用于这些标签的命名风格,记录标签消息,删除标签,并用旧消息和新名称重新创建它,对于近百个标签手动执行将是一场噩梦.执行此操作的脚本或一系列 git 命令对自动化很有用.

I have almost a hundred tags representing version numbers on a repository, and each tag is annotated with a useful description about what was changed that version. I'd like to change the naming style I've used for these tags, and recording the tag message, deleting the tag, and recreating it with the old message and a new name would be a nightmare to do manually for almost a hundred tags. A script or a series of git commands to do this would be useful for automation.

推荐答案

坏消息是带注释的标记对象的名称嵌入在该带注释的标记对象中.所以如果你想彻底彻底,你需要替换那些标签对象:

The bad news is that the name of an annotated tag object is embedded inside that annotated tag object. So if you want to be totally thorough, you'll need to replace those tag objects:

  • 如果它们是签名的标签,你需要用新的签名重新生成它们.

  • If they're signed tags you will need to regenerate them with new signatures.

否则,可以将标记对象复制到新标记对象,并进行系统名称更改.例如,filter-branch 命令就是这样做的.(这有点难看,因为您必须使用一些低级别的管道命令;没有简单方便的方法.但请参见下文.)

Otherwise, it's possible to copy the tag objects to new tag objects, with the systematic name changes you'd like to make. The filter-branch command does this, for instance. (It's a bit ugly since you have to use some low level plumbing commands; there's no easy convenient method. But see below.)

好消息是,没有绝对要求您使用正确"注释标签和轻量级标签.也就是说,一个带注释的标签实际上是一对实体:一个轻量级标签(其名称只是 refs/tags/ 命名空间中的一个条目),以及一个带注释的标签对象(在存储库本身)通常在其 tag 字段中具有相同的名称.但是你可以新建一个轻量级标签,指向已有的注解标签对象,然后选择性地删除原来的轻量级标签:

The good news is that there's no absolute requirement that you use the "correct" annotated tag along with a lightweight tag. That is, an annotated tag is actually a pair of entities: a lightweight tag (with some name that's simply an entry in the refs/tags/ name-space), along with an annotated tag object (in the repository itself) that normally has the same name in its tag field. But you can make a new lightweight tag that points to the existing annotated tag object, then optionally delete the original lightweight tag:

$ git cat-file -p v2.5.0 | sed 's/@/ /'
object a17c56c056d5fea0843b429132904c429a900229
type commit
tag v2.5.0
tagger Junio C Hamano <gitster pobox.com> 1438025401 -0700

Git 2.5
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABAgAGBQJVtoa5AAoJELC16IaWr+bLRtQP/0RYjVe9fLubiN5vLaAJ98B5
K3apw8bScJ4bZQJiOGMZg7AJ8pSB9XchqopjNlO2v8XVrZEkFPQ7ln3ELjOITusO
0MIZz6Y4sS0yqMklNPWx/OIzvSyKOs8quUA/J87Ha+pnMYlobDgWJxDnZ3hVO6q2
0lVMRUvwM9qsBiPsVKyAba5qPDBn9uTjgR/hivA3Ha97foq/qMM6rjERp5hX8KTE
JacLMlx7ZSAJiNKmz1mVk+xyDVGDh9nojiz93jRYohAM8gsbyyRayPGKlKsMrajC
s0bLxilV8zupNrMOs54ND71VqNo992ewiCrB3FBpTah2rPE0MKsxCY72pFiZp/hn
w1I3seQbd880d2TXfczVVphG3xN3xbfnC+aEqobgPuFIgGxHqeWqUpDQnWa0XhtK
i5phhENEjbMdCDGf7ylRRvCTL5O2Mz1XnNfZBP0uqIX6PyzaCwcZM1DUP0xY8Nvz
xo0BplMvK4sAr+fCW0HHHlDO6Ky3jjRyPUEyVEbwF50lZ3Sgzkkf32I06yeZgQW9
Ge6+qGopw7JVQM95UDMBysbQSNuTmwnclwn0DBfc85vQwZw5OwnRk0RsQxcnqNr2
B4Nyubl8Pge4H0T0pahpAdr0AU7JHv4i4yYZh1Dr4d+hzzXAK+ospCxkcJVX7Qz5
BP8pbLPj3ubLAenxg71I
=xfTv
-----END PGP SIGNATURE-----
$ git rev-parse v2.5.0
8d1720157c660d9e0f96d2c5178db3bc8c950436
$ git tag silly v2.5.0
$ git rev-parse silly
8d1720157c660d9e0f96d2c5178db3bc8c950436
$ git tag -d v2.5.0
Deleted tag 'v2.5.0' (was 8d17201)
$ git for-each-ref refs/tags/silly
8d1720157c660d9e0f96d2c5178db3bc8c950436 tag    refs/tags/silly

(我恢复了旧标签并在此之后删除了傻"版本,所以它没有经过非常彻底的测试,但至少没有炸毁.)

(I restored the old tag and deleted the "silly" version after this, so it's not terribly thoroughly tested, but it didn't blow up at least.)

要自动复制标签或重新创建标签(带或不带签名),只需使用 git for-each-ref 查找所有旧标签,并使用结果名称制作新标签.使用 git cat-file -p 漂亮地打印现有标签以进行修改以制作新标签,或者只是使新标签指向现有标签对象.打印标签(到管道或文件)后,您可以使用 sed 或其他一些可编程编辑器对其进行编辑,并将结果提供给 git tag -F 作为输入文件,或 git mktag 创建一个标签对象,将生成的 SHA-1 提供给git tag制作对应的轻量级标签.

To automate either copying the tags, or re-creating the tags (with or without signatures), simply use git for-each-ref to find all the old tags, and use the resulting names to make the new tags. Use git cat-file -p to pretty-print the existing tag for modifying to make the new tag, or simply make the new tag pointing to the existing tag object. After printing the tag (to a pipe or file) you can edit it with sed or some other programmable editor, and feed the result to git tag -F as an input file, or to git mktag to create a tag object, feeding the resulting SHA-1 to git tag to make the corresponding lightweight tag.

(注意:如果您已经编写了一个 bash 脚本来完成所有这些工作,并且只需要帮助来完成它或纠正一些问题,请发布到目前为止的脚本.)

(Note: if you've gotten partway through writing a bash script to do all this, and just need help to finish it up or correct some issue(s) with it, post the script-so-far.)

这篇关于如何在 Git 中重命名带注释的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆