Search code examples
gitgit-log

Show only tagged commits in git log


I'm looking for a way to show a git log, ordered by commits, showing their dates, and also, showing only commits which have tags.

Our webhost has an automated deploy system, and when a particular commit is pushed to production, it gets a certain tag on it. I am troubleshooting an issue, and I want to look at only those commits that have been on production.

I was able to make a git alias, which I call nanolog, like this:

nanolog = log --date-order --date=format:'%Y-%m-%d %H:%M:%S' --format='%C(yellow)%cd%Creset  %h  \"%s\"  %Cred%cn %Cgreen(%cr)'

which gives me a log looking like this:

2019-06-17 23:39:43  13f7e5f89  "Merge branch 'live-config' into int"  Webhost (3 weeks ago)
2019-06-17 23:24:20  32b6141dc  "Merge branch 'live-config' into int"  User Name (3 weeks ago)

I've looked at the documentation for git log, and in the section about --format, which is what I used to specify which information is displayed in my nanolog alias, I did not find any way to display tags. I looked at the pretty formats documentation, and likewise did not find a way to specify tag information.

In the stock git log, you see tags next to the commit hash:

commit d1d59f2fe151d1eb240b453e6efe76e4dfe13a93 (tag: webhost_test_198, tag: webhost_live_114, origin/06-24, 06-24)
Merge: 13f7e5f89 143b58189
Author: Webhost <webhost@webhost.com>
Date:   Fri Jun 21 20:25:24 2019 +0000

    Merged int into master

It's fairly laborious and somewhat error-prone to scroll through the stock git log, which does include tags information, along with all the other untagged commits. How can I get a brief log that shows only commits with tags?

Ideally I would like something like this:

2019-06-17 23:39:43  13f7e5f89 (tag: webhost_test_198, tag: webhost_live_114, origin/06-24, 06-24) "Merge branch 'live-config' into int"  Webhost (3 weeks ago)
2019-06-03 23:24:20  32b6141dc (tag: webhost_test_197, tag: webhost_live_113,)  "Merge branch 'live-config' into int"  User Name (5 weeks ago)

Solution

  • Let's start from a simple --oneline output on a mock repo and only last three commits

    git log --oneline -3
    
    4c37e97 fixed a bug in sub-feature F1
    d9c4599 added sub-feature F1
    06014cb changed part XYZ
    

    Now if we add decorations (references pointing to commits, namely tags and branches) with the --decorate flag :

    git log --oneline --decorate -3
    
    4c37e97 (HEAD -> feature/abc) fixed a bug in sub-feature F1
    d9c4599 added sub-feature F1
    06014cb (tag: release/1.22) changed part XYZ
    

    And at this point you can also add the --simplify-by-decoration flag to filter out commits not referenced by any tag/branch

    git log --oneline --decorate --simplify-by-decoration -3
    
    4c37e97 (HEAD -> feature/abc) fixed a bug in sub-feature F1
    06014cb (tag: release/1.22) changed part XYZ
    eec8aad (master) some older change
    

    But in your case with a specific format, you have the %d to this effect.

    If we modify your nanolog, which in my example outputs the following :

    git nanolog -3
    
    2019-07-08 19:19:46  4c37e97  "fixed a bug in sub-feature F1"  Romain (69 seconds ago)
    2019-07-08 19:19:11  d9c4599  "added sub-feature F1"  Romain (2 minutes ago)
    2019-07-08 19:18:26  06014cb  "changed part XYZ"  Romain (2 minutes ago)
    

    into this one

        nanolog2 = log --date-order --date=format:'%Y-%m-%d %H:%M:%S' --format='%C(yellow)%cd%Creset  %h  \"%d %s\"  %Cred%cn %Cgreen(%cr)'
    

    ...we'll obtain

    git nanolog2 -3
    
    2019-07-08 19:19:46  4c37e97  " (HEAD -> feature/abc) fixed a bug in sub-feature F1"  Romain (12 minutes ago)
    2019-07-08 19:19:11  d9c4599  " added sub-feature F1"  Romain (13 minutes ago)
    2019-07-08 19:18:26  06014cb  " (tag: release/1.22) changed part XYZ"  Romain (14 minutes ago)
    

    Finally, yes, you can pipe this to a grep "(tag:" or maybe just grep tag to filter out commits with only branches but no tags.