Git: How to see commit history (git log)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the commit history of a project can provide insightful context, help trace changes made over time, and facilitate collaborative development efforts. Git, a distributed version control system, offers powerful tools to interrogate the trail of commits. One of the essential Git commands for this purpose is git log. This tutorial will guide you through using git log to view your commit history, starting with basic usage and leading up to more advanced techniques.

The First Look at git log

The git log command displays the commit logs. Each commit in Git is represented by a unique hash, a message describing the changes, the author’s information, and the date it was created. To get started, just type the following in your terminal:

git log

You will see an output similar to this:

commit 5fbeb4a1e8f9b1c2ae5b9a1feed7fbcd153c4e2d
Author: Alice Johnson <[email protected]>
Date:   Mon Mar 15 15:00:00 2023 -0400

    Fix typo in the introduction

Limiting Log Output

For repositories with extensive history, the full log can be overwhelming. You can limit the number of commit messages outputted with a simple parameter:

git log -n 5

This command will show only the last five commits:

commit 5fbeb4a1e8f9b1c2ae5b9a1feed7fbcd153c4e2d
Author: Alice Johnson <[email protected]>
Date:   Mon Mar 15 15:00:00 2023 -0400

    Fix typo in the introduction

... 4 more commits ...

Prettifying Log Output

Git provides a myriad of options to change the format of the log output. The --pretty option is especially handy. You can use built-in short-hands like oneline, short, full, and fuller, or a custom format:

git log --pretty=oneline

This produces each commit in a single line, which is a compact way to view the history.

5fbeb4a... Fix typo in the introduction
4ac9e32... Add user authentication feature
...

Let’s take a look at a custom format where we display the author’s name, the commit hash, and the date:

git log --pretty=format:"%h - %an, %ad : %s"

The output of the above command might look something like this:

5fbeb4a - Alice Johnson, Mon Mar 15 15:00:00 2023 -0400 : Fix typo in the introduction
4ac9e32 - Bob Smith, Sun Mar 14 12:35:00 2023 -0400 : Add user authentication feature
...

Filtering by Date

You can also filter logs by date using the --since and --until flags:

git log --since="2 weeks ago"

This command shows all commits from the last two weeks.

Filtering by Author

If you’re working in a team and want to see commits from a specific contributor, you can filter by author:

git log --author="Alice Johnson"

It will display commits made by Alice Johnson.

Filtering by Content

To find commits that introduced or removed certain content, you can use the -S flag:

git log -S"authentication login"

It will show commits where the given string was added or removed from the codebase.

Viewing Diffs for Commits

Sometimes it’s not enough to see the commit message, and you may want to see what changes were made. You can add the -p flag for this purpose:

git log -p -2

This shows the diffs for the last two commits.

Advanced: Using Graphs

When dealing with branches and merges, visualizing the project history can be very useful. The following command displays the history as a text-based graph:

git log --graph --oneline --decorate --all

This command will output a very informative graph showing all branches and tags alongside commit messages.

Advanced: Combining Filters

You can combine filtering options to refine your search. For example, to list commits by a particular author since a specific date, displaying each in a single line:

git log --author="Alice" --since="2023-01-01" --pretty=oneline

This narrowed down log will display Alice’s contributions after the start of the year 2023.

Conclusion

In this tutorial, we learned how to navigate through a repository’s history using git log with practical examples of how to use its many options. From basic querying to complex filtering and graphical visualization, git log is an essential tool in any developer’s Git arsenal. Mastering it will greatly improve your ability to understand and manage your code’s history.