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
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.
- ‘short’ is the commit sha1, author and title line
- ‘full’ is commit sha1, author, commiter (which could be seperate then the author), title line and full commit message
Those git log options had a lot of what I needed, especially in the case of
full but I only wanted the
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 (
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.