How do you view the revision history in Git?


Let’s say you want to view all commits to the Git repository. The git log command returns all of the commits that have been made to the repository. This command lists the latest commits in chronological order, with the latest commit first.

The syntax of the git log command is given below −

$ git log

The following screenshot shows how to use the command to view all commits in the current repository.

dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log
commit cbc01c8399faf9063eca4ba7781d08eebbe56aaa (HEAD −> master)
Author : "Kiran <kiran.p@tutorialspoint . com>
Date: Fri Jan 22 19:38:42 2021 +0530
   file2. txt

commit 8100770d7274e6bae4e252d7b3c461020b8bc5b2
Author : "Kiran <kiran.p@tutorialspoint . com>
Date : Fri Jan 22 19:28:34 2021 +0530
   file1. txt

The command displays the following details for each commit in the history −

  • Secure Hash Algorithm (SHA) or the Commit Hash − This is a long unique string following the word commit. The meta-data which includes the commit message, committer, commit date, author, author date along with the hash of the root object (i.e., the folder) are hashed using the SHA-1 hashing algorithm to generate the commit hash. The pseudo-code to generate the commit hash can be represented as given below −

SHA1 (commit message => "test commit"
committer => Tom Cruise <tom.cruise@gmail.com>
commit date => Sat Nov 8 10:57:57 2020 +0100
author => Tom Cruise <tom.cruise@gmail.com>
author date => Sat Nov 8 10:57:57 2020 +0100
tree => 9c435a86e664be00db0d973e981425e4a3ef4f4d)

A change in any of the above values will change the commit hash thereby helping Git to maintain integrity.

  • Author − Person who originally wrote the code

  • Date − Commit date and time

  • Commit Message − Some text (E.g., comments or some description about the commit)

(HEAD−> master) in the above output indicates the current branch, here the master branch.

To navigate through the results of git log command use −

  • j or ↓ to scroll down by one line

  • k or ↑ to scroll up by one line

  • spacebar or the Page Down button to scroll down by one page

  • b or the Page Up button to scroll up by one page

  • q to quit the log

To view details up to a specific commit, append the commit hash to the git log command. The following example will display the commit with the hash 63096f31b85c0cfe26ffdce922564597fab0cf99 and all the commits made before this commit.

$ git log 63096f31b85c0cfe26ffdce922564597fab0cf99

Some common flags that can be used with the git log command to customize the information presented are −

 − −oneline − The commit history is displayed in the following format if this flag is used −

  • One commit per line

  • The first seven characters of the SHA

  • The commit message

Execute the following command −

$ git log − − oneline

The following screenshot shows the output when this flag is used −

cbc018 (HEAD−>master) file2.txt
8100770 file1.txt

−−patch or −p − This flag can be used to understand what exactly changed in each file. In other words, if this flag is used, the following details for each commit are displayed:

  • Files that are modified

  • Location of lines that were added or removed

  • Any specific change that you made

$ git log −− patch
Or
$ git log −p

The output of the above command is shown in the screenshot

delI@DESKTOP-N961NR5 MINGw64 /e/tut_repo (master)
$ git log −p
commit c81c9abe7c4c8273aIIab7d20a3aacObe54bfOOc (HEAD −> master)
Author : Kiran <kiran.p@gmail.com>
Date: Sun Jan 24 19:28:41 2021 +0530

   This is a short description
   This has to be a long description of why the
   commit was done. This should be multi line and should
   give more description

diff --git a/hello.txt b/hello.txt
index 4792a4a..9011447 100644
--- a/hello.txt
+++ b/hello.txt
@@ -2, 3, +2, 4 @@ hello , Cat
hello ,dog
hello again
hello
+hello again

Updated on: 20-Feb-2021

924 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements