DEV Community

Cover image for Git Log -Basics of Git
5 Mins Learn
5 Mins Learn

Posted on • Updated on • Originally published at 5minslearn.Medium

Git Log -Basics of Git

Git Log — Command Options (Formatting & Filtering)

As we get to familiarize with Git in these series of blogs, it’s highly recommended to know some of the utility commands of Git for debugging purposes.

Git Log in short Shows the commit logs

Git Log commands is used to list the commits made by the developers in the repo. It’s a kind of review tool to read the history of the repo. Git Log command comes with lot of options which will take hours of time to go through each and every option and understand it’s usage. Visit Git Log official documentation page, to go through all options in Git Log.

The advanced features of git log can be split into two categories:

  • Formatting how each commit is displayed

  • Filtering which commits are included in the output

Together, these two skills give you the power to go back into your project and find any information that you could possibly need.

Let’s understand most powerful and useful Git Log command options

Formatting Options

Git Log

Git Log command shows all the commits with their ID, author, date and it’s commit message

Simple Git Log command

Oneline

git log --oneline option compresses each commit to a single line. It displays commit hash and first line of commit message. This was the widely used option. This option helps to gain high-level overview of a project.

git log — oneline

git log --pretty=oneline is alternative to this command except it shows entire commit hash.

Log with modified lines

git log --oneline -p option shows each commit with the changes made on each file.

git log --oneline -p

Pretty

git log --pretty=:format option is used to view log in different pretty formats. Replace :format with any one of oneline, short, medium, full, fuller, reference, email, raw.

Some of the format options are as follows,

oneline

git log --pretty=oneline

short

git log --pretty=short

raw

git log --pretty=raw

Stat

git log --oneline --stat option shows logs with insertions and deletes on each file.

git log --oneline --stat

Graph

git log --oneline --graph --decorate option shows logs with tree visual representation. This option can be used, if your project has multiple branches. Changes on branches and merges can be best viewed with this option.

git log --oneline --graph --decorate

Shortlog

The git shortlog command is a special version of git log intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who’s been working on what.

By default, git shortlog sorts the output by author name, but you can also pass the -n option to sort by the number of commits per author.

git shortlog

Custom Formatting

For all of your other git log formatting needs, you can use the --pretty=format:"" option. This lets you display each commit however you want using printf-style placeholders.

For example, the %an, %h, %s and %cd characters in the following command are replaced with the committer name, abbreviated commit hash, commit message and the committed date, respectively.

git log --pretty=format:"%an committed (%h) %s on %cd"

git log --pretty=format:"%an committed (%h) %s on %cd"

Filtering Options

Git supports out of the box filtering options. Let’s explore filtering options available in git log command.

By Date

If you’re looking for a commit from a specific time frame, you can use the --after or --before flags for filtering commits by date.

git log --oneline --after="2022-7-5" / git log --oneline --after="yesterday"

git log --oneline --after="2022-7-5"

By Count

You can limit git log’s output by including the - option. For example, the following command will display only the 2 most recent commits.

git log --oneline -2

git log --oneline -2

By Author

Filter the commits created by a particular user, use the --author flag. You can either use regular expression or the exact name of the author to filter the commits.

git log --oneline --author="Arunachalam B"

git log --oneline --author="Arunachalam B"

You can also use regular expressions to create more complex searches. For example, the following command searches for commits by either Arunachalam or Ashwin.

git log --oneline --author="Arunachalam\|Ashwin"

git log --oneline --author="Arunachalam\|Ashwin"

By Message

To filter commits by their commit message, use the --grep flag. This works just like the --author flag discussed above, but it matches against the commit message instead of the author.

`git log --oneline --grep="Upgrades"`
Enter fullscreen mode Exit fullscreen mode

git log --oneline --grep="Upgrades"

By File

Many times, you’re only interested in changes that happened to a particular file. To show the history related to a file, all you have to do is pass in the file path. For example, the following returns all commits that affected either the foo.py or the bar.py file:

`git log --oneline -- package.json`
Enter fullscreen mode Exit fullscreen mode

git log — oneline — package.json

Hope you’ll understand the powerful options available with git log command. This is the most preferred command by developers to identify a commit in a repo.

Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.

References

  1. https://git-scm.com/docs/git-log

  2. https://www.atlassian.com/git/tutorials/git-log

Top comments (1)

Collapse
 
responsivecrocodile profile image
Responsive Crocodile

And maybe will be useful data visualization tool for git log: github.com/bakhirev/assayo