How To List Commit History with Git Log Command with Examples? – POFTUT

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.

List Commit History

List Commit History

We can see from output that following information about the commit provided.

  • Commit number which is a unique hash identifies the commit
  • Author the developer who commit. Also email information is provided
  • Date specifies 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.

List One Commit Per Line

List One Commit Per Line

Print Statistics

We may need to print information about the commit in details. We will use --stat option.

Print Statistics 

Print Statistics

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. -p option can be used to print path or diff of the files for the commits.

LEARN MORE  How To Clone Git Branch Command Tutorial with Examples?

Print Patch or Diff Information

Print Patch or Diff Information

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 - minus.

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.

Print Specific Commit In Detail

Print Specific Commit In Detail

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.

Show/Print Specific Commit Stats

Show/Print Specific Commit Stats

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.

Group Commits By Author

Group Commits By Author

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.

Show Author Commit Numbers

Show Author Commit Numbers

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.

LEARN MORE  Git Status - Show The Working Tree Status

Sort Authors By Commit Numbers

Sort Authors By Commit Numbers

Pretty Print

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.

Pretty Print

Pretty Print

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 dmiller.

Filter By Author

Filter By Author

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.

Filter By Number

Filter By Number

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 .

Filter By Date

Filter By Date

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.

LEARN MORE  How to Install Git on Linux, Mac or Windows?

Filter By Message

Filter By Message

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.

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.

Filter By Content

Filter By Content

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.

Filter By Commit Id/Hash Range

Filter By Commit Id/Hash Range

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)