Deep Dive into the Git "log" Command

Deep Dive into the Git "log" Command

Git log : Used to retrive the commit history

Basic Usage:

  • The git log command is used to display the commit history of a Git repository.

  • By default, it shows a list of commits in reverse chronological order, with the most recent commit displayed first.

By performing "git log" , it will generate the following :

HTML

commit <7_Character_Hash>
Author: <Author_name>  <Author_email>
Date:   <date>

    <Subject/commit message>

---------------------------------------------
Eg:
commit xyz1234
Author: Alice Johnson <alice@example.com>
Date:   Sun Aug 22 20:00:00 2023 +0200

    Fix bug in module Z

Commit Information Displayed:

  1. commit <commit hash>: The full commit hash.

  2. Author: <author>: The author of the commit.

  3. Date: <date>: The date and time the commit was made.

  4. Commit message/Subject: <subject>: The first line of the commit message.

Case :- 1 : git log

  • Displays all the commits that are made so far.

  • It will show all the detailed information like time, author name and author email & commit message.

  • Refer the below snippet

HTML

abc@linux:~/Desktop/git-practice$ git log
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 2 files of 4 lines

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : 2 files of 2 lines

Case :-2 : git log --oneline

  • This option is very helpful if we have lot of commits and to identify commit based on message

  • It will neglect all the author details, time stamp details

  • It will also neglect the unwanted commit-hash, as it shows only the "7_Character_Hash"

  • Refer the below snippet.

HTML

abc@linux:~/Desktop/git-practice$ git log --oneline
8d45e28 (HEAD -> master) 2nd Commit : 2 files of 4 lines
1b3cae4 1st Commit : 2 files of 2 lines

Case :-3 : git log <file_name>

  • It is used to view the commit history for a specific file in a Git repository.

  • By providing the filename as an argument, you can see the commits that affected that file, along with their corresponding commit information.

  • This command is particularly useful when you want to understand the history of changes for a specific file, track who made changes, and review the commit messages associated with those changes.

  • only particular file's commit changes will be displayed.

  • Refer the below code snippet for more understanding

HTML

#Checking commits performed on file1.txt
abc@linux:~/Desktop/git-practice$ git log file1.txt
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 4 lines added in file1.txt 

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : 2 lines added in file1.txt 

-----------------------------------------------------

#Checking commits performed on file2.txt
abc@linux:~/Desktop/git-practice$ git log file2.txt
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 2 lines added in file2.txt 

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : 1 line added in file2.txt

Case :-4 : git log -n -x

  • This command will display the details of the most recent 'x' commits in your repository.

  • The output will include information about each commit such as the commit hash, author, date, and commit message.

  • Refer the following snippet

HTML

#Normal git log command
-------------------------------
abc@linux:~/Desktop/git-practice$ git log
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 2 files of 4 lines

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : 2 files of 2 lines

HTML

# git log -n x
-------------------------------
Here, let x=1 : It will display the latest commit alone(i.e.., one commit alone)
abc@linux:~/Desktop/git-practice$ git log -n 1
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 2 files of 4 lines

----------------------------------

Here, let x=2 : It will display the last 2 latest  commits
abc@linux:~/Desktop/git-practice$ git log
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : 4 lines are added

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : 2 lines are added

Case :- 5 : git log --oneline -n -x

  • This command will show a concise list of the most recent 'x' commits, each represented by their short commit hash and commit message.

  • The output doesn't include information about each commit's author, date details.

  • Refer the following snippet

HTML

abc@linux:~/Desktop/git-practice$ git log --oneline -3
8d45e28 (HEAD -> master) 2nd Commit : 2 files of 4 lines
1b3cae4 1st Commit : 2 files of 2 lines
1b3cae9 Added Readme file

Case :- 6 : Pattern Search in Commit message

git log --grep "<pattern to be searched>"

  • We can search based on given pattern in commit message

  • Eg : git log --grep "added" : it will list all the logs that contains commit msg "added".

HTML

#It will search & lists the commits that contains pattern "Added"
abc@linux:~/Desktop/git-practice$ git log --grep "Added"
commit 8d45e289e64be0df7d5da5de8c2838bd558e7e76 (HEAD -> master)
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:35:19 2023 +0530

    2nd Commit : Added feature2

commit 1b3cae43a498f9669d0050c15b076017110e7632
Author: Tom <tom1523@gmail.com>
Date:   Tue Aug 29 14:31:14 2023 +0530

    1st Commit : Added feature1

Case :- 7 : Searching based on the author_name & date

Show commits more recent than a specific Time

git log --until="date"

git log --before="date"

eg : git log --until="5 minutes ago"

git log --before="2023-05-17"

Show commits based on Author

lists all the commits based on the author name

git log --author=author_name

(or)

git log --author=author_name --oneline

eg : git log --author=Tom