Checking the difference between commits
Previously, we used git log
to see the overall history of the repository. But often, you'd want to see the commits that affected a specific file.
For example, if you spotted a weird piece of code in one of your files and want to see who made that change and why, you'd first want to see the history of that particular file. You can do that with the git log
command followed by the file path, for example, git log hello.html
.
Check the history of the hello.html
file using the command above.
Tip: Remember that you can scroll through the output of git log
using the and arrow keys on your keyboard. To exit pager mode, press q.
Now, let's do the same with the style.css
file.
Check the history of the style.css
file.
As you see, only the commits that affected the specified file are shown. This is very useful when you want to see how a specific file has changed over time. Now, how do you compare the actual changes between two commits?
We've already used git diff
to check what changes we made in the working directory compared to the staging area or the last commit. Now let's see how we can compare any two commits in the repository.
For example, let's see what changed in the hello.html
file since version 1.0
(we created the tag 1.0
in the chapter about Git tags, remember?).
To compare two commits, we can use the git diff
command followed by the two commit references—git diff <from> <to>
. The order matters: Git will show you what changed to get from the first commit to the second one. If you pass just one commit reference, you will be comparing it to the working tree.
Don't forget that there are multiple ways to refer to commits: their hashes, tags, branch names, or using relative references like HEAD~1
, etc.
Let's compare version 1.0
of the file hello.html
with its state in the latest commit. You can do that with the following command:
git diff 1.0 HEAD
Run the above command to compare the last two commits.
Tip: When the output of git diff
can't fit on one screen, your terminal will go into pager mode. Same as before, you can scroll through the output using the and arrow keys on your keyboard. To exit pager mode, press q.
You should see something like this:
diff --git a/hello.html b/hello.htmlindex ae7ab8c..7833e8a 100644--- a/hello.html+++ b/hello.html@@ -1,4 +1,7 @@ <html>+ <head>+ <link rel="stylesheet" href="style.css"/>+ </head> <body> <h1>Hello, World!</h1> </body>diff --git a/style.css b/style.cssnew file mode 100644index 0000000..d224431--- /dev/null+++ b/style.css@@ -0,0 +1,3 @@+h1 {+ color: red;+}\ No newline at end of file
The output shows the changes between these two commits: the lines that were removed (marked with -
) and the lines that were added (marked with +
).
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.