Search code examples
node.jsgitgit-log

How to build a list of all git commits on master?


Given a git repository, I would like to list all of the commits on the branch origin/master by date along with their SHA values. What is the easiest way to achieve this?

My desired result is an array of objects in Node.js representing a git repository, containing commit data such as

    [
      {
        date: "2020-02-02",
        sha: "03ffd2d7c3c1fdcc86f947537c6f3afa209948dd",
      },
      {
        date: "2019-03-13",
        sha: "3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95",
      },
      ...
    ]

or simply a comma-separated list such as

2020-02-02
03ffd2d7c3c1fdcc86f947537c6f3afa209948dd
2019-03-13
3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95
...

Solution

  • The easiest way would start with using what git provides out of the box. Here's an example:

    git log origin/master --date-order --format=%H%n%cs