Sam Keller

My git log
may 15, 2016
I suppose if I'm going to have my own website with a section for a blog, I should probably put something in that blog from time to time.

I'm a big fan of git and specifically of using it on the command line. So, I thought I'd share a quick tip on the options I use for git log to get a sense of a repository's history from the command line. There are a lot of options for git log, and plenty of other people on the internet sharing what they use to spruce up git log, but I thought I'd share what I do. Here's the command:
git log --all --graph --decorate --date=local --color --pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s" | less -R
Let me break this down piece by piece.


This makes all of the commits for all the references in refs/ show up. So any commit towards any of the branches, remotes, and tags that my repository knows about will show up.


A handy option to show an ASCII graph that gives you a visual sense of the commit history -- this makes it easy to see what branches were merged into what other branches.


Building off of the --all option, this prints out the ref names of any commits that are shown. That way, we know to which commit each branch, remote, and tag points to.


This shows the timestamp of each commit using my local timezone -- because I travel back and forth between the US and Uganda for my job at Fenix, this helps me keep my head straight when figuring out what commit happened when.


This colorizes the ASCII graph lines but is only needed if you're piping through less like I am -- without using less, the graph lines should be colorized without needing this option.

--pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s"

The most complicated part -- formatting the output with --pretty to make it look nice. This line specifies where you want each part of the commit and in what color. Each time you want to call for a change in color, you do a %C(color_name) and that will change the color for the following text. If you want to reset it to not be colorized anymore, you call %Creset. Then, you also specify the attributes: %h is for the abbreviated commit hash, %cn is for the committer name, %cd is for the committer date, %d is for the ref names with parentheses around them, and then %s is the subject of the commit. So, my git log will read as follows: Change color to yellow, show the abbreviated commit hash, change color to green, show the date, change color to blue, show the committer name, change color to red, show any refs, reset the color to be none, and show the subject of the commit.

| less -R

Lastly, I pipe everything through the less command so that after I'm done with the command, no trace of it remains on the screen. I had to use the -R option to make it display correctly with less, but that may just be my own shell's problem. Here's what it looks like on DuckDuckGo's "zeroclickinfo-fathead" repository:

I'd also recommend creating an alias for this command so you don't have to memorize all this. I put an alias in my ~/.bash_profile file like so:
alias glog='git log --all --graph --decorate --date=local --color --pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s" | less -R'

There are tons more options for git log and you can read more about them in the source for git-log