What is the difference between an annotated and unannotated tag?

If I want to tag the current commit. I know both of the following command lines work:

git tag <tagname>

and

git tag -a <tagname> -m '<message>'

What is the difference between these commands?


TL;DR

The difference between the commands is that one provides you with a tag message while the other doesn't. An annotated tag has a message that can be displayed with git-show(1), while a tag without annotations is just a named pointer to a commit.

More About Lightweight Tags

Unless you select the -s flag for a signed tag, the tag will be a lightweight, unsigned tag by default. There are also some differences with lightweight tags:

  • When you use git tag <tagname> , Git will create a tag at the current revision but will not prompt you for an annotation. It will be tagged without a message.
  • When you use git tag -a <tagname> , Git will prompt you for an annotation unless you have also used the -m flag to provide a message.
  • When you use git tag -a -m <msg> <tagname> , Git will tag the commit and annotate it with the provided message.
  • When you use git tag -m <msg> <tagname> , Git will behave as if you passed the -a flag for annotation and use the provided message.
  • Basically, it just amounts to whether you want the lightweight tag to have an annotation associated with it or not.


    Usage differences

    man git-tag says:

    Annotated tags are meant for release while lightweight tags are meant for private or temporary object labels.

    And certain behaviors do differentiate between them in ways that this recommendation is useful eg:

  • annotated tags can contain a message, creator, and date different than the commit they point to. So you could use them to describe a release without making a release commit.

    Lightweight tags don't have that extra information, and don't need it, since you are only going to use it yourself to develop.

  • git push --follow-tags will only push annotated tags
  • git describe without command line options only sees annotated tags
  • Internals differences

  • both lightweight and annotated tags are a file under .git/refs/tags that contains a SHA-1

  • for lightweight tags, the SHA-1 points directly to a commit:

    git tag light
    cat .git/refs/tags/light
    

    prints the same as the HEAD's SHA-1.

    So no wonder they cannot contain any other metadata.

  • annotated tags point to a tag object in the object database.

    $ git tag -as -m msg annot
    $ git cat-file -t "$(cat .git/refs/tags/annot)"
    tag
    $ # Get a textual representation of the tag object.
    $ git cat-file -p "$(cat .git/refs/tags/annot)"
    object 4284c41353e51a07e4ed4192ad2e9eaada9c059f
    type commit
    tag annot
    tagger Ciro Santilli <your@mail.com> 1411478848 +0200
    
    msg
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)
    
    <YOUR PGP SIGNATURE>
    -----END PGP SIGNAT
    

    And this is how it contains extra metadata. As we can see from the output, the metadata fields are:

  • the object it points to
  • the type of object it points to. Yes, tag objects can point to any other type of object like blobs, not just commits.
  • the name of the tag
  • tagger identity and timestamp
  • message. Note how the PGP signature is just appended to the message
  • Bonuses

  • Determine if a tag is annotated:

    git cat-file -t tag
    

    Outputs commit for lightweight, tag for annotated.

  • List only lightweight tags: How can I list all lightweight tags?


  • The big difference is perfectly explained here.

    Basically, lightweight tags are just pointers to specific commits. No further information is saved ; on the other hand, annotated tags are regular objects , which have an author and a date and can be referred because they have their own SHA key.

    If knowing who tagged what and when is relevant for you, then use annotated tags. If you just want to tag a specific point in your development , no matter who and when did that, then lightweight tags are good enough.

    Normally you'd go for annotated tags, but it is really up to the Git master of the project.

    链接地址: http://www.djcxy.com/p/4656.html

    上一篇: 垃圾收集机制如何工作?

    下一篇: 带注释和不带注释的标签有什么区别?