9

Situation

I am using git log with a custom --pretty:format:

 git --no-pager log --pretty=format:"%C(yellow)%h%Creset %s %Cgreen(%cr) %Cblue<%an>%Creset" -5

which produces an output like this

7224466 update version (4 days ago) <Xerus>
3f00703 improve stuff (9 days ago) <Xerus>

Problem

I want to also see the tags of a commit if it has any associated with it, like the option --decorate, but I couldn't find any mention of the tags in the formatting documentation.

1 Answer 1

15

You can use %d or %D, as mentioned in the git documentation for pretty formatting. They will show ref names, i.e. the names of branches and tags associated with the corresponding commit.

You'll probably want to use the lowercase d, since it automatically formats the ref properly for pretty displaying in the console, together with %C(auto), which will automatically color it the way you are used to.

Putting it together, you could modify your command to this:

 git --no-pager log --pretty=format:"%C(auto)%h%d - %s %Cgreen(%cr) %Cblue<%an>%Creset" -5

which will result in an output like this

a2b8f3c (HEAD -> master, origin/master) - refactor: rename variable snackbarTextCache (8 weeks ago) <Xerus>
51a90be (tag: dev116-51a90be) - Fix connect.sid instructions (3 months ago) <Xerus>
fc372c3 - Update dependencies (3 months ago) <Xerus>
2
  • is there a way to change the format of the tag so if we're using them for version numbers, they could be enclosed in [ ], so they can look like this: 51a90be [dev116-51a90be] - Fix connect.sid instructions ...
    – Merricat
    Commented May 23, 2022 at 21:57
  • you could pipe it into sed: sed 's|(tag: \(.*\))|[\1]|'
    – xeruf
    Commented May 24, 2022 at 22:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.