brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git log with your own format string shows exactly the fields you want

Git
~/tips/tip-git-log-format-placeholders

git log --format='%h %an %ar %s' -10

Builds your own log line with placeholders for hash, author, date and subject.

The built-in log formats either show too much or too little. With a format string you pick the fields yourself.

git log --format='%h %an %ar %s' -10

Each % placeholder is a field: %h is the short hash, %an the author name, %ar the author date relative to now (“5 days ago”) and %s the subject line. A line comes out like 6ae2e76 Alice 5 days ago Fix login redirect.

A few others I find useful: %ae for the author email, %ad for the author date in the format of --date, %d for branch and tag names, and %b for the body of the message. For a CSV-ish export:

git log --date=short --format='%h,%ad,%an,%s'

Colors work too, with %C(yellow) to start a color and %Creset to end it. That’s nice in an alias, pointless in a file.

Be careful when you parse this output in a script. A comma or tab can sit inside a commit subject, so splitting on it breaks. For scripts I’d use a separator nobody types, such as %x00 for a NUL byte, and avoid %ar because “5 days ago” changes every day.

Further reading: git-log documentation, pretty formats

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link