Sling Academy
Home/DevOps/Git: How to see commit history (git log)

Git: How to see commit history (git log)

Last updated: January 27, 2024

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.

Next Article: How to View, Create, and Switch Git Branches: A Complete Guide

Previous Article: Git: How to undo a ‘git add’ command (unstage files)

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide