How do I show tags in a custom git log format?
Asked Answered
S

1

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.

Splanchnic answered 16/6, 2019 at 18:27 Comment(0)
S
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>
Splanchnic answered 16/6, 2019 at 18:27 Comment(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 ...Oligocene
you could pipe it into sed: sed 's|(tag: \(.*\))|[\1]|'Splanchnic

© 2022 - 2024 — McMap. All rights reserved.