35

I really like the short git log format where I can see author, date and change description like this:

git log --pretty=format:"%h%x09%an%x09%ad%x09%s"

Which outputs:

  fbc3503 mads    Thu Dec 4 07:43:27 2008 +0000   show mobile if phone is null...   
  ec36490 jesper  Wed Nov 26 05:41:37 2008 +0000  Cleanup after [942]: Using timezon
  ae62afd tobias  Tue Nov 25 21:42:55 2008 +0000  Fixed #67 by adding time zone supp
  164be7e mads    Tue Nov 25 19:56:43 2008 +0000  fixed tests, and a 'unending appoi

(from stackoverflow question "link text")

Now, the question is, how do I save this as a new format on my machine so I only have to write something like, for instance:

git log --format=jespers_favourite
Community
  • 1
  • 1
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155

4 Answers4

42

In newer versions of Git (confirmed with v1.7.8) it is possible to set named pretty-print log formats using git config pretty.named_format. These can be set at a machine-wide, user or file level with the <file-option> argument.

To create a log format called jespers_favourite or the whole machine use --system

git config --system pretty.jespers_favourite "%h%x09%an%x09%ad%x09%s"

For single user use '--global'

git config --global pretty.jespers_favourite "%h%x09%an%x09%ad%x09%s"

Leaving the <file-option> argument blank will default to setting the config file of the current repository, .git/config unless defined otherwise.

tdbit
  • 933
  • 11
  • 15
  • 2
    This is almost right, but there are a couple of important caveats: the name can't contain an underscore (at least in Git 1.7.10), and the custom format must begin with "format:" (or refer to one of the built-in formats). – RJHunter Jan 03 '13 at 10:57
25

Considering the git log manual page mentions:

--pretty[=<format>]
--format[=<format>]

Pretty-print the contents of the commit logs in a given format, where can be one of oneline, short, medium, full, fuller, email, raw and format:. When omitted, the format defaults to medium.

the <format> can only have predefined values.
That only leaves you the possibility to define an alias as a shortcut for that command.

[alias]
        jespers_favourite = log --pretty=format:"%h%x09%an%x09%ad%x09%s"

or

[alias]
        compactlog = log --pretty=format:"%h%x09%an%x09%ad%x09%s"

Rokit adds in the comments:

Escaping the quotes worked until I tried adding custom colors.
For that, I also had to add an additional set of regular quotes around the escaped ones, e.g.

log --pretty=format:"\"%C(#9be3bc) %s\""
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @ftassi Good point. I have restored the link (with the one you mention). – VonC Jan 05 '13 at 13:31
  • 2
    I needed to escape the quotes too, in order to make this work – ftassi Jan 05 '13 at 13:43
  • 1
    @ftassi you needed to escape the quote in a `git config alias.xxx...` commands? Not in the `.config` file itself? – VonC Jan 05 '13 at 13:51
  • 2
    @VonC I've escaped them it the ~/.gitconfig file, here's a snippet of what I'm using https://gist.github.com/283ad42cd367ec35cf76 – ftassi Jan 05 '13 at 14:07
  • Escaping the quotes worked until I tried adding custom colors. For that, I also had to add an additional set of regular quotes around the escaped ones, e.g. `log --pretty=format:"\"%C(#9be3bc) %s\""` – Rokit Nov 09 '22 at 23:34
  • 1
    @Rokit Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Nov 10 '22 at 06:25
7

With recent versions of git (1.7… 2.21 here) you can change your default format to a named format or the format string. And while you're doing it you may as well add some PRETTY FORMATS auto colors. Also I find it easier to edit the file rather than use the commands; but I start with them to get the right config file format first, and then tinker.

The default is changed with:

git config --global --add format.pretty \
"%C(yellow)%h%Creset%x09%Cred%<(13)%an%Creset%x09%Cblue%ad%Creset%x09%s"

Resulting in the addition to your .gitconfig of:

[format]
    pretty = %C(yellow)%h%Creset%x09%Cred%<(13)%an%Creset%x09%Cblue%ad%Creset%x09%s

It is, however, probably more useful to you to add a named format as mentioned in the later part of your question.

A named format is added with:

git config --global pretty.dateline \
"format:%C(yellow)%h%Creset%x09%Cred%<(13)%an%Creset%x09%Cblue%ad%Creset%x09%s"

Or by adding to your .gitconfig as:

[pretty]
    dateline = format:%C(yellow)%h%Creset%x09%Cred%<(13)%an%Creset%x09%Cblue%ad%Creset%x09%s

Used in combination:

If you add both, you could make them read:

[pretty]
    dateline = format:%C(yellow)%h%Creset%x09%Cred%<(13)%an%Creset%x09%Cblue%ad%Creset%x09%s

[format]
    pretty = dateline

Caveats:

The difference in the format: prefix should be noted, and personally I like to leave the default pretty format as medium. As you know the %ad part can be modified by --date=relative or any of default, iso8601, local, raw, relative, rfc2822, short ; so I like to use it like:

git log -25 --pretty=dateline --date=short -- when/was/this/changed.txt
dlamblin
  • 43,965
  • 20
  • 101
  • 140
6

You can configure the default pretty format using git-config. From the git-config documentation:

 format.pretty
           The default pretty format for log/show/whatchanged command, See git-log(1), git-show(1), git-whatchanged(1).

For example:

git config --add format.pretty fuller

or original poster's desired format:

git config --add format.pretty "%h%x09%an%x09%ad%x09%s"

Like with other git config settings, format.pretty may be set at the global, system, or repository scope (default).

Marc D
  • 61
  • 1
  • 2
  • FYI this changing the default format doesn't sound like what the question originally wanted, but it's a useful point to add. – dlamblin Aug 20 '20 at 07:31