How to Filter the Commit History in Git

John Wachira Feb 02, 2024
  1. Filter the Commit History by Amount
  2. Filter the Latest Commits
  3. Filter the Commit History by Date
  4. Filter the Commit History by Author
  5. Filter the Commit History by Multiple Users
  6. Exclude Commits From a Specific User
How to Filter the Commit History in Git

This article discusses the various commands you can use in Git to filter your commit history. We use the git log command to check the commit history in our repositories.

You can use several combinations with the git log command to format the output, as we will see below.

You can filter the commit history by the following.

Filter the Commit History by Amount

Using the command below, you can limit the number of commits displayed by the git log command.

$ git log -4

The command above will output the recent four commits in our repository.

Filter the Latest Commits

To filter the latest commits, you only need to specify how many. If we want the latest six commits, we can run:

$ git log -6

Filter the Commit History by Date

You can specify a time frame using the --after and --before flags. The flags accept a variety of formats, as we will see below.

$ git log --after="2022-7-27"

This command only displays the commits we created after July 27th, 2022. We can pass relative references as shown below.

$ git log --after="yesterday"

You can use both --before and --after as illustrated in the example below.

$ git log --after="2022-7-20" --before="2022-7-25"

You can use --since and --until in place for --after and --before, respectively.

Filter the Commit History by Author

We can display commits from a specific author by adding the --author flag to our git log command, as shown below.

$ git log --author="John"

The command above will display commits whose author has the phrase John in their name. You can make complex searches like the one below.

Filter the Commit History by Multiple Users

You can filter your commit history by multiple users using the command below.

$ git log --author="John\|Ann"

Such a command will display commits whose author has the phrases John or Ann in their names.

Exclude Commits From a Specific User

Here is an example command.

$ git log --perl-regexp --author='^((?!Chris).*)$'

The command above will display all the commits whose author name does not have Chris.

In conclusion, the above are the most common filtering options we use. However, there are still other parameters you can feed to your git log command to filter out the output.

You can also filter by message, range, file, and content.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Log