Can a lightweight tag be converted to an annotated tag?

倖福魔咒の 提交于 2019-12-17 22:13:54

问题


I've tagged a commit with a lightweight tag, and pushed that tag to a remote repo, shared with other developers. I have now realised I should have annotated it so that it appears in git describe.

Is there a way to convert it/re-tag the commit without breaking things?


回答1:


A lightweight tag is just a 'ref' that points at that commit. You can force-create a new annotated tag on top of the old tag:

git tag -a -f <tagname> <tagname>

As of Git v1.8.2, you need to use --force to replace any tags on a remote with git push, even if you are replacing a lightweight tag with something that is effectively a fast-forward or a true tag object pointing at the same commit as the existing tag reference.

git push --force origin <tagname>



回答2:


Based on Charles' answer and on this blog post, I think it is better to use something like this:

#!/bin/sh
tag=$1
date="$(git show $tag --format=%aD | head -1)"
GIT_COMMITTER_DATE="$date" git tag -a -f $tag $tag



回答3:


You can also simply use git describe --tags to also include lightweight tags in the search.




回答4:


Convert all tags to annotated (based on Charles Bailey's example and Ferenc Wágner's comment):

for tag in $(git tag -l); do git tag -a -f $tag $tag^0 -m $tag; done
git push --tags --force


来源:https://stackoverflow.com/questions/5002555/can-a-lightweight-tag-be-converted-to-an-annotated-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!