523

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?

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
user462301
  • 5,339
  • 2
  • 17
  • 11
  • 1
    possible duplicate of [In what circumstances should I add the -a flag to the git tag command?](http://stackoverflow.com/questions/4092640/in-what-circumstances-should-i-add-the-a-flag-to-the-git-tag-command) – Thilo Jul 16 '12 at 23:37
  • 1
    @Thilo This is *not* an exact duplicate. The referenced question is about when to annotate, not about the related flags. – Todd A. Jacobs Jul 16 '12 at 23:42
  • 2
    This is very well explained in Git's documentation: http://git-scm.com/book/en/Git-Basics-Tagging – Samy Dindane Jul 17 '12 at 08:25
  • 1
    TLDR unannotated: commit; annotated: commit, author, date, (optional) comment – Antony Hatchkins Aug 19 '19 at 05:10

4 Answers4

405

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

According to the documentation: "To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name". There are also some different options to write a message on annotated 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 (this is a lightweight tag).
  • 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 tag to have an annotation and some other information associated with it or not.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 7
    Is there a difference between a tag "annotation" and a commit message? – Steve Bennett Jul 16 '12 at 23:57
  • 4
    @SteveBennett Yes. A tag annotation is not a commit message. You can't see it with git-log(1); you need to use git-show(1). – Todd A. Jacobs Jul 17 '12 at 00:03
  • 180
    The difference between "annotated" and "lightweight" tags goes beyond message. You can have annotated tag without a message (`git tag -a -m ''`), but an annotated tag always has *tagger* (author) and *date*. – Piotr Findeisen Oct 24 '13 at 00:36
  • @PiotrFindeisen That's quite a big deal, thank you. … the message bit I'd almost never care about (because tag names in my experience are descriptive of a point in process anyway, and a message is pretty unnecessary). The tagger and the date matter to me, though. – Benjohn Jul 29 '15 at 12:38
  • 1
    Same for me. Version tags usually have pretty useless messages (can it say more than the name? What for?). Unfortunately, this top-voted answer does not mention this difference. – Piotr Findeisen Jul 30 '15 at 14:39
  • 68
    Another important thing to note is that when you push your tags to a remote repository using ``git push --follow-tags``, only the annotated tags will be pushed. – Sander Toonen Dec 11 '15 at 17:13
  • 8
    Complementing @Xatoo comment, `git push origin --tags` push both types of tags, annotated and lightweight tags. – Wesley Gonçalves May 11 '20 at 05:04
  • @PiotrFindeisen The tag command with `-m` and no value returns for me `error: switch 'm' requires a value`. Should there be a whitespace between `''`? – FireEmerald May 25 '21 at 14:50
  • See here for how to update your global **.gitconfig** file to push annotated tags to the remote with every `git push`: [How do you push a tag to a remote repository using Git?](https://stackoverflow.com/questions/5195859/how-do-you-push-a-tag-to-a-remote-repository-using-git/26438076#26438076) – Gabriel Staples May 30 '21 at 07:47
321

Push annotated tags, keep lightweight local

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 e.g.:

  • 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
    cat .git/refs/tags/annot
    

    contains the SHA of the annotated tag object:

    c1d7720e99f9dd1d1c8aee625fd6ce09b3a81fef
    

    and then we can get its content with:

    git cat-file -p c1d7720e99f9dd1d1c8aee625fd6ce09b3a81fef
    

    sample output:

    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:

    A more detailed analysis of the format is present at: What is the format of a git tag object and how to calculate its SHA?

Bonuses

  • Determine if a tag is annotated:

    git cat-file -t tag
    

    Outputs

    • commit for lightweight, since there is no tag object, it points directly to the commit
    • tag for annotated, since there is a tag object in that case
  • List only lightweight tags: How can I list all lightweight tags?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
63

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.

Community
  • 1
  • 1
Luis
  • 2,833
  • 3
  • 27
  • 41
0

Another difference is when git tag -a foo fails, because there is already a tag foo/bar, as explained here.

With Git 2.42 (Q3 2023), "git tag"(man) learned to leave the "$GIT_DIR/TAG_EDITMSG" file when the command failed, so that the user can salvage what they typed.
That git tag message file would not exist for a lightweight tag.

See commit 08c12ec, commit 669c11d, commit 719515f (16 May 2023) by Kristoffer Haugsbakk (LemmingAvalanche).
(Merged by Junio C Hamano -- gitster -- in commit 6d2a88c, 13 Jun 2023)

tag: keep the message file in case ref transaction fails

Signed-off-by: Kristoffer Haugsbakk

The ref transaction can fail after the user has written their tag message.
In particular, if there exists a tag foo/bar and git tag -a foo(man) is said then the command will only fail once it tries to write refs/tags/foo, which is after the file has been unlinked.

Hold on to the message file for a little longer so that it is not unlinked before the fatal error occurs.

And:

doc: tag: document TAG_EDITMSG

Suggested-by: Junio C Hamano
Signed-off-by: Kristoffer Haugsbakk

Document TAG_EDITMSG which we have told the user about on unsuccessful command invocations since commit 3927bbe ("tag: delete TAG_EDITMSG only on successful tag", 2008-12-06, Git v1.6.1-rc2 -- merge).

Introduce this documentation since we are going to add tests for the lifetime of this file in the case of command failure and success.

Use the documentation for COMMIT_EDITMSG from git-commit.txt as a template since these two files share the same purpose.1

† 1: from commit 3927bbe:

“ This matches the behavior of COMMIT_EDITMSG, which stays around
  in case of error.

git tag now includes in its man page:

FILES

$GIT_DIR/TAG_EDITMSG

This file contains the message of an in-progress annotated tag. If git tag exits due to an error before creating an annotated tag then the tag message that has been provided by the user in an editor session will be available in this file, but may be overwritten by the next invocation of git tag.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250