Header

Viewing the commit history

How to see previous changes using Git.

Viewing a list of the latest commits

If you want to see what’s happened recently in your project, you can use git log. This command will output a list of the latest commits in chronological order, with the latest commit first.

Every commit in the list will look a little something like this:

commit d240853866f20fc3e536cb3bca86c86c54b723ce
Author: Robert Lyall <rob@atech.media>
Date:   Fri Apr 26 11:37:39 2019 +0100

    Initial commit

You’ll be able to see the name of who made the commit, their e-mail address, what time the commit was made, and the message they entered when they committed.

Commit hashes

The long string following the word commit is called the commit hash. It’s unique identifier generated by Git. Every commit has one, and I’ll show you what they’re used for shortly.

Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.

You can either use the arrow keys to navigate up and down, or press the spacebar to view the next page. To exit the list, just press the letter q on your keyboard.

A bit more information

Just knowing that some changes took place is great, but often you want to know exactly what changed in each file. Don’t worry, Git has got you covered!

By running git log -p, you’ll get the same list as before, however each commit will have a list of the files that have been added/modified/removed, as well as the actual changes themselves.

Each commit in the list will look a little something like this:

commit 8fd3350cf6fba8bcd4abc7233d4ff6b81dd6ed3c
Author: Robert Lyall <rob@atech.media>
Date:   Fri Apr 26 14:02:54 2019 +0100

    Add the correct link to Brie

diff --git a/index.html b/index.html
index f501e7f..c23c354 100644
--- a/index.html
+++ b/index.html
@@ -48,7 +48,7 @@
             <li class="product">
               <img alt="Brie" class="product__image" src="/assets/images/cheese/brie.svg" />
               <h3 class="product__name">
-                <a class="product__link" href="#">Brie</a>
+                <a class="product__link" href="/cheese/brie">Brie</a>
               </h3>
             </li>
             <li class="product">

Note: If you’re wondering what “diff” means, it’s short for difference, and it’s essentially a summary of file changes between two commits in your repository.

Looking up changes for a specific commit

If you have the hash for a commit, you can use the git show command to display the changes for that single commit.

git show 5eba8ab3b718a6ab6610186be934ba214e228a58

The output is identical to each individual commit when using git log -p.

commit 5eba8ab3b718a6ab6610186be934ba214e228a58 (HEAD -> master)
Author: Robert Lyall <rob@atech.media>
Date:   Fri Apr 26 14:03:11 2019 +0100

    Remove Cheddar

diff --git a/index.html b/index.html
index c23c354..ec42f1d 100644
--- a/index.html
+++ b/index.html
@@ -51,12 +51,6 @@
                 <a class="product__link" href="/cheese/brie">Brie</a>
               </h3>
             </li>
-            <li class="product">
-              <img alt="Cheddar" class="product__image" src="/assets/images/cheese/cheddar.svg" />
-              <h3 class="product__name">
-                <a class="product__link" href="#">Cheddar</a>
-              </h3>
-            </li>
             <li class="product">
               <img alt="Robiola" class="product__image" src="/assets/images/cheese/robiola.svg" />
               <h3 class="product__name">

Commit hashes are generally unique enough for you to use the first few letters/numbers when looking up a hash rather than the full hash.

So, you can just use git show 5eba8a instead to get the same result.

Want to learn more about Git?

This tutorial is part of a free beginner-friendly course!

Learn More