Pretty git logging

I’ve recently been doing quite a bit of work with git log due to working on getting our CI server up to scratch with git. One of git logs handy options is

--pretty

Which allows you to define how the log is presented to you. As well as having a number of pre defined formats which you can pass to —pretty, for example:

--pretty=oneline

Pretty also allows you to create your own format using:

--pretty=format:<format>

Below is a full list of all available placeholders:

  • %H: commit hash
  • %h: abbreviated commit hash
  • %T: tree hash
  • %t: abbreviated tree hash
  • %P: parent hashes
  • %H: commit hash
  • %h: abbreviated commit hash
  • %T: tree hash
  • %t: abbreviated tree hash
  • %P: parent hashes
  • %p: abbreviated parent hashes
  • %an: author name
  • %aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %ae: author email
  • %aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %ad: author date (format respects —date= option)
  • %aD: author date, RFC2822 style
  • %ar: author date, relative
  • %at: author date, UNIX timestamp
  • %ai: author date, ISO 8601 format
  • %cn: committer name
  • %cN: committer name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %ce: committer email
  • %cE: committer email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %cd: committer date
  • %cD: committer date, RFC2822 style
  • %cr: committer date, relative
  • %ct: committer date, UNIX timestamp
  • %ci: committer date, ISO 8601 format
  • %d: ref names, like the —decorate option of git-log(1)
  • %e: encoding
  • %s: subject
  • %f: sanitized subject line, suitable for a filename
  • %b: body
  • %B: raw body (unwrapped subject and body)
  • %N: commit notes
  • %gD: reflog selector, e.g., refs/stash@{1}
  • %gd: shortened reflog selector, e.g., stash@{1}
  • %gs: reflog subject
  • %Cred: switch color to red
  • %Cgreen: switch color to green
  • %Cblue: switch color to blue
  • %Creset: reset color
  • %C(…): color specification, as described in color.branch.* config option
  • %m: left, right or boundary mark
  • %n: newline
  • %%: a raw %
  •  %x00: print a byte from a hex code

This very handily allows you to format the log however you wish. So let’s say we want output a very simple log that shows the commit’s author, hash and email adress, we can do the following:

git log --pretty=format:'Hash:%H Author:%an Email:%ae' -n 1

The -n 1 option simply means we only want to see the lastest commit logged, the above command outputs our log as we wished:

Hash:9af8acadf6739947114120e4cbe8a4bedd883c30 Author:Alex Fish Email:alex@alexefish.com

Nice!

1 note 
Tags: git --pretty format log
  1. alexefish posted this