DEV Community

Cover image for How to Format Git Log Output (And Stop Googling the Commands)
kandz
kandz

Posted on Originally published at tools.kandz.me

How to Format Git Log Output (And Stop Googling the Commands)

Git log is powerful, but the syntax is impossible to remember.

You want a clean graph, a custom date format, or a search by authorβ€”and you end up Googling it for the tenth time.

Let's look at how to get exactly what you want from your Git history.


The Shortest Possible Output

If you just want the author and date, you need to use the --format flag.

git log --format="%an %ad" --date=short
Enter fullscreen mode Exit fullscreen mode

Let's break down what those flags do:

  • --format="%an %ad": %an is the author's name, and %ad is the author's date.
  • --date=short: This formats the date as YYYY-MM-DD.

Output:

Alice 2023-10-24
Bob 2023-10-23
Enter fullscreen mode Exit fullscreen mode

Adding the Commit Hash

If you need the short commit hash for reference, add %h:

git log --format="%h %an %ad" --date=short
Enter fullscreen mode Exit fullscreen mode

Output:

a1b2c3d Alice 2023-10-24
e4f5g6h Bob 2023-10-23
Enter fullscreen mode Exit fullscreen mode

The --oneline Shortcut (And Its Limits)

Git has a built-in shortcut for a compact format:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

However, --oneline only includes the short hash and the commit message. It does not include the author or date. For that, you have to use the custom --format string above.


Visualizing the Graph

To see the branching history of your commits, add the --graph flag:

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

If you want to combine this with custom formatting, you can do it all in one command:

git log --graph --format="%h %an %ad %s" --date=short
Enter fullscreen mode Exit fullscreen mode

Common Gotchas

  • Timezone: %ad uses the author's timezone. If you want a consistent timezone, use %cd (committer date) and set the TZ environment variable.
  • Long names: If a user has a long name, the output alignment can break. You can't easily truncate strings in the format flag without some complex string manipulation.

Building Complex Log Commands

While --format="%an %ad" is easy to remember, things get complicated when you want to add graph visualizers (--graph), filter by date (--since), or search by author (--author).

If you want to configure advanced Git log formatting interactively without memorizing all the flags, I built a free Git Log Commands Generator.

Try it here

100% client-side. Nothing uploaded.


What's your most-used git log command? Let me know in the comments.

Top comments (0)