Before you go, check out these stories!

0
Hackernoon logoTen Useful Git Log Tricks by@srebalaji

Ten Useful Git Log Tricks

Author profile picture

@srebalajiSrebalaji

If you are using Git for a while you should have come across git log. As everyone knows, the git log is a simple command that helps us to
view the changes or project history.

Even with that simplicity, it is a very powerful tool and it comes with numerous options that help us to view the project changes and its structure. We will see some of the most used options in this article.

git log โ€”oneline

git log --oneline

This command helps you to view the commits in a cleaner way. It condenses each commit to a line and has only minimal information like shorter commit hash, commit message.

Filter commits by time period

These commands will filter out the commits by the given time period. For example, โ€” after will only filter commits after the given time period and โ€” before will only filter commits before the given time period.

git log --after="2020-15-05"

The above command will show only commits after May 15th, 2020

git log --after="2020-15-05" --before="2020-25-05"

The above command will show only commits from May 15 to May 25

You can also use the following date formats

git log --after="yesterday" // shows only commits from yeserday

git log --after="today" // shows only today commits

git log --before="10 day ago" // omits last 10 days commits

git log --after="1 week ago" //show only commits from last week

git log --after="2 week ago"

git log --after="2 month ago" // shows only last 2 months commits

git log with diff changes

git log -p

This command will show the log with the diff changes. So that you can know the changes done in each commit.

In the above image, you can see the git diff changes.

Filter commits by author

git log --author="Srebalaji"

The above command will filter out the commits done by the particular
author. Note that the Git filters out by regex pattern. So donโ€™t worry
about the exact name match or case sensitivity.

Git log can take multiple options so you can combine options for your need. For example,

git log --after="1 week ago" --author="srebalji" -p

The above command will filter commits for the past week by the respective author and also shows the diff changes.

Filter commits by log messages

Sometimes, you need to filter commits by log messages. Git accepts a
regex pattern to search for the log messages and displays all the
matched commits.

git log --grep="ISSUE-43560"

The above command will filter commits by the respective pattern. And remember by default itโ€™s case sensitive.

To make the search case insensitive, you can pass -i parameter

git log -i --grep="issue-43560"

The below command is using a regex pattern search and will search for both the issue ids.

git log -i --grep="issue-43560\|issue-89786"

Filter commits by files

Sometimes you need all commits changes that have affected some particular files. This will come in hand in many places.

git log main.rb

This command will filter commits that made changes to the respective file.

You can also pass multiple files to it.

git log main.rb search.rb login.rb

You can see I have passed three files to filter out.

Remember you can also pass multiple options.

git log -i --grep="fix " main.rb search.rb

This command will filter out commits changes done to the specified files and also will match the log message by the given search pattern.

Filter commits by file content

You may need to search for a specific string in the source code that has been added in the commit history. This can be possible by

git log -S"function login()"

The above command will search for the string โ€œfunction login()โ€œ. By default, itโ€™s case sensitive.

You can make it case-insensitive by adding -i. And to view the content you can view the diff changes.

git log -i -S"function login()" -p

Show only merge commits

This command helps us in knowing the merges done to the current branch.

git log --merges

The above command will show only the merge commits in the current branch. Nothing more.

Showing diff between branches

We have already seen this command in one of our previous issues.

git log master..develop

This command will help you show all the commits from develop but that
are not present in the master branch. In this way, you can know that how
many new commits are added to the develop branch that is not present in
the master branch. And make sure you have the updated changes in the
local before comparing.

Custom formatting log messages

Git also provides options to custom format our log messages. You can check out custom pretty options for more options.

For example,

git log --pretty=format:"%Cred%an - %ar%n %Cblue %h -%Cgreen %s %n"

You can see in the above image that the commit logs are custom
formatted. Itโ€™s pretty easy and it comes in handy if you want to view
only specific details of the log.

That's it. Hope you learned something new :)

Thank you for reading :) :)

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Previously published at https://gitbetter.substack.com/p/useful-tricks-you-might-not-know

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.