Viewing commit history in Git

git log command shows the commit logs. It provides many useful options to help you view and find the commit history.

Show commit history in default format

# Show the whole commit history
$ git log
commit 69c0b0bd7ff8cba857cae89a01c9da4a27b43632
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 9 21:30:11 2018 +0800

    feat: add main php file

commit 9438770e0d9ba6d775a61d9b4d1d4030a7ee682b
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 9 19:34:45 2018 +0800

    Initial commit

Show commit history in short format with one log in one line

$ git log --oneline
69c0b0b feat: add main php file
9438770 Initial commit

Show commits containing specified string

# Show only commits whose commit messages contain 'main menu'
$ git log --grep 'main menu'

Show commit history with limited number

# Show only the last two commit logs
$ git log -2

Show commit history of limited time period

# Show commit logs that are made in the last 2 weeks
# --after or --since works the same
$ git log --after=2.week

# Show commit logs that are made after 2018-12-01
$ git log --after=2018-12-01

# Show commit logs that are made before 2018-12-01
$ git log --before=2018-12-01

Show commits related to a string

# Show only those commits that changed the number of occurrences of update_post_meta,
# means the commits add or remove 'update_post_meta'
$ git log -S update_post_meta

Show commits that changed a file / files

# show commits that introduced changes to my-plugin.php
$ git log -- my-plugin.php

# show commits that introduced changes to files in inc/ directory
$ git log -- inc/

Show commit history with difference/changes

# Show commit logs with differemce introduced in each commit
$ git log -p 

Show commits of specified author or committer

# Show only commits made by john
$ git log --author john

# Show only commits committed by john
$ git log --committer john

Show commits in graph displaying the commit structure

# Show all commits of branches in a graph structure
# --all,
# --graph,
# --oneline,
# --decorate, 
$ git log --all --graph --oneline --decorate