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.

Anatomy of a Command

git

 

[program]

log

 

[command]

--oneline

 

[options]

Breakdown of how to use git log

at the command line in your shell

--graph

 

[options]

Default Output

Each output shows:

  • SHA-1 checksum
  • Author’s name and email
  • Date written
  • Commit message.
git log

Default Example

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Mary Smith <msith@example.com>
Date:   Sat Mar 3 16:40:33 2007 -0700

    removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Mary Smith <msith@example.com>
Date:   Sat Mar 1 10:31:28 2007 -0700

    first commit

Formatting Options

There are two basic ways to format the log output.

Built-in Formatting

Custom Formatting

--oneline
--pretty=format:"your custom string"

Built-in Formats

The built-in formats are preconfigured options you can use for displaying commonly used output.

One Line

The --oneline option reduces the output 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

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.

The --decorate option will display branch and tag information.

It is common to combine the oneline and decorate option.

Graph

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.

The --graph option will give you a visual display of the branches.

 

The graph shows ASCII characters creating lines and asterisks.

Diffs

There are options for displaying diffs in your history.

-p    for showing the patch

--stat    for showing file changed stats

The two most common options are:

Stat Example

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.

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:

--pretty=format:"string"

The format string may be enclosed in quotes and can contain replacement placeholders.

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

Custom Example

Placeholder example 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

Combine Format Types

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

You can also combine the built-in options

with your custom format.

Custom Color

You can use color in you custom output as well.

Check color setting in your Git config for options.

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

git log --pretty=format:"%Cred %h %Creset %s %Cblue %cr %Creset"

Filtering Output

By default Git will show all commit history in your log output.

 

After a short amount of time, too much information is shown.

Git provides a ways to filter or limit

the output of your log command.

Filter 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 --oneline -5

Filter by Date

You can filter by date using different date expressions.

git log --after="2012-11-1"

git log --since="1 year ago"

Filter by 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 -- my_file.module my_file.inc

Filter by Commit Messages

To filter commit messages use:

--grep


git log --grep="Issue 42"

Filter by Content

You can search and filter the source code

for a particular string by using:

-S"your string"

 

 

This is also know as pickaxe.


git log -S"variable = 42"

Git Configuration

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

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

Alias Custom Commands

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.

Alias Examples

Set alias from command line:

Manually set in gitconfig file:


git config --global alias.last 'log -1 HEAD'
[alias]
    st = status
    co = checkout
    br = branch

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

Create an alias in your shell profile configuration:

</the end>

https://slides.com/mikebarkas/customize-git-log-format

Made with Slides.com