Git - Viewing the Commit History
Git - Viewing the Commit History
These examples use a very simple project called “simplegit”. To get the project, run:
When you run git log in this project, you should get output that looks something like this:
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 16:40:33 2008 -0700
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 10:31:28 2008 -0700
Initial commit
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.
A huge number and variety of options to the git log command are available to show you exactly what you’re
looking for. Here, we’ll show you some of the most popular.
One of the more helpful options is -p or --patch, which shows the difference (the patch output) introduced in
each commit. You can also limit the number of log entries displayed, such as using -2 to show only the last two
entries.
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 5/11
6/27/23, 2:00 AM Git - Viewing the Commit History
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 16:40:33 2008 -0700
end
-
-if $0 == __FILE__
- git = SimpleGit.new
- puts git.show
-end
This option displays the same information but with a diff directly following each entry. This is very helpful for
code review or to quickly browse what happened during a series of commits that a collaborator has added. You
can also use a series of summarizing options with git log. For example, if you want to see some abbreviated
stats for each commit, you can use the --stat option:
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
Rakefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 16:40:33 2008 -0700
lib/simplegit.rb | 5 -----
1 file changed, 5 deletions(-)
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 6/11
6/27/23, 2:00 AM Git - Viewing the Commit History
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 10:31:28 2008 -0700
Initial commit
README | 6 ++++++
Rakefile | 23 +++++++++++++++++++++++
lib/simplegit.rb | 25 +++++++++++++++++++++++++
3 files changed, 54 insertions(+)
As you can see, the --stat option prints below each commit entry a list of modified files, how many files were
changed, and how many lines in those files were added and removed. It also puts a summary of the information
at the end.
Another really useful option is --pretty. This option changes the log output to formats other than the default. A
few prebuilt option values are available for you to use. The oneline value for this option prints each commit on a
single line, which is useful if you’re looking at a lot of commits. In addition, the short, full, and fuller values
show the output in roughly the same format but with less or more information, respectively:
The most interesting option value is format, which allows you to specify your own log output format. This is
especially useful when you’re generating output for machine parsing — because you specify the format
explicitly, you know it won’t change with updates to Git:
Useful specifiers for git log --pretty=format lists some of the more useful specifiers that format takes.
%H Commit hash
%T Tree hash
%P Parent hashes
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 7/11
6/27/23, 2:00 AM Git - Viewing the Commit History
%s Subject
You may be wondering what the difference is between author and committer. The author is the person who
originally wrote the work, whereas the committer is the person who last applied the work. So, if you send in a
patch to a project and one of the core members applies the patch, both of you get credit — you as the author, and
the core member as the committer. We’ll cover this distinction a bit more in Distributed Git.
The oneline and format option values are particularly useful with another log option called --graph. This option
adds a nice little ASCII graph showing your branch and merge history:
$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 Ignore errors from SIGCHLD on trap
* 5e3ee11 Merge branch 'master' of https://github.com/dustin/grit.git
|\
| * 420eac9 Add method for getting the current branch
* | 30e367c Timeout code and tests
* | 5a09431 Add timeout protection to grit
* | e1193f8 Support for heads with slashes in them
|/
* d6016bc Require time for xmlschema
* 11d191e Merge branch 'defunkt' into local
This type of output will become more interesting as we go through branching and merging in the next chapter.
Those are only some simple output-formatting options to git log — there are many more. Common options to
git log lists the options we’ve covered so far, as well as some other common formatting options that may be
useful, along with how they change the output of the log command.
Option Description
--shortstat Display only the changed/insertions/deletions line from the --stat command.
--name-only Show the list of files modified after the commit information.
--name-status Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit Show only the first few characters of the SHA-1 checksum instead of all 40.
Display the date in a relative format (for example, “2 weeks ago”) instead of using the
--relative-date
full date format.
--graph Display an ASCII graph of the branch and merge history beside the log output.
Show commits in an alternate format. Option values include oneline, short, full,
--pretty
fuller, and format (where you specify your own format).
However, the time-limiting options such as --since and --until are very useful. For example, this command
gets the list of commits made in the last two weeks:
$ git log --since=2.weeks
This command works with lots of formats — you can specify a specific date like "2008-01-15", or a relative date
such as "2 years 1 day 3 minutes ago".
You can also filter the list to commits that match some search criteria. The --author option allows you to filter
on a specific author, and the --grep option lets you search for keywords in the commit messages.
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 9/11
6/27/23, 2:00 AM Git - Viewing the Commit History
You can specify more than one instance of both the --author and --grep search criteria, which will limit
the commit output to commits that match any of the --author patterns and any of the --grep patterns;
Note
however, adding the --all-match option further limits the output to just those commits that match all --
grep patterns.
Another really helpful filter is the -S option (colloquially referred to as Git’s “pickaxe” option), which takes a
string and shows only those commits that changed the number of occurrences of that string. For instance, if you
wanted to find the last commit that added or removed a reference to a specific function, you could call:
$ git log -S function_name
The last really useful option to pass to git log as a filter is a path. If you specify a directory or file name, you
can limit the log output to commits that introduced a change to those files. This is always the last option and is
generally preceded by double dashes (--) to separate the paths from the options:
$ git log -- path/to/file
In Options to limit the output of git log we’ll list these and a few other common options for your reference.
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
Only show commits in which the author entry matches the specified
--author
string.
Only show commits in which the committer entry matches the specified
--committer
string.
--grep Only show commits with a commit message containing the string.
For example, if you want to see which commits modifying test files in the Git source code history were
committed by Junio Hamano in the month of October 2008 and are not merge commits, you can run something
like this:
$ git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link HEAD
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 10/11
6/27/23, 2:00 AM Git - Viewing the Commit History
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix "checkout --track -b newbranch" on detached HEAD
b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch
Of the nearly 40,000 commits in the Git source code history, this command shows the 6 that match those criteria.
Depending on the workflow used in your repository, it’s possible that a sizable percentage of the commits in
Tip
your log history are just merge commits, which typically aren’t very informative. To prevent the display of
merge commits cluttering up your log history, simply add the log option --no-merges.
prev | next
About this site
Patches, suggestions, and comments are welcome.
Git is a member of Software Freedom Conservancy
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History 11/11