Git: How to compare two different versions of a file

Git FAQ: How do I compare two different versions of a file with git?

This solution depends on what exactly you need to do.

1) You have not run `git add`

If you just modified a file but haven’t run git add on it yet, use this:

$ git diff

2) You have run `git add`

To see changes to a file you have run git add on, but have not committed, use this:

$ git diff --cached

A git file diff example

In my case, I updated this file, and then added it with git add, so I used that second command:

> git diff --cached 
diff --git a/_overviews/overview/ b/_overviews/overview/
index eecc4e86..2773a705 100644
--- a/_overviews/overview/
+++ b/_overviews/overview/
@@ -1,6 +1,9 @@
 title: Introduction
 description: This page begins the overview documentation of the Scala 3 language.
+num: 1
+next-page: scala-features

The + changes in the last three lines showed the changes I made, so this gave me confirmation that I did what I wanted.

Comparing two file versions in your Git repository

If you’re using Git and need to compare two recent versions of the same file, I can confirm that this git diff command works:

git diff HEAD^ HEAD nodeBlog.scala.html

That command compares the second-most recent version of the file (given by HEAD^) to the most recent version of the file (HEAD). In this example I want to see the detailed differences of these versions of the file named nodeBlog.scala.html. In my case, the result of this command is shown in the image.

Per the excellent book Pro Git, “HEAD^” means, “The parent of HEAD.”

As shown on this SO page, there are other ways to issue this Git command, and similar commands.

Update ('git log' and then 'git diff')

I did the same thing again today, but with some different Git commands. First, I created a short/concise list of all changes to a file using this git log command:

$ git log --pretty=oneline --abbrev-commit divKofiPatreon.scala.html
6f343d4 Changed the Ko-Fi text (Tip Your Barista?) and color to see if it makes a difference.
f73f744 Took the Kofi ad out of the upper-LHS. Also added the RSS icon back to the header.
eb7c0ce moved the text that i put under the Kofi icon into the properties file so i can test things more easily on the server.
3091aaf Got rid of the Patreon logo. Only using KoFi for now. Also changed the KoFi text.
755b10c Changed the text under the Kofi ad.

Once I saw the commit (f73f744) that I wanted to compare the current version of the file (HEAD) to, I ran this command:

$ git diff HEAD f73f744 divKofiPatreon.scala.html 

That showed all of the differences between the two versions of the file, with this difference being the important change I was looking for:

-<script type='text/javascript'>kofiwidget2.init('Tip Your Barista (author)?', '#4299cc', 'N4N31K5N6');kofiwidget2.draw();</script> 
+<script type='text/javascript'>kofiwidget2.init('Buy Me A Coffee!', '#FF5E5B', 'N4N31K5N6');kofiwidget2.draw();</script>

So again, if you need to see how to compare two versions of a file with Git, I hope these examples are helpful.

Photo D8
Git: How to compare two different versions of a file