51

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?

4 Answers 4

41

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>
12
  • 5
    But won't this use a confusing date?
    – SamB
    Jan 9, 2014 at 19:06
  • 3
    Also, are there any implications for developers that already pulled the original lightweight tag? Will those developers see the annotation to the new tag after they've pulled again, or do they need to do something special (like deleting their local tag) before?
    – sschuberth
    Jul 23, 2015 at 10:23
  • 1
    @ZacThompson: This changed in v1.8.2 (March 2013)
    – CB Bailey
    May 20, 2016 at 23:47
  • 4
    If you'd rather not have tags tagging tags, do git tag -a -f <tagname> <tagname>^0 instead. Otherwise you create a new reference to your old tag instead of the stolen refs/tags/<tagname>, excluding it from garbage collection. Jul 21, 2016 at 14:56
  • 6
    Warning: If you do this on a GitHub repo, and the lightweight tag you are replacing was associated with a release, deleting it will silently delete the entire release (and release notes) associated with the lightweight tag. As noted here, there's no way to restore an accidentally-deleted release.
    – Edward
    Jun 25, 2020 at 22:25
25

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
4
  • 3
    Nice :) Instead of | head -1 you can use -s or --no-patch. And maybe using the committer date (%cD) (and also their name, %cn, as GIT_COMMITER_NAME) might be desirable. Feb 10, 2017 at 9:49
  • And email! GIT_COMMITTER_EMAIL%ce (attn. committer is always spelled with double TT)
    – Kay
    Jul 14, 2019 at 12:22
  • 1
    @TobiasKienzler Hmm, I don't think I would use their name unless I had reason to believe they had actually tagged it. Using the committer date rather than the author date does make sense, though, as the tag can't have been made before the commit was, and committer dates are normally not before author dates ...
    – SamB
    Aug 14, 2021 at 20:16
  • @SamB Ah true, of course I assumed the committer tagged, which now (sorry :) that you mention it is probably impossible to determine from the lightweight tag alone, good point Aug 16, 2021 at 4:11
6

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
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.