Filtering the history

When browsing the history of a repository we'll usually want to filter commits. There are several filters that we can use along with the git log command. For example, to see the last 3 commits we run

git log --online -3

To filter by author we use the --author option and pass the name of the author. We can short the name in an unambiguous way.

git log --oneline --author="Daniel"
Filtering the history 1
Filtering the history 2

We have several ways of filtering by date. We use the --before or --after options. As a value we can pass a date, or a relative date. So, for example, if we run

git log --oneline --before="2020-08-17"

Git will show us all commits done on or prior to August 17, 2020. When using relative dates there are multiple options that Git will understand. For example, if we run

git log --oneline --after="yesterday"

Git will show us all commits done yesterday or today. If we run

git log --oneline --after="two weeks ago"

Git will return all commits done in the past two weeks.

To find all commits that have a specific pattern in its message we use the --grep option. For example, if we want to find all commits that mention app.py we run

git log --oneline --grep="app.py"

The --grep option will search for the pattern in the commit message. Keep in mind that the search is case sensitive.


Now suppose we want to find all commits that have added or removed the declaration of the hello() function. To achieve this we run

git log --oneline -S"hello()"

We can also add the --patch option to see what was done in each of the involved commits.

Filtering the history 3
Filtering the history 4

We can also filter the history by a specific range of commits. First we need to get the IDs for the two commits. Then we run

git log --oneline oldest_commit..newest_commit

Keep in mind that Git will look for commits greater than oldest_commit and less than or equal to newest_commit.

We can find all commits that touch a particular file or group of files. To do so we just pass the name or names of the files to git log. When doing this it's recommended to separate the last option passed to the git log command with -- so that Git does not confuse options from filenames. Also, keep in mind that all options must be supplied before the filenames.

Filtering the history 5