How to Filter the Commit History in Git

John Wachira Mar 11, 2025 Git Git Log
  1. Using Git Log with Options
  2. Filtering by Commit Message
  3. Using Interactive Rebase for Commit Filtering
  4. Conclusion
  5. FAQ
How to Filter the Commit History in Git

When working with Git, understanding how to filter your commit history can be a game-changer. Whether you’re trying to track down a specific change, analyze project evolution, or simply clean up your view, filtering commits allows you to focus on what matters most. This guide will walk you through various methods to filter the commit history in Git, helping you pick out specific commits with ease. By mastering these techniques, you’ll enhance your version control skills and streamline your workflow. Let’s dive in and explore how to effectively filter commits using Git commands.

Using Git Log with Options

One of the most common ways to filter your commit history is by using the git log command with specific options. This command allows you to customize the output according to your needs. For example, you can filter by author, date, or even specific keywords in the commit messages.

Here’s how you can filter commits by author:

Bash
 bashCopygit log --author="Author Name"

Output:

 textCopycommit 1234567890abcdef1234567890abcdef12345678
Author: Author Name <author@example.com>
Date:   Mon Oct 1 12:34:56 2023 +0000

    Commit message from Author Name

By using the --author flag, you can narrow down the commits to those made by a specific person. This is particularly useful when you want to review contributions from a team member or identify changes made by yourself.

You can also filter commits by date. For example, if you want to see commits made in the last week, you can use:

Bash
 bashCopygit log --since="1 week ago"

Output:

 textCopycommit 1234567890abcdef1234567890abcdef12345678
Author: Author Name <author@example.com>
Date:   Mon Oct 1 12:34:56 2023 +0000

    Recent commit message

The --since option allows you to specify a starting point for your commits, making it easy to focus on recent activity. You can also use --until to specify an end date, giving you complete control over the time frame of your commit history.

Filtering by Commit Message

Sometimes, the commit message itself holds the key to filtering the history. If you’re looking for specific keywords or phrases within your commit messages, you can use the --grep option with git log. This is particularly useful for tracking down features or bug fixes that were described in the commit messages.

Here’s how you can filter commits by a keyword:

Bash
 bashCopygit log --grep="fix"

Output:

 textCopycommit 234567890abcdef234567890abcdef23456789
Author: Author Name <author@example.com>
Date:   Tue Oct 2 12:34:56 2023 +0000

    Fix issue with user authentication

Using the --grep option, you can quickly find all commits that mention the word “fix”. You can also combine this with other options, like --author, to further refine your search. For example:

Bash
 bashCopygit log --author="Author Name" --grep="fix"

Output:

 textCopycommit 345678901abcdef345678901abcdef34567890
Author: Author Name <author@example.com>
Date:   Wed Oct 3 12:34:56 2023 +0000

    Fix another issue related to user authentication

This combination allows you to pinpoint exactly what changes a specific author made concerning a particular issue. It’s a powerful way to track down relevant changes in your project.

Using Interactive Rebase for Commit Filtering

If you want to filter commits in a more hands-on way, you can use the interactive rebase feature in Git. This method allows you to view your commit history and selectively edit or remove commits. It’s particularly useful for cleaning up your commit history before merging changes into a main branch.

To start an interactive rebase, run:

Bash
 bashCopygit rebase -i HEAD~n

Replace n with the number of commits you want to view. For example, if you want to see the last five commits:

Bash
 bashCopygit rebase -i HEAD~5

Output:

 textCopypick 1234567 Commit message 1
pick 2345678 Commit message 2
pick 3456789 Commit message 3
pick 4567890 Commit message 4
pick 5678901 Commit message 5

In the interactive rebase interface, you’ll see a list of commits. You can change pick to edit or drop to modify or remove the commit. This gives you fine-grained control over your commit history.

After making your changes, save and close the editor. Git will apply your changes, and you can continue the rebase process. This method is powerful but should be used with caution, especially in shared repositories.

Conclusion

Filtering the commit history in Git is an essential skill for any developer. Whether you’re narrowing down commits by author, message, or date, these techniques can significantly enhance your productivity and understanding of your project’s evolution. By mastering these Git commands, you’ll be better equipped to manage your codebase and collaborate effectively with others. So, the next time you need to sift through your commit history, remember these methods and make your workflow smoother and more efficient.

FAQ

  1. What is the purpose of filtering commit history in Git?
    Filtering commit history helps you focus on specific changes, track contributions, and analyze project evolution.

  2. Can I filter commits by multiple criteria?
    Yes, you can combine options like --author and --grep to filter commits by both author and commit message.

  3. Is it safe to use interactive rebase in shared repositories?
    Use interactive rebase with caution in shared repositories, as it rewrites commit history and can affect collaborators.

  1. How do I undo a rebase if something goes wrong?
    You can use git rebase --abort to cancel the rebase process and return to the original state.

  2. Are there any GUI tools for filtering commit history?
    Yes, many Git GUI clients offer visual ways to filter and browse commit history, making it easier for users who prefer a graphical interface.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
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