Custom formatting with git log

  • Post author:
  • Post category:Git / Learning
  • Reading time:5 mins read

I’ve chatted about Git before, but today I wanted to dive on 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 a 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) so that I can 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 my subject line of my commit (that first line of the git commit message) and then the full body of the commit message are really important 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 full 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 CLI approach 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. In order 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 a lot of options for formatting, and you don’t have to use the custom options if you don’t want.

  • ‘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

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?

Leave a Reply