18

I am trying to recreate the Git format setting of oneline as a format string (in order to extend it further).

So for this command

git log --format=oneline

What is the format string equivalent of oneline? The closest I can get is

git log --format="%h %d %s"

However, this does not produce any colors. I know I can hard code some of them, like the commit hash. But the %d has dynamic colors, depending on what it shows.

4 Answers 4

35

Turn on auto color

git log --format="%C(auto) %h %d %s"

and the output will look like this

enter image description here

From the git log documentation

%C(…): color specification, as described in color.branch.* config option; adding auto, at the beginning will emit color only when colors are enabled for log output (by color.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal). auto alone (i.e. %C(auto)) will turn on auto coloring on the next placeholders until the color is switched again.

3
  • I struggled a little bit to find it but the exact command to produce the same output as "git reflog" is: git log -g --abbrev-commit --pretty="%C(auto)%h%d %gD: %gs"
    – Oli
    Aug 17, 2020 at 13:20
  • On Windows 10 CMD, for some reason, %h%d becomes https://localhost:8888/d. If you add a space between them, then there is a space before the coloured text that indicates the position of HEAD.
    – Unknow0059
    Jan 11, 2021 at 4:34
  • 1
    @Unknow0059 In Windows environment variable substitution is done using %VAR_NAME%. Thus it seems that you have an environment variable h that contains the localhost URL you posted. Thus %h%d is substituted with https://localhost:8888/d. You can list the environment variables by typing set on the command line. I hope that helps.
    – René Link
    Jan 11, 2021 at 6:28
2

After times of struggling, I finally decide to use a pretty format like this:

lg = "log --graph --pretty=format:'%Cblue%ad%Creset -%C(yellow)%d%Creset %h %s %C(bold blue)<%an>%Creset'"

And the pretty effect looks like this enter image description here

And here is how to use it:

Step 1
Open your ~/.gitconfig file using your favorite editor like emacs or vim. Add below command under the [alias] section.

lg = "log --graph --pretty=format:'%Cblue%ad%Creset -%C(yellow)%d%Creset %h %s %C(bold blue)<%an>%Creset'"

Step 2

Within any of your local git repository, try execute:

git lg

So you are seeing the beautiful formatted log output now, just as the screenshot shows.

1

You will have to play with the colors. The color that you should use to solve your problem is auto that take a different color for each type of reference.

An example :

%Cred%h %Cgreen%<(10,trunc)%cd %C(auto)%d%Creset%s %C(bold blue)%an

1

there is a nice script that does it for you. Add it as alias and you will see a cool printings.

https://github.com/garybernhardt/dotfiles/blob/master/.githelpers

alias: l = "!source ~/.githelpers && pretty_git_log"

Here you have a sample .gitconfig file on how to set the alias + more aliases.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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