Git Log -Basics of Git
Tags: git, gitlog, gettingstarted, gitbasics
Published: 2022-11-22 13:38:23

![Git Log — Command Options (Formatting & Filtering)](https://cdn-images-1.medium.com/max/2160/1*5OYwLzhDXpOh29t8yD12Cw.png)

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](https://git-scm.com/docs/git-log), 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](https://cdn-images-1.medium.com/max/2000/1*JOIcUTFdYeDo2C6HyV0GXg.png)

### 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](https://cdn-images-1.medium.com/max/2000/1*sC9BuCeBHsLuJXmqG1i2uA.png)

`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](https://cdn-images-1.medium.com/max/2000/1*mZQ3TwNawQOHz78ZqSCcdg.png)

### 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](https://cdn-images-1.medium.com/max/2096/1*yKSWm_wdF3Sgo8iPnl-20g.png)

*short*

![git log --pretty=short](https://cdn-images-1.medium.com/max/2000/1*Hb7GDz-val0k0m0ff2IFVA.png)

*raw*

![git log --pretty=raw](https://cdn-images-1.medium.com/max/2000/1*ZFB7vGbAfbacPIItsbLvLQ.png)

### Stat

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

![git log --oneline --stat](https://cdn-images-1.medium.com/max/2000/1*7aHVpq9haDleKhxLd6aRKg.png)

### 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](https://cdn-images-1.medium.com/max/2000/1*FAbcykK0YuXfNu_3PEqYsg.png)

### 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](https://cdn-images-1.medium.com/max/2000/1*lEw0nW3iEKWWutO_DwcuHg.png)

### 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"](https://cdn-images-1.medium.com/max/2384/1*I75IWlctd3TZtm-B09PSGg.png)

## 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"](https://cdn-images-1.medium.com/max/2000/1*ciWJURDoPvleKZnrDKH9ow.png)

### 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](https://cdn-images-1.medium.com/max/2000/1*MgE1wRkMdsf8GRHZw7YHkg.png)

### 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"](https://cdn-images-1.medium.com/max/2000/1*vl1W5XkLkgGRb8v0ngTOng.png)

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"](https://cdn-images-1.medium.com/max/2000/1*d6ugPi9dIKsJDpbNFl4YyA.png)

### 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"`

![git log --oneline --grep="Upgrades"](https://cdn-images-1.medium.com/max/2000/1*CZ9PHqOr4qnlxcZv45Rqzg.png)

### 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`

![git log — oneline — package.json](https://cdn-images-1.medium.com/max/2000/1*Svk40xhobHEumE7I2E-FzA.png)

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](https://5minslearn.gogosoon.com/?ref=devdotto_git_log) to receive more such insightful articles that get delivered straight to your inbox.

## References

 1. [https://git-scm.com/docs/git-log](https://git-scm.com/docs/git-log)

 2. [https://www.atlassian.com/git/tutorials/git-log](https://www.atlassian.com/git/tutorials/git-log)