Check Commit Histories – git log

Viewing Commit Histories with git log

git log is the command used when you want to get the commit history information of the repository.

Key information that the git log command provides

For example, when you run this command...

git log

...you'll see a message like the one below. In this case, you can see that there are two commits.

commit acc4aa4c31b033bc39113073ef404e14a970e65b (HEAD -> master, origin/master)
Author: bloovee <bloovee2021@gmail.com>
Date:   Sat Oct 28 11:23:58 2023 +0800

    added .gitignore file

commit 651e510f668aa841a49793b4fa18d2de74c41f5c
Author: bloovee <bloovee2021@gmail.com>
Date:   Sat Oct 28 11:03:27 2023 +0800

    the first commit

For each commit, there are four lines of information.

  1. The first line is commit hash. The commit hash is a unique ID of each commit. When you want to retrieve a commit, you need this hash.
  2. The second line is the username and email address of the author of the commit.
  3. The third line is the date and time of the commit.
  4. The last line is the commit message.

Key options

"--oneline" option

Displays each commit in one line. If commit histories become too long, you may want to skip some items to display. The --oneline option is useful for shortening the descriptions. With this option, each commit history is shown with one line.

When you run the command below...

git log --oneline

You'll see the command line response as shown below.

acc4aa4 (HEAD -> master, origin/master) added .gitignore file
651e510 the first commit

"--graph" option

Displays commits with a graph. With the --graph option, you can see the branch diversion or integration history visually. It is effective when commit histories are more complicated with multiple branches. The branch concept will be explained in the next chapter. The following is an example of running git log with the --graph and --oneline option.

When you run the command below...

git log --oneline --graph

...and when there are multiple branches, you'll see the commit history in a tree structure.

*   b5105dc (HEAD -> master) Merged branch A
|\  
| *   8fef9f1 (Branch_A) Merged branch B
| |\  
| | * 3ab0477 (Branch_B) Edit 5 on branch B
| |/  
| * e83087e Edit 4 on branch A
| * 8fcc58c Edit 3 on branch A
* | a815b57 Edit 2 on master branch
* | 931aef2 Edit 1 on master branch
|/  
* 5c1fd7a First Commit

IdeaTips: Exit from the git log display mode

When you run the git log command, the command line switches to a different display mode in which you cannot type. To go back to the original mode, you need to press the q key.