How to filter Git logs
Getting all commits
Getting the last n commits
We can get the last n commits by running “git log -n”. Let’s say we want to get the last 2 commits.
git log -2
Getting commits since a specific date
We can filter commits based on the date they were made. Let’s say we want to get all the commits made 01/17/2022.
git log --since 2022-01-17
Getting commits before a specific date
Similarly, we can get commits that were made before a specific date.
git log --until 2022-01-16
Getting commits by author
We can also get commits made by a specific author. For instance:
git log --author=pipinho13
Getting commits between two SHAs
We can get logs between two SHAs like this:
git log <XXX>...<YYY>
where XXX and YYY are the respective SHAs. Note that we can replace with HEAD if we need to get the commits from up to now, i.e.:
git log <XXX>...HEAD
Getting commits related to a file
We can filter logs by files. Suppose we want to get all the logs associated with the “README.md” file.
git log README.md
Getting commits using an expression
We can use the powerful “grep” command to filter commits. We usually look for some expression in the commit message. For example, let’s get the commits that contain the word “wrong” in the message:
git log --grep="wrong"