Can a lightweight tag be converted to an annotated tag?
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?
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>
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
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
You can also simply use git describe --tags
to also include lightweight tags in the search.