Git log output to XML, JSON, or YAML?

This script wraps git log and produces JSON output:

I did something like this to create a minimal web api / javascript widget that would show the last 5 commits in any repository.

If you are doing this from any sort of scripting language, you really want to generate your JSON with something other than " for your quote character, so that you can escape real quotes in commit messages. (You will have them sooner or later, and it's not nice for that to break things.)

So I ended up with the terrifying but unlikely delimiter ^@^ and this command-line.

var cmd = 'git log -n5 --branches=* --pretty=format:\'{%n^@^hash^@^:^@^%h^@^,%n^@^author^@^:^@^%an^@^,%n^@^date^@^:^@^%ad^@^,%n^@^email^@^:^@^%aE^@^,%n^@^message^@^:^@^%s^@^,%n^@^commitDate^@^:^@^%ai^@^,%n^@^age^@^:^@^%cr^@^},\'';

Then (in node.js) my http response body is constructed from stdout of the call to git log thusly:

var out = ("" + stdout).replace(/"/gm, '\\"').replace(/\^@\^/gm, '"');
if (out[out.length - 1] == ',') {
    out = out.substring (0, out.length - 1);

and the result is nice JSON that doesn't break with quotes.

I wrote this in Powershell to get git logdata and save it as json or other format:

$header = @("commit","tree","parent","refs","subject","body","author","commiter") 
[string] $prettyGitLogDump= (git log MyCoolSite.Web- --pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;') 
$gldata = foreach ($commit in $prettyGitLogDump.Replace("; ",';') -split  ";", 0, "multiline") {
          $prop = $commit -split "\|"
          $hash = [ordered]@{}
          for ($i=0;$i -lt $header.count;$i++) {$hash.add($header[$i],$prop[$i])} 
$gldata |  ConvertTo-Json | Set-Content -Path "GitLog.json" 

The headernames:


have to be in sync with datafields :


See prettyformat docs.
I choose pipe | as a separator. I am taking a risc that it is not used in the commit message. I used semicolon ; as a sep for every commit. I should of course chosen something else. You could try to code some clever regular expression to match and check if your separators are used in the commit message. Or you could code more complex regularexpression to match split point or code a powershell scriptblock to define the split.

The hardest line in the code to figure out was.

prettyGitLogDump.Replace("; ",';') -split ";", 0, "multiline"

You have to set option multiline becuase there can be CR/LF in the messages and then split stops - you can only set multiline if nr of split is given. Therefore second paramvalue 0 which means all.

(The Replace("; ",';') is just a hack I get a space after the first commit. So I remove space after commit separator. Probably there is a better solution.)

Anyway i think this could be a workable solution for windows users or powershells fans that want the log from git to see who made the commit and why.

to output to a file:

git log > filename.log

To specify a format, like you want everything on one line

git log --pretty=oneline >filename.log

or you want it a format to be emailed via a program like sendmail

git log --pretty=email |

to generate JSON, YAML or XML it looks like you need to do something like:

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

This gist (not mine) perfectly formats output in JSON:

See also:

  • Git how to save a preset git log --format
  • How to parse the output of git log