The Git Log tool allows you to view information about previous commits that have occurred in a project.
The simplest version of the log command shows the commits that lead up to the state of the currently checked out branch.
These commits are shown in reverse chronological order (the most recent commits first).
1. Display All Commits
You can force the log tool display all commits (regardless of the branch checked out) by using the –all option.
$ git log --all commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: John Doe <JDoe@example.com> Date: Mon Mar 24 10:14:08 2014 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. . . .
2. View n Most Recent Commits
The real power of the Git Log tool, however, is in its diversity. There are many options that not only allow you to filter commits to almost any granularity you desire, but to also tailor the format of the output to you personal needs.
The most trivial way to filter commits is to limit output to the ‘n’ most recently committed ones. This can be accomplished using the -<n> option. Replace <n> with the number of commits you would like to see. i.e. To see the 3 most recent commits:
$ git log -3 commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: John Doe <JDoe@example.com> Date: Mon Mar 24 10:14:08 2014 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. commit 3ca28cfa2b8ea0d765e808cc565e056a94aceaf5 Author: Bobby Jones <BJones@example.com> Date: Mon Mar 24 01:52:04 2014 -0300 Fermentum magnis facilisis torquent platea sapien hac, aliquet torquent ad netus risus.
3. Filter Commits By Author or Committer
Another common way to filter commits is by the person who wrote or committed the changes. This can be done using the –author and –committer options. The syntax is
git log --author <name> git log --committer <name>
The author option will limit results to commits in which the changes were written by <name>. The –committer option, on the other hand, will limit results to commits that were committed by that individual. Many times, the author and committer will be the same person (you would generally expect this to be the case) but, in the case where a developer submits a patch of their work for approval, the developer may not actually commit the code.
$ git log --author=Bob commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. commit 3ca28cfa2b8ea0d765e808cc565e056a94aceaf5 Author: Bobby Jones <BJones@example.com> Date: Mon Mar 24 01:52:04 2014 -0300 Fermentum magnis facilisis torquent platea sapien hac, aliquet torquent ad netus risus. commit cfc101ad280f5b005c8d49c91e849c6c40a1d275 Author: Bob Smith <BSmith@example.com> Date: Thu Mar 20 10:31:22 2014 -0300 Natoque, turpis per vestibulum neque nibh ullamcorper. . . .
Note: Notice how this option matches any commit in which the <name> we specify is a substring match of the commit’s author (We’ve found both Bob Smith’s and Bobby Jones’ commits).
4. Filter Commits by X Days Ago
Many times you’ll want to limit the commits to those within a given date range. This can be accomplished using the –before and –after options:
git log --before <date> git log --after <date>
The date can be specified as a string with the format: “yyyy-mm-dd”. Git will also accept Ruby expressions as arguments here, so you can do things like the following to see commits that occurred in the last 2 days
$ git log --after 2.days.ago commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: John Doe <JDoe@example.com> Date: Mon Mar 24 10:14:08 2014 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. commit 3ca28cfa2b8ea0d765e808cc565e056a94aceaf5 Author: Bobby Jones <BJones@example.com> Date: Mon Mar 24 01:52:04 2014 -0300 Fermentum magnis facilisis torquent platea sapien hac, aliquet torquent ad netus risus.
5. Filter Commits by Date Range
To specify a date range, use both options:
git log --after <date> --before <date>
To see the commits that occurred on Feb 2nd, 2014:
$ git log --after "2014-02-01" --before "2014-02-02" commit 69e1684ae9605544707fc36a7bf37da93dc7b015 Author: Bob Smith <BSmith@example.com> Date: Sun Feb 2 01:26:00 2014 -0400 Praesent tempus varius vel feugiat mi tempor felis parturient.
6. View All Diff of Changes for Each Commit
Options to Modifying format of the output: To view the entire diff of changes for each commit found, use the -p option (think ‘p’ for patch):
$ git log -p commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. diff --git a/foo.txt b/foo.txt index 5554f5b..2773ba4 100644 --- a/foo.txt +++ b/foo.txt @@ -436,3 +436,4 @@ Fermentum mollis. Lacus fermentum nonummy purus amet aliquam taciti fusce facilisis magna. Viverra facilisi curae augue. Purus ve nunc mi consectetuer cras. +Ad, maecenas egestas viverra blandit odio. commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: John Doe <JDoe@example.com> Date: Mon Mar 24 10:14:08 2014 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. diff --git a/foo.txt b/foo.txt index 9cdef98..5554f5b 100644 --- a/foo.txt +++ b/foo.txt @@ -435,3 +435,4 @@ Lacinia et enim suspendisse conubia lacus. Fermentum mollis. Lacus fermentum nonummy purus amet aliquam taciti fusce facilisis magna. Viverra facilisi curae augue. +Purus ve nunc mi consectetuer cras. . . .
7. View Summary of Changes for Each Commit
To view a summary of the changes made in each commit (# of lines added, removed, etc), use the –stat option:
$ git log --stat commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Bob Smith <BSmith@example.com> Date: Tue Mar 25 22:09:26 2014 -0300 Ut sit. foo.txt | 1 + 1 file changed, 1 insertion(+) commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: John Doe <JDoe@example.com> Date: Mon Mar 24 10:14:08 2014 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. foo.txt | 1 + 1 file changed, 1 insertion(+)
8. View Just One Line Per Commit
To just get the bare minimum information in a single line per commit, use the –oneline option. Each commit will be shown as simply the commit hash followed by the commit message, on a single line:
$ git log --oneline c36d210 Ut sit. 97eda7d Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. 3ca28cf Fermentum magnis facilisis torquent platea sapien hac, aliquet torquent ad netus risus. 3a96c1e Proin aenean vestibulum sociosqu vitae platea, odio, nisi habitasse at, in lorem odio varius. 1f0548c Nulla odio feugiat, id, volutpat litora, adipiscing. cfc101a Natoque, turpis per vestibulum neque nibh ullamcorper. . . .
9. View Commit History in ASCII Graph
The Git Log tool can also display the commit history in an ascii art graphical representation with the –graph option. This option works well when combined with the –oneline option mentioned above.
$ git log --graph * commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 | Author: Bob Smith <BSmith@example.com> | Date: Tue Mar 25 22:09:26 2014 -0300 | | Ut sit. | * commit 97eda7d2dab729eda23eefdc14336a5644e3c748 | Author: John Doe <JDoe@example.com> | Date: Mon Mar 24 10:14:08 2014 -0300 | | Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio. | * commit 3ca28cfa2b8ea0d765e808cc565e056a94aceaf5 | Author: Bobby Jones <BJones@example.com> | Date: Mon Mar 24 01:52:04 2014 -0300 | | Fermentum magnis facilisis torquent platea sapien hac, aliquet torquent ad netus risus. . . .
10. Format the Git Log Output
To take complete control over the format of the output, use the –pretty option. This can be extremely useful if you are using the output of the log tool for further reporting. The syntax of this option is:
git log --pretty=format:"<options>"
The <options> are specified in a similar way as formatted strings are in many languages. For example:
$ git log --pretty=format:"Commit Hash: %H, Author: %aN, Date: %aD" Commit Hash: c36d2103222cfd9ad62f755fee16b3f256f1cb21, Author: Bob Smith, Date: Tue, 25 Mar 2014 22:09:26 -0300 Commit Hash: 97eda7d2dab729eda23eefdc14336a5644e3c748, Author: John Doe, Date: Mon, 24 Mar 2014 10:14:08 -0300 Commit Hash: 3ca28cfa2b8ea0d765e808cc565e056a94aceaf5, Author: Bobby Jones, Date: Mon, 24 Mar 2014 01:52:04 -0300 Commit Hash: 3a96c1ed29e85f1a119ad39033511413aad616d1, Author: John Doe, Date: Sun, 23 Mar 2014 06:05:49 -0300 Commit Hash: 1f0548cc700988903380b8ca40fd1fecfa50347a, Author: John Doe, Date: Fri, 21 Mar 2014 17:53:49 -0300 . . .
For a full list of the available formatting options available, see the man page for the Git Log tool, or visit the online documentation here.
git help log
The final thing to note is that you can combine these options in almost anyway you see fit. This allows you to not only customize the queries you perform, but also how the results are displayed.
Comments on this entry are closed.
Thanks!!! Great article.
Please share any graphical front end to make the output especially code diffs more user friendly.
Hi,
Thanks a lot, very nice article