How To List Commit History with Git Log Command with Examples?
Git source code versioning tool provides a lot of features. One of the most important and useful features is
log or history. We can use
git log command in order to list, filter, view commit history in different ways. In this tutorial we will examine
git log command usage in detail with examples.
List Commit History
We will start with
git log command without any parameter. This will list all commit history in a interactive terminal where we can see and navigate.
$ git log
We can see from output that following information about the commit provided.
Commitnumber which is a unique hash identifies the commit
Authorthe developer who commit. Also email information is provided
Datespecifies when the commit occurred
- The last line provides note and information about the commit.
List One Commit Per Line
If we need to only list unique part of the commit id with the note provided by author we can use
--oneline option which will just print single line about each commit.
$ git log --oneline
We may need to print information about the commit in details. We will use
$ git log --stat
We can see from output that extra information like changed file, changed file count, number of lines added , number of lines deleted.
Print Patch or Diff Information
If we are interested with the code diff information we need to use
-p option can be used to print path or diff of the files for the commits.
$ git log -p
We see from screenshot that added and removed code is shown clearly. Added code color is green and removed code is red. Also added code lines start with
+plus and removed code lines starts with
Show/Print Specific Commit In Detail
If we need to look specific commit we need to use
git show command. We will also provide the commit id or number we can to print.
$ git show b1efd742499b00eef970feeef84dc64f301db61f
We can see that specific commit provides diff information in detail.
Show/Print Specific Commit Stats
If we cant to just print specific commit stat and information we can provide
--stat option to the
git show command.
$ git show --stat b1efd742499b00eef970feeef84dc64f301db61f
Group Commits By Author
If we want to inspect the commits according to the author name we need to group commits by author. We can use
shortlog command in order to list commits notes grouped by author name.
$ git shortlog
Show Author Commit Numbers
If we are interested with the authors commit numbers we need to provide
-s options to the
shortlog command.This will provides commit numbers for each authors.
$ git shortlog -s
Sort Authors By Commit Numbers
We can improve previous example and sort authors by their commit numbers. We will add
-n too the previous example where final command will be like below.
$ git shortlog -n -s
We can also customize the log output according to our needs. We can use
--pretty option and som parameters to print different log output. In this example we will use
%cn for author name
%h hash value of commit and
%cd for commit time.
$ git log --pretty="%cn committed %h on %cd"
Filter By Author
In some cases we may need to filter commits according to the author name. We will use
--author and provide the author name to filter and show only given author. In this example we will filter author named
$ git log --author="dmiller"
Filter By Number
If we want to list and print specified number of commit we need to use
- with the number we want to print. In this example we will print last 5 commit.
$ git log -5 --oneline
Filter By Date
We can also filter according to date. We will provide the date we want to start listing. We will use
--after option and provide the date. Date will be MM-DD-YYYY format. In this example we will list commits those created after 1 December 2018 .
$ git log --after="12-1-2018"
We can also use
--before where commits created before specified date will be printed.
Filter By Message
We can print or list logs by filtering according to the message. We will use
--grep option and provide the filter term. We will filter for message
http in this example.
$ git log --grep="http"
Filter By File
If we are looking for specific file change during commit we can filter for file. We will use
-- and provide file names which is expected to be in commit change. In this example we will look file
ip.c which is expected to be committed.
$ git log -- ip.c
Filter By Content
Also we can filter commits according to the commit content. This will be very useful if we want to search and filter for specific change. We will use
-S option and provide filter term. In this example we will filter for
raw_scan . Keep in mind that this may take some time because it will search in all commits which is not indexed for fast search.
$ git log -S"raw_scan"
Filter By Commit Id/Hash Range
Commits have their own hash ids. If we want to list range of commits we can provide the start and end commit id where commits between them will be listed.
$ git log b642dc129c4d349a849fb0e..1ba01193725f4c
List Only Merges
By default merge commits are printed and listed. But if the default behaivour is change with config and we want to list and print merge commits we can use
--merge option to list merge commits too.
$ git log --merge
List No Merges
By default merges commits are printed and listed with
git log command. If we do not want to list or print then for all operations we can use
--no-merges option which will do not show merge commits.
$ git log --no-merge