Git is a wonderful piece of software that makes life easier and more productive for a programmer. But sadly most developers don’t use it up to their full potential so we are going to blog about various simple and advanced usages of git as a series. In the first article of the series, we are going to talk about git logs.

Git log is a great feature, that allows us to keep track of our works. There are different options available under git log command, which help us to customize the output of git log and also to filter the log.

Git Graphs

The –graph option draws a graph representing the branches and its structure of commit history. –oneline is used to display commit message and its hash in a single line, which is used along with –decorate, which helps us to easily see which commit belongs to which branch.

Custom formatting

This lets you display each commit however you want in printf style. For example %cn,  %h and %cd which represents committer name, abbreviated hash and committer date respectively.

Filtering the output

Git log can be filtered by different filters and format output

By amount

It displays latest n commits and its output.

For example

Will display latest 3 commit details

By date

Using --after and --before,  we can get logs after or before the specified date. You can also use these both to get logs between two dates

Example

--since and and --until are synonym for --after and --before

--date flag is used to format the output of the date. There are different option available for --date flag such as short, iso8601, relative, etc…

By Author

When you are only looking for commits by certain user you can --author flag. You can also use regular expressions for this.

By message

When you want to search for log with certain string using --grep flag. You can use -i option if you want ignore the case.

By file

When you want to get log only on some files you can specify files after -- flag

By Content

Git log allows you to get logs of files containing certain string. You can specify string using the -S flag

Format options available

Git log format options

Example

That was a bit on how we can use Git log, and specifically on how to format the output. Leave your comments, questions, and doubts in the comment section in case you need to reach me.