Compare Files, Commits, and Branches in Git

This article will teach us how to compare files, commits, and branches in Git using the git diff command. We use the git diff command to show the disparities between the files resulting from two commits or the current state of our repo and a previous commit.

The command comes in handy when comparing data sources.

The git diff Command

Syntax:

git diff

By default, the command will show the uncommitted changes in our repository.

The command will show us all removed and added lines of code in the original file. Let’s check out some examples.

Git Diff Commits

We can compare two commits in the same branch in the context below.

git diff <commit-1-id> <commit-2-id>

Example:

Here is the commit history for our local repository.

$ git log --oneline
c84e6f9 (HEAD -> main) Third Code Correction
9af4e38 Second Code Correction
a45ca97 First Code Correction
8c1cefc My Commit Message
c5bf6c8 Sixth Commit
3b641e0 Fourth Commit
21ca1e7 Third Commit
b2f7710 Initial commit

In a scenario where we wanted to see the differences between our Third Code Correction and Second Code Correction commits, how would we go about it?

We will run the git diff command and mention the hash of our two commits, as shown below.

$ git diff 9af4e38 c84e6f9
diff --git a/insert.php b/insert.php
index 985a7af..a5f31c6 100644
--- a/insert.php
+++ b/insert.php
@@ -1,7 +1,6 @@
 <?php
 // Use ls command to shell_exec function
 $output = shell_exec('git');
-
 // Display the list of all files and directories
 echo "<pre>$output</pre>";
 ?>

The output above shows a difference at line 67. We can see that the Third Code Correction commit removed an empty line of code at line 67.

Git Diff Branches

To compare two branches in our repository, we run this command.

git diff <branch1> <branch2>

We would run this command if we wanted to compare the master branch and another branch called dev.7.

git diff master dev.7

If we add two dots between the branches, Git will compare the latest commits between the two.

Git Diff Files

We can compare two files in our repository using the git diff command in the context below.

git diff <path-to-file1> <path-to-file2>

Example:

$ git diff Head:sample.php HEAD:insert.php
diff --git a/sample.php b/insert.php
index dce9c57..a5f31c6 100644
--- a/sample.php
+++ b/insert.php
@@ -1,14 +1,6 @@
 <?php
-    $a= 23;
-    $nationality = "Dutch";
-    //applying conditions on nationality and age
-    if ($nationality == "Dutch")
-    {
-        if ($a >= 18) {
-            echo "Eligible to vote";
-        }
-        else {
-            echo "Not eligible to vote";
-        }
-    }
+// Use ls command to shell_exec function
+$output = shell_exec('git');
+// Display the list of all files and directories
+echo "<pre>$output</pre>";

Note how we specified the branch in the git diff command. If the files are in different branches, you will need to specify the branch for each file.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Git Diff

  • Show Staged and Unstaged Changes in Git
  • Use Git Diff to Ignore Whitespaces in Git
  • Difference Between Two Commits in Git