Lately I’ve been less inclined to open a tool like gitk or SourceTree to inspect the commit graph of a repository. Some time ago I read about a technique to output the graph on the command line. Since I didn’t save the link, I had to do some digging and found the following solution on Stack Overflow. The screenshot is from the Twitter Bootstrap 3.0.0-WIP branch.

The basic command is uses git log. The command below is great if you want all of the details for each commit but it makes it difficult to get an overview.

$ git log --graph

By adding –pretty=oneline we can get the same result but all of the info on one line instead of multiple.

$ git log --graph --pretty=oneline

You rarely need the full commit hash so let’s use "format" to show just the short hash and the commit subject.

$ git log --graph --pretty=format:'[%h] %s by %cn'

The following are a few of the options for "format." The rest are found in the Pretty Formats section of the git-log man page.

Option  Description of Output
%H  Commit hash
%h  Abbreviated commit hash
%T  Tree hash
%t  Abbreviated tree hash
%P  Parent hashes
%p  Abbreviated parent hashes
%d  Ref names
%an Author name
%ae Author e-mail
%ad Author date (format respects the --date= option)
%ar Author date, relative
%cn Committer name
%ce Committer email
%cd Committer date
%cr Committer date, relative
%s  Subject

%Creset reset color
%C(..) color specification, as described in color.branch.* config option

Now let’s add some color to the output as was as the ref name.

$ git log --graph --pretty=format:'[%C(yellow)%h%Creset]%C(cyan)%d%Creset %s %C(white dim)by %cn%Creset'

Now that we have the output exactly as we want it, let’s create an alias in ~/.gitconfig so that we don’t have to retype this long command every time we want to see the graph.

[alias]
l = log --graph --pretty=format:'[%C(yellow)%h%Creset]%C(cyan)%d%Creset %s %C(white dim)by %cn%Creset'    

Now run git with this alias.

$ git l

Finally, we don’t always need to look at the entire commit history so let’s just look at recent commits.

$ git l --since=2.weeks
$ git l --since=3.days

This blog post was inspired by this answer on StackOverflow.

You can find even more information on git log in Chapter 2.3 of the Pro Git book online.