5) Git & GitHub Tips & Tricks Lesson

Git Log

14 min to complete · By CodingNomads Team

The central feature of a version control system is to record every little change in your repository. The git log command gives you access to your records.

This lesson discusses the information you can retrieve with the git log command and the different options for using it.

How Git Log Works

Git log allows you to review the history of everything that's happened to a repository. You can browse through the history of your work on a per-change basis rather than needing to surf through the whole source code to find a specific piece of code.

What Git Log Can Retrieve

git log allows you to retrieve information such as:

  • Commit History
  • Bugs
  • Author Information
  • Contributors
  • Updates
  • and More

The git log command is your primary window to this information, because it displays the record of your project's commits. And you don't necessarily have to dive deeply. git log is also a great feature to take a quick look at the recent commits, read their commit message, or fetch their commit hash.

What Git Log Displays

Running git log will display you with the following information for each commit:

  • Commit Hash
  • Author Metadata, such as name and email address
  • Date and Time of the commit
  • Commit Message

By gaining access to the commit hash, you unlock the full power of checking out your project at that particular point in history, which opens up the portal to any other information that you want to check up on.

How Git Log Looks in Practice

When you use the git log command, Git will display the most recent commits and the status of your repository's HEAD:

$ git log

The command lists all the latest commits in the repo's history. Each of these commits contains a unique ID, the commit hash, that Git created with the help of a hashing algorithm. These hashes help to identify each commit uniquely.

The output also includes some metadata, such as author name, email address, date, and time:

Git Log Output

This certainly is a lot of information, and a lot of whitespace as well. How can you display it a little more condensed?

Git Log Command Options

Below are the different ways you can use the git log command.

git log --oneline: Display Commit Information in One Line

You can make the information for each commit fit into a single line. This way, you can view deeper into your repo's history at once:

$ git log --oneline

You can also pass the oneline flag to another flag, called --pretty to display the output neatly:

$ git log --pretty=oneline
Git Log Pretty OneLine

Depending on the CLI shell you are working with, some of the color highlighting and formatting may look different for you.

git log --graph: Display a Graphical History

Despite the fact that Git is a CLI tool, it has some nice visual options already built in. You can show a graph of the commits and how they are related with the following command:

$ git log --graph

The --graph option draws a text-based graph showing the branch structures of the repository's commit history. The output makes it easier to understand which commit belongs to which branch, as well as how the branches are related to each other:

Git Log Graph

You can also modify how the graph is displayed by adding two options:

  • --oneline
  • --decorate
$ git log --graph --oneline --decorate

This will make it easier to see a graph, especially if the history has a lot of commits:

Git Log Graph Decorated and Condensed

As you can see, Git provides some great options that can help you to visualize your project's history, and get a better understanding of how commits and branches are related to each other.

git log --stat: Display Information About Modified Files

The git log --stat command shows which files were modified in each commit. Additionally, git log --stat shows the number of lines that have been edited, and a summary line of the total records that have been updated or modified in this commit.

Let's give it a try:

$ git log --stat

Running this command on your repo will show you some output similar to the one shown below:

Git Log Stat Output

You can notice that it is easy to get a quick overview of the changes by viewing:

  • Files: A list of all the files that have been changed in this commit
  • Lines: The number of lines that have been added or removed from each file
  • Stats: A summary of the total number of records changed across the whole commit

git log --patch: Display Patch Information

The git log --patch command displays the patch representation of each of the commits:

$ git log --patch
# or
$ git log -p

The output will show you the location of the lines that have been added or removed in the files. It's a way to view the changes that were made more specifically.

Checking a Specific Branch with Git Log

You can also use git log to check the commit history of a specific branch by adding the branch name to the git log command:

$ git log <branch name>

Git Log Filtering Options

If you want to display only specific types of information from your Git repo's history, you can do this with the help of filters.

Git Log Author

You can filter your history by author. Doing so will only show the commits made by the author you specified in the filter command:

$ git log --author "author name"

Git Log Grep Commit Message

To filter using the commit message, you can use the --grep option. It works like the author option, and will filter to only display commit messages that contain your search terms. You don't have to type the complete commit message for this filter to work:

$ git log --grep "commit message search term"

Git Log for Date & Time

You can also search by the date and time of a commit. For this, you need to pass any of the following arguments:

  • --after
  • --before
  • --since
  • --until

Following the flag, you need to pass a date in one of the supported formats, e.g.:

$ git log --after="yy-mm-dd"

You can also use multiple options together to search for commits between certain dates, e.g.:

$ git log --after="yy-mm-dd" --before="yy-mm-dd"

The date strings can be formatted in different ways. For example, you can also pass reference statements, such as "2 years ago", "12 days ago", and more. The command below would be a valid search string to filter your commits by date:

$ git log --after="11 days ago"

Git correctly interprets a surprising amount of different date and time entries. Play around with it and see if you can break it :)

Exiting Git Log Output

If you are viewing some output of a git log command that is too long to display on one page of your terminal, you might wonder how to exit that output screen. You can do so by pressing q, for quit, on your keyboard. This will bring you back to your main terminal.

For more information about git log, check out the Git Log documentation, as well as this guide on Viewing the commit history.

Summary: What is Git Log?

  • The git log command displays the records of all changes made to your repository - a central feature of a version control system.
  • You can use git log to check the commit history of a specific branch by adding the branch name to the git log command: git log <branch name>.
  • You can quit out of git log by pressing q, for quit, on your keyboard. This will bring you back to your main terminal.

Git Log allows you to retrieve information like:

  • Commit History
  • Bugs
  • Author Information
  • Contributors
  • Updates, and more