Search code examples
gittagsdescribe

Git describe does not provide expected tag


I have a script that uses

git describe --tags --abbrev=0

In order to determine the latest tag on the repo. So far so good.

But if I have to tags on the same commit, the command gives me the first available tag for said commit instead of the latest.

Here is a simple exemple

~ $ mkdir test_dir
~ $ cd test_dir/
test_dir $ git init
Initialized empty Git repository in /home/amaurin/test_dir/.git/
test_dir (unborn) $ touch coucou
test_dir (unborn ✱1) $ git add coucou
test_dir (unborn ✚1) $ git commit -m "Initial Commit"
[master (root-commit) 6972f53] Initial Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 coucou
test_dir (master ✔) $ git tag 1
test_dir (master ✔) $ git describe --tags --abbrev=0
1
test_dir (master ✔) $ git tag 2
test_dir (master ✔) $ git tag
1
2
test_dir (master ✔) $ git describe --tags --abbrev=0
1

At the last call I was expecting 2, not 1 and I don't understand why...

The funny thing is that I've seen in a GIT changelog that a similar issue has been fixed already :

  • "git describe" did not tie-break tags that point at the same commit correctly; newer ones are preferred by paying attention to the
    tagger date now.

https://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.7.1.1.txt and the funny thing is that I have a recent version of git

$ git --version
git version 2.2.1

Please help :'-(

sigh... sigh...


Solution

  • You are using a lightweight tag, so there is not tagger date. Using annotated tags will give you a tag object which records the tag date. (Annotated tags must have a message.)

    git tag 1 -m 'Tag 1'
    git tag 2 -m 'Tag 2'
    

    Definitely read http://git-scm.com/book/en/v2/Git-Basics-Tagging to find out more about tags in Git.