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
Let's break down what those flags do:
-
--format="%an %ad":%anis the author's name, and%adis the author's date. -
--date=short: This formats the date as YYYY-MM-DD.
Output:
Alice 2023-10-24
Bob 2023-10-23
Adding the Commit Hash
If you need the short commit hash for reference, add %h:
git log --format="%h %an %ad" --date=short
Output:
a1b2c3d Alice 2023-10-24
e4f5g6h Bob 2023-10-23
The --oneline Shortcut (And Its Limits)
Git has a built-in shortcut for a compact format:
git log --oneline
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
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
Common Gotchas
-
Timezone:
%aduses the author's timezone. If you want a consistent timezone, use%cd(committer date) and set theTZenvironment 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.
100% client-side. Nothing uploaded.
What's your most-used git log command? Let me know in the comments.
Top comments (0)