DEV Community

Cover image for Custom formatting with git log
christine
christine

Posted on • Originally published at christine-seeman.com on

Custom formatting with git log

Before, I’ve chatted about git, but I wanted to dive on today some learning that I did recently on git log specifically.

I wanted to streamline how I populate my pull request descriptions. Typically with my git commits, I like to keep to the atomic commit philosophy. My commits tell the why of my code change. The what of my code change really is in the code itself, so for me, when someone writes something like changed formatting or updated database table, that really doesn’t tell me something helpful. I write my commit messages for future developers (i.e., future me) to understand my thought process behind a change and why I was doing it. This is so helpful for debugging, code sleuthing, or to help out if someone is just plain curious.

So the subject line of my commit (that first line of the git commit message) and then the entire body of the commit message are crucial for helping describe what is in my code and why I am doing it. Using that data fills out a lot of what I want to provide in a pull request description. So, what I really needed from my commits, the subject, and the entire commit message body, was something that I would manually click around in my pull request to retrieve. Knowing git, I knew there had to be a CLIapproach to retrieving that data.

A quick couple searching through git’s very complete documentation turned up what I needed. from my branch to use in my pull request description. To grab the git commit subject and body, I needed a combo of using git log and custom formatting on that git log.

Git log has many options for formatting, and you don’t have to use the custom options if you don’t want to.

  • ‘oneline’ provides just <sha1> <title line> and is as compact as possible. Great for if you want to check out a lot of commits.

Example git log –pretty=oneline

  • ‘short’ is the commit sha1, author and title line

Example git log –pretty=short

  • ‘full’ is commit sha1, author, commiter (which could be seperate then the author), title line and full commit message

Example git log –pretty=full

Those git log options had a lot of what I needed, especially in the case of full but I only wanted the subject and body nothing else! So I had to pull in the custom formatting options. So what ended up working for me was to limit to the last 4 commit (-4) and format for the subject (%s) and body (%b)


git log -4 --pretty=format:"%s %b" | pbcopy
Enter fullscreen mode Exit fullscreen mode

So by using that command, I retrieved just the subject and the body of the commit message, and then copied the text from standard input into clipboard buffer (| pbcopy). This helps me fill out my pull request that much quicker.

Next up, I am going to explore GitHubs CLI and see if I can streamline my pull request process even better, wish me luck! What git log fu helps you with your development flow?

Top comments (0)