How can I list all lightweight tags?

I want to list all of the lightweight tags in my repository; the best I can think of involves combining git for-each-ref, grep, and cut, but it seems like it’ll be kind of fiddly…

(While we’re at it, we might as well talk about the same thing for annotated tags: someone is sure to end up here wondering about that at some point.)


By lightweight tags, I meant those tag refs that do not refer to tag objects. (In other words, unannotated tags.)

  • Git tags are displayed but I can't delete one of them
  • Will tag referencing commits unreachable from any branch be lost?
  • Why is “git describe” showing an older tag version on my clean git repo copy?
  • How do I read tagger information from a git tag?
  • Git tag checkout and git hook
  • How push tag without push a commit to remote repository with TortoiseGit?
  • Git - Checkout a remote tag when two remotes have the same tag name
  • In git how can I delete all tags(local and remote) except the latest one
  • One Solution collect form web for “How can I list all lightweight tags?”

    All the lightweight tags are in the refs/tags/ namespace and can be enumerated with, e.g.:

    git for-each-ref --format '%(refname:short)' refs/tags/


    git show-ref --tags

    As for annotated tags, well, the trick here—it affects the “lightweight” tags part too—is that an annotated tag is actually an object in the git repository, but, there’s a lightweight tag that points to that object, that lets you get at the annotated tag by its tag name.1 So it’s really a pair of things: a lightweight tag, plus the in-repo annotated tag object, that makes it “not a lightweight tag”, except for that stubborn fact that it is a lightweight tag at the same time!

    Thus, it boils down to: find all lightweight tags, then optionally select only tags pointing to commits or tags pointing to tag-objects depending on the behavior you want, then go on to emit the tag name.

    There’s a long example in the git-for-each-ref documentation of writing an entire script in the --format string and using eval to execute it (or you could pipe to sh to execute, at the cost of one extra process). I usually find it simpler to pipe the output of git for-each-ref into a while read ... loop:

    git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |
        while read ty name; do [ $ty = commit ] && echo $name; done

    which prints all lightweight-only tags.

    Compare with:

    git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |
        while read ty name; do [ $ty = tag ] && echo $name; done

    which prints all annotated tags (or more precisely, “lightweight-that-are-annotated” tags).

    Note that a tag can (conceivably—there’s no actual use case for this right now, as far as I know) point to something other than a commit or a tag; it’s up to you whether to do something with a tag pointing directly to a tree or blob.

    1Without the lightweight tag, you would not be able to refer to annotated tag annotag using the name annotag—not without going through all the search effort that git fsck uses to find dangling objects, at least. Moreover, if you delete the lightweight tag, the annotated tag object may get garbage-collected. You can make one tag object point to another tag object to keep it in the repo (i.e., inhibit the gc) without an external name for the second tag object, as long as the first one has an external name. That’s definitely an odd thing to do though.

    Interestingly, the internal format for the annotated tag contains the external name, so one can use this technique to protect “old” annotated tags, hide them by removing their lightweight tags, and then later restore the original lightweight tag. Whether anyone can come up with a use for this, though… 🙂

    Git Baby is a git and github fan, let's start git clone.