Mike Barkas

Software Technologies

Mike Barkas

Mike Barkas

Software Technologies

Customizing Git Log Output

November 1, 2016

Git Log

The git log command provides information regarding the history of the repository. There is a lot of history information that can be accessed using the log command and there is many options to format and filter this information.

The normal output of the log command shows the author, date, and commit information on multiple lines.

In this article I describe a few of the methods I use to display commit history information in a few different ways.

I will start by describing a few of the options available and then describe how I use a couple of them.


The two broad categories described below are built-in formatting and custom formatting.


Built-in Formatting

There are options you can use via the command line to use the built-in log formatting to display commonly used output.

One line The --oneline option reduces the commit information to one line of abbreviated information. This is helpful to get a quick overview of recent repository information.

git log --oneline

a90b071 Fixup typo in readme file. 2f5276b Update readme description. a21337a Set spacing to align text output. 5829ca9 Merge pull request #1 from repository/branch fdf62a4 First commit.

Decorate –decorate option will display branch and tag information. It is very common to combine the above oneline command above with the decorate option.

git log --oneline --decorate

a90b071 (HEAD -> master, origin/master) Fixup typo in readme file.
2f5276b Update readme description.
a21337a (tag: your-tag) Set spacing to align text output.
5829ca9 Merge pull request #1 from repository/branch
fdf62a4 First commit.

Graph

–graph option will give you a visual representation of the branches and how they were merged together. The graph only shows ASCII characters creating lines and asterisks, but it is enough to create a visual reference that is easy to understand.


git log --oneline --graph
  • a90b071 Fixup readme file.
  • 2f5276b Improve readme.
  • a21337a Set spacing to align text output.
  • 5829ca9 Merge pull request #1 from repository/branch |
    | * fdf62a4 Experiment with branch name. | * df77cdc Change shell declaration. |/
  • f958f93 Start branch loop.
  • 6f4fd45 Git comment notes for use.
  • e4b884f Start bash file.
  • e68d143 First commit.

Diffs
There are a lot of options for displaying diffs in your commit history. The two most common options are the -p option, for patch, and the --stat option for showing stats of inserted and deleted code.


git log --stat
  • a90b071 (HEAD -> master, origin/master) Fixup readme file. | README.md | 3 +++ | 1 file changed, 3 insertions(+)
  • 2f5276b Improve readme file content. | README.md | 5 +++++ | 1 file changed, 5 insertions(+)
  • a21337a Set spacing to align text output. | branches.sh | 4 ++– | 1 file changed, 2 insertions(+), 2 deletions(-)
  • 5829ca9 Merge pull request #12 from repository/branch |
    | * e57ba7a Changed while loop to a for in. | | branches.sh | 19 +++++++++———- | | 1 file changed, 9 insertions(+), 10 deletions(-) | * fdf62a4 Change with branch name. | | branches.sh | 11 +++++—— | | 1 file changed, 5 insertions(+), 6 deletions(-) | * df77cdc Change shell declaration. |/ | branches.sh | 2 +- | 1 file changed, 1 insertion(+), 1 deletion(-)
  • a68ff57 Start readme file. | README.md | 2 ++ | 1 file changed, 2 insertions(+)
  • f958f93 Start branch loop. | branches.sh | 11 +++++++++++ | 1 file changed, 11 insertions(+)

Shortlog
The shortlog command will group commits by author and display a short commit message. This is a command to be used instead of the log command. You can sort the output by number of commits per author using the -n option.

git shortlog  

Mary Smith (4): First commit. Start binary file. Git comment notes for use in readme. Edit readme file.

Joe Willis (3) Add method callback. Delete unused variable. Fix typo.


Custom formatting

If you don’t like the built-in formatting options, or just want to create additional output information, you can customize your own log output format.

Create your custom output by using the --pretty option and use format:"string". The format string may be enclosed in quotes and can contain replacement placeholders.

Replacements examples:

%cn will give you committer name
%h will display the commit hash
%cd will show the commit date

Using the placeholders above in an example for git log with custom formatting.

git log --pretty=format:"%h - %cn made this commit on %cd"  

a90b071 - Mary Smith made this commit on Thu Dec 17 23:18:07 2015 -0500 2f5276b - Mary Smith made this commit on Thu Dec 17 23:18:07 2015 -0500 a21337a - Mary Smith made this commit on Thu Dec 17 23:04:20 2015 -0500 5829ca9 - Mary Smith made this commit on Mon Nov 9 21:37:06 2015 -0500 fdf62a4 - Mary Smith made this commit on Mon Nov 9 20:24:32 2015 -0500 a68ff57 - Mary Smith made this commit on Mon Nov 9 19:23:39 2015 -0500 f958f93 - Mary Smith made this commit on Fri Sep 25 17:22:48 2015 -0400 e4b884f - Mary Smith made this commit on Fri Sep 25 17:02:40 2015 -0400

There are many placeholder tokens available to create your custom log output. See the Git log documentation for all options.

You can also combine your custom format and built-in options.

git log --oneline --pretty=format:"%h - %cn made this commit on %cd"

Color
You can use basic colors in you custom output as well. Check your color setting in your Git config for options.

Color token examples:

%Cred switch color to red
%Cgreen switch color to green
%Cblue switch color to blue
%Creset reset color

Remember to turn off a color, using reset, after you are finished to return your terminal color back to normal.

Changing log output color example:

git log --pretty=format:"%C(green)[%h]%Creset %gd %s %C(yellow)(%cr)%Creset"


Filtering

By default Git will show all commit history in your log output. After a short amount of time, this will display too much information.

Count
Reduce the amount of history shown with your log output with a number option like: -5. This will limit the output to only show the last 5 commits.

Example to only show 5 commits and a built-in option:

git log --online -5

Date
You can filter by date using different date expressions.

git log --after="2012-11-1"
git log --since="1 year ago"

File
You can filter by file(s) by listing the file name(s). Important note to remember to use the -- flag to indicate the following are file names and not branch names.

git log -- product_models.py

Message
To filter commit messages use the --grep option.

git log --grep="Issue 42"

Content
You can search and filter the source code for a particular string by using the -S"your string" option. This is also know as pickaxe.

git log -S"variable = 42"

There are many options for filtering by date, file, range, and branches. Check the Git documentation of all the options.


Configuration
Many of the default configurations for the log output can be changed or set via your gitconfig file.

Alias
Once you have created a few custom log formatting commands, it makes it much easier to recall them if you create an alias for them.

You can create an alias in your gitconfig file by using the keyword [alias]. You can also create a shell alias for your favorite commands.

I personally use a shell alias so I can also include the git in my command.

Here is an example of the four log commands I use most often set up in my shell as an alias:


gl

My standard git log limited by 6-8 commits so it fits in my Tmux pane nicely.

alias gl='git log --oneline --graph --no-decorate -6'


gll

This is my git log long alias to display more information and use color.

alias gll='git log --pretty=format:"%C(green)[%h]%Creset %gd %s %C(yellow)(%cr)%Creset %C(cyan)%cn%Creset" --graph'


glp

My alias for git log people to use the output of shortlog.

alias glp='git shortlog'


gls

This is what I call git log stats.

alias gls='git log --oneline --graph --decorate --stat'


Summary

Use the buit-in log formats and experiment creating your own custom log commands.

Create a shortcut or alias for the commands you use most often.

Consult the Git documentation for the many options available.

Git