Viewing a commit

Suppose now that we want to look at one specific commit in our history. To do this we'll use the git show command. We need to specify the commit that we want to inspect. There are two ways in which we can reference a commit: 1) by the commit ID, 2) as the number of commits before the HEAD pointer of our repository.


Here's an example (image). If we want to view the commit previous to the HEAD pointer of our repository we can run either one of the following commands

git show 292c34d
git show HEAD~1

If we use the commit ID we don't have to pass all the first 7 characters of the ID. We can pass fewer characters as long as it doesn't generate ambiguity. If we instead go with the second option, the number after the tilde is how many steps back from the HEAD pointer we wish to go. Not including the tilde and number means that we wish Git to show the HEAD.

Viewing a commit 1

Regardless of which one we use, Git will print the commit information along with the diff. If instead of seeing the diff we want to inspect the state of a specific file at that point in the repository's history we run

git show HEAD~1:file_name.ext

or, if the file is in a sub-directory

git show HEAD~1:path/file_name.ext
Viewing a commit 2

If instead of browsing the commits we want to inspect the state of our entire repository at the time of a specific commit we run

git ls-tree HEAD~1

This will print the list of the objects that were present in our repository at the time of the commit that we specified. Elements of type blob are files and elements of type tree are sub-directories within our project.


Now we can ask Git to show us the content of a specific file by running

git show file_id

For example, if we want to inspect the main.txt file inside the src sub-directory, we run

git ls-tree HEAD~1:src/

This tells Git that we want to inspect the ls-tree of the src subdirectory. Now we can pick our file by its ID

git show 31e0f
Viewing a commit 3