简体   繁体   中英

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

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.

You can use %d or %D , as mentioned in the git documentation for pretty formatting . They will show ref names, ie 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM