Custom formatting with git log
Tags: git, showdev, todayilearned, github
Published: 2021-05-13 17:53:39

---
title: Custom formatting with git log
published: true
date: 2021-05-13 17:45:30 UTC
tags: Git, showdev, todayilearned, github 
canonical_url: https://christine-seeman.com/2021/05/custom-formatting-with-git-log/?utm_source=rss&utm_medium=rss&utm_campaign=custom-formatting-with-git-log
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5fvozsxv3cdcqo5c1z4.png
---

Before, I’ve chatted about [git](https://christine-seeman.com/2018/07/git-workflow-and-my-new-love-of-git-alias/), 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](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests) 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 [CLI](https://git-scm.com/docs/gitcli)approach to retrieving that data.

A quick couple searching through git’s [very complete documentation](https://git-scm.com/docs) 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](https://git-scm.com/docs/git-log) and [custom formatting](https://git-scm.com/docs/pretty-formats/2.11.4) 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.

![](https://christine-seeman.com/wp-content/uploads/2021/05/image-1024x155.png)<figcaption>Example git log –pretty=oneline</figcaption>

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

![](https://christine-seeman.com/wp-content/uploads/2021/05/image-1-1024x251.png)<figcaption>Example git log –pretty=short</figcaption>

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

![](https://christine-seeman.com/wp-content/uploads/2021/05/image-2-1024x721.png)<figcaption>Example git log –pretty=full</figcaption>

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](https://cli.github.com/manual/) and see if I can streamline my [pull request](https://cli.github.com/manual/gh_pr) process even better, wish me luck! What git log fu helps you with your development flow?