Skip to main content
Chapter 2 of 61 · 14 sections · Page 19

Git: Browsing History

Inspect commit histories using git log, formatting, graph visualization, author filters, and date ranges.

Original chapter reference: Chapter 2: Browsing the history. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Explanation -q, --quiet Quiet, suppresses diff output --source Shows source of commit --use-mailmap Use mail map file (changes user info for committing user) --decorate[=...] Decorate options Show log for specific range of lines in a file, counting from 1. Starts from line n, goes to line --L <n,m:file> m. Also shows diff. --show-signature Display signatures of signed commits -i, --regexp-ignore-case Match the regular expression limiting patterns without regard to letter case

Section 2.1: "Regular" Git Log

Source example · PDF page 19
git log

will display all your commits with the author and hash. This will be shown over multiple lines per commit. (If you wish to show a single line per commit, look at onelineing). Use the q key to exit the log.

By default, with no arguments, git log lists the commits made in that repository in reverse chronological order – that is, the most recent commits show up first. As you can see, this command lists each commit with its SHA-1 checksum, the author’s name and email, the date written, and the commit message. - source

Example (from Free Code Camp repository):

Source example · PDF page 19
commit 87ef97f59e2a2f4dc425982f76f14a57d0900bcf
Merge: e50ff0d eb8b729
Author: Brian
Date:   Thu Mar 24 15:52:07 2016 -0700
Merge pull request #7724 from BKinahan/fix/where-art-thou
Fix 'its' typo in Where Art Thou description
commit eb8b7298d516ea20a4aadb9797c7b6fd5af27ea5
Author: BKinahan
Date:   Thu Mar 24 21:11:36 2016 +0000
Fix 'its' typo in Where Art Thou description
commit e50ff0d249705f41f55cd435f317dcfd02590ee7
Merge: 6b01875 2652d04
Author: Mrugesh Mohapatra
Date:   Thu Mar 24 14:26:04 2016 +0530
Merge pull request #7718 from deathsythe47/fix/unnecessary-comma
Remove unnecessary comma from CONTRIBUTING.md

If you wish to limit your command to last n commits log you can simply pass a parameter. For example, if you wish to list last 2 commits logs

Source example · PDF page 20
git log -2

Section 2.2: Prettier log

To see the log in a prettier graph-like structure use:

Source example · PDF page 20
git log --decorate --oneline --graph

sample output :

Source example · PDF page 20
* e0c1cea (HEAD -> maint, tag: v2.9.3, origin/maint) Git 2.9.3
*   9b601ea Merge branch 'jk/difftool-in-subdir' into maint
|\
| * 32b8c58 difftool: use Git::* functions instead of passing around state
| * 98f917e difftool: avoid $GIT_DIR and $GIT_WORK_TREE
| * 9ec26e7 difftool: fix argument handling in subdirs
* |   f4fd627 Merge branch 'jk/reset-ident-time-per-commit' into maint
...

Since it's a pretty big command, you can assign an alias:

Source example · PDF page 20
git config --global alias.lol "log --decorate --oneline --graph"

To use the alias version:

Source example · PDF page 20
# history of current branch :
git lol

# combined history of active branch (HEAD), develop and origin/master branches :
git lol HEAD develop origin/master

# combined history of everything in your repo :
git lol --all

Section 2.3: Colorize Logs

Source example · PDF page 20
git log --graph --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr)
%C(yellow)<%an>%Creset'

The format option allows you to specify your own log output format:

Parameter Details %C(color_name) option colors the output that comes after it %h or %H abbreviates commit hash (use %H for complete hash) %Creset resets color to default terminal color %d ref names %s subject [commit message] %cr committer date, relative to current date %an author name

Section 2.4: Oneline log

Source example · PDF page 20
git log --oneline

will show all of your commits with only the first part of the hash and the commit message. Each commit will be in a single line, as the oneline flag suggests.

The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. - source

Example (from Free Code Camp repository, with the same section of code from the other example):

Source example · PDF page 21
87ef97f Merge pull request #7724 from BKinahan/fix/where-art-thou
eb8b729 Fix 'its' typo in Where Art Thou description
e50ff0d Merge pull request #7718 from deathsythe47/fix/unnecessary-comma
2652d04 Remove unnecessary comma from CONTRIBUTING.md
6b01875 Merge pull request #7667 from zerkms/patch-1
766f088 Fixed assignment operator terminology
d1e2468 Merge pull request #7690 from BKinahan/fix/unsubscribe-crash
bed9de2 Merge pull request #7657 from Rafase282/fix/

If you wish to limit you command to last n commits log you can simply pass a parameter. For example, if you wish to list last 2 commits logs

Source example · PDF page 21
git log -2 --oneline

Section 2.5: Log search

Source example · PDF page 21
git log -S"#define SAMPLES"

Searches for addition or removal of specific string or the string matching provided REGEXP. In this case we're looking for addition/removal of the string #define SAMPLES. For example:

Source example · PDF page 21
+#define SAMPLES  100000

or

Source example · PDF page 21
-#define SAMPLES  100000

git log -G"#define SAMPLES"

Searches for changes in lines containing specific string or the string matching provided REGEXP. For example:

Source example · PDF page 21
-#define SAMPLES  100000
+#define SAMPLES  100000000

Section 2.6: List all contributions grouped by author name

git shortlog summarizes git log and groups by author

If no parameters are given, a list of all commits made per committer will be shown in chronological order.

Source example · PDF page 21
$ git shortlog
Committer 1 (<number_of_commits>):
    Commit Message 1
    Commit Message 2
    ...
Committer 2 (<number_of_commits>):
    Commit Message 1
    Commit Message 2
    ...

To simply see the number of commits and suppress the commit description, pass in the summary option:

Source example · PDF page 22
-s
--summary

$ git shortlog -s
<number_of_commits> Committer 1
<number_of_commits> Committer 2

To sort the output by number of commits instead of alphabetically by committer name, pass in the numbered option:

Source example · PDF page 22
-n
--numbered

To add the email of a committer, add the email option:

Source example · PDF page 22
-e
--email

A custom format option can also be provided if you want to display information other than the commit subject:

Source example · PDF page 22
--format

This can be any string accepted by the --format option of git log.

See Colorizing Logs above for more information on this.

Section 2.7: Searching commit string in git log

Searching git log using some string in log:

Source example · PDF page 22
git log [options] --grep "search_string"

Example:

Source example · PDF page 22
git log --all --grep "removed file"

Will search for removed file string in all logs in all branches.

Starting from git 2.4+, the search can be inverted using the --invert-grep option.

Example:

Source example · PDF page 22
git log --grep="add file" --invert-grep

Will show all commits that do not contain add file.

Section 2.8: Log for a range of lines within a file

Source example · PDF page 23
$ git log -L 1,20:index.html
commit 6a57fde739de66293231f6204cbd8b2feca3a869
Author: John Doe <john@doe.com>
Date:   Tue Mar 22 16:33:42 2016 -0500

    commit message

diff --git a/index.html b/index.html
--- a/index.html
+++ b/index.html
@@ -1,17 +1,20 @@
 <!DOCTYPE HTML>
 <html>
-       <head>
-        <meta charset="utf-8">
+
+<head>
+    <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">

Section 2.9: Filter logs

Source example · PDF page 23
git log --after '3 days ago'

Specific dates work too:

Source example · PDF page 23
git log --after 2016-05-01

As with other commands and flags that accept a date parameter, the allowed date format is as supported by GNU date (highly flexible).

An alias to --after is --since.

Flags exist for the converse too: --before and --until.

You can also filter logs by author. e.g.

Source example · PDF page 23
git log --author=author

Section 2.10: Log with changes inline

To see the log with changes inline, use the -p or --patch options.

Source example · PDF page 23
git log --patch

Example (from Trello Scientist repository)

Source example · PDF page 23
ommit 8ea1452aca481a837d9504f1b2c77ad013367d25
Author: Raymond Chou <info@raychou.io>
Date:   Wed Mar 2 10:35:25 2016 -0800

    fix readme error link
diff --git a/README.md b/README.md
index 1120a00..9bef0ce 100644
--- a/README.md
+++ b/README.md
@@ -134,7 +134,7 @@ the control function threw, but *after* testing the other functions and
readying
 the logging. The criteria for matching errors is based on the constructor and
 message.

-You can find this full example at [examples/errors.js](examples/error.js).
+You can find this full example at [examples/errors.js](examples/errors.js).

 ## Asynchronous behaviors

commit d3178a22716cc35b6a2bdd679a7ec24bc8c63ffa
:

Section 2.11: Log showing commited files

Source example · PDF page 24
git log --stat

Example:

Source example · PDF page 24
commit 4ded994d7fc501451fa6e233361887a2365b91d1
Author: Manassés Souza <manasses.inatel@gmail.com>
Date:   Mon Jun 6 21:32:30 2016 -0300

    MercadoLibre java-sdk dependency

 mltracking-poc/.gitignore |  1 +
 mltracking-poc/pom.xml    | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

commit 506fff56190f75bc051248770fb0bcd976e3f9a5
Author: Manassés Souza <manasses.inatel@gmail.com>
Date:   Sat Jun 4 12:35:16 2016 -0300

    [manasses] generated by SpringBoot initializr

 .gitignore                                                                            |  42
++++++++++++
 mltracking-poc/mvnw                                                                   | 233
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mltracking-poc/mvnw.cmd                                                               | 145
+++++++++++++++++++++++++++++++++++++++
 mltracking-poc/pom.xml                                                                |  74
++++++++++++++++++++
 mltracking-poc/src/main/java/br/com/mls/mltracking/MltrackingPocApplication.java      |  12 ++++
 mltracking-poc/src/main/resources/application.properties                              |   0
 mltracking-poc/src/test/java/br/com/mls/mltracking/MltrackingPocApplicationTests.java |  18 +++++
 7 files changed, 524 insertions(+)

Section 2.12: Show the contents of a single commit

Using git show we can view a single commit

Source example · PDF page 24
git show 48c83b3
git show 48c83b3690dfc7b0e622fd220f8f37c26a77c934

Example

Source example · PDF page 25
commit 48c83b3690dfc7b0e622fd220f8f37c26a77c934
Author: Matt Clark <mrclark32493@gmail.com>
Date:   Wed May 4 18:26:40 2016 -0400

    The commit message will be shown here.

diff --git a/src/main/java/org/jdm/api/jenkins/BuildStatus.java
b/src/main/java/org/jdm/api/jenkins/BuildStatus.java
index 0b57e4a..fa8e6a5 100755
--- a/src/main/java/org/jdm/api/jenkins/BuildStatus.java
+++ b/src/main/java/org/jdm/api/jenkins/BuildStatus.java
@@ -50,7 +50,7 @@ public enum BuildStatus {

                        colorMap.put(BuildStatus.UNSTABLE, Color.decode( "#FFFF55" ));
-                       colorMap.put(BuildStatus.SUCCESS,  Color.decode( "#55FF55" ));
+                       colorMap.put(BuildStatus.SUCCESS,  Color.decode( "#33CC33" ));
                        colorMap.put(BuildStatus.BUILDING, Color.decode( "#5555FF" ));

Section 2.13: Git Log Between Two Branches

git log master..foo will show the commits that are on foo and not on master. Helpful for seeing what commits you've added since branching!

Section 2.14: One line showing commiter name and time since commit

Source example · PDF page 25
tree = log --oneline --decorate --source --pretty=format:'"%Cblue %h %Cgreen %ar %Cblue %an
%C(yellow) %d %Creset %s"'  --all --graph

example

Source example · PDF page 25
*    40554ac  3 months ago  Alexander Zolotov    Merge pull request #95 from
gmandnepr/external_plugins
|\
| *  e509f61  3 months ago  Ievgen Degtiarenko    Documenting new property
| *  46d4cb6  3 months ago  Ievgen Degtiarenko    Running idea with external plugins
| *  6253da4  3 months ago  Ievgen Degtiarenko    Resolve external plugin classes
| *  9fdb4e7  3 months ago  Ievgen Degtiarenko    Keep original artifact name as this may be
important for intellij
| *  22e82e4  3 months ago  Ievgen Degtiarenko    Declaring external plugin in intellij section
|/
*  bc3d2cb  3 months ago  Alexander Zolotov    Ignore DTD in plugin.xml
Buy me a coffee
Daily Practice
0/0