Select Page

Introduction

The git diff command is used to differentiate two sets of data inputs, and write down the changes in stdout which are required to merge the two inputs into one. At least, that’s what the diff function is meant for. But in git, the diff subcommand has a lot more to offer than just that, which is why it is referred to as a multifunctional command in git, and is a very important  one indeed.

In this article, we discuss the several ways you could use the git diff command. You’d be surprised to know that it is not used to compare files only, but also other things like branches, commits, and a whole lot more! 


git diff file example

Let’s start off with the barebones. If you’re wondering how a git diff workflow looks on common, this section of the article is what you’re looking for. Before everything else, let’s create a demo folder and initialize it with git:

$ mkdir demo
$ cd demo
$ git init

Pretty straight forward. Now let us create a demo file with some text appended to it, which will be necessary to demonstrate the use of git diff:

$ touch file1.txt
$ echo ‘Hello, World!’ > file1.txt

Now in order for the git diff command to give me an output, I will first need to commit the changes I made, of course:

$ git add file.txt
$ git commit -m “A new file is created”

Looks good to me. It’s time to modify our file1.txt with some new text:

$ echo ‘Some new text’ > file1.txt

Alright! At this very instance, let’s try using the git diff command to see what we get back in the output:

$ git diff

diff --git a/file1.txt b/file1.txt
index 8ab686e..5d067f3 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
-Hello, World!
+Some new text

I know it looks a bit intimidating, but it isn’t really any rocket-science. To give you a very quick brief, the first line tells you the input source, the second line is some metadata, the third one with tells you that this file will go through a change, specifically, a removal one, and the next line with +++ is also a marker for change that does the opposite.

The last 3 lines of output are called chunks. These give you an insight of which texts are being changed to what other texts. The (minus) sign depicts that the line will be removed, while the + (plus) sign tells you which line will be added to replace the removed line.


git diff two files

You can compare two different files that reside in the same branch in a repository using git diff, and including a flag called “–no-index”, and then specifying the filenames. The syntax should look something like this:

$ git diff --no-index <file1> <file2>

Now let’s look at a simple example of the above. Before that, I’ll create a new file in our previous demo repository and compare it with file1.txt:

$ touch file2.txt
$ echo ‘This text is used for comparison’ > file2.txt

Now if I try out the syntax mentioned, I should get an output like this:

$ git diff --no-index file1.txt file2.txt

diff --git a/file1.txt b/file2.txt
index 8ab686e..fc336a6 100644
--- a/file1.txt
+++ b/file2.txt
@@ -1 +1 @@
-Hello, World!
+This text is used for comparison

git diff file names only

If you don’t want to see the changes that are being made, and only the names of the files that are compared, you can use the following syntax:

$ git diff --name-only

git diff specific file between commits

You can git diff for a file between two different commits by using their HASH IDs, by using the following syntax:

$ git diff <commit_id> <commit_id> -- path/to/target/file

Let us observe an example by first creating a file and making two separate commits for it:

$ touch file.txt
$ echo ‘This is the first line’ > file.txt
$ git add file.txt
$ git commit -m “A file is created”
$ echo ‘This line will replace the first line’ > file.txt
$ git add file.txt
$ git commit -m “The file is changed”

Very well. Now in order to compare the two commits, we need their HASH IDs. We will use the git reflog command to figure it out:

$ git reflog

13675b1 (HEAD -> master) HEAD@{0}: commit: The file is changed
4c9a4c1 HEAD@{1}: commit (initial): A file is created

Now using the IDs, I can compare the two commits, by using the following command:

$ git diff 4c9a4c1 13675b1 file.txt

diff --git a/file.txt b/file.txt
index f442643..d3e2104 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-This is the first line
+This line will replace the first line

git diff specific file last commit

You can also compare the very last commit with a given commit id, if you specify the HEAD, which should look something like:

$ git diff <commit_id> HEAD -- path/to/file

The output should look something like this:

$ git diff 4c9a4c1 HEAD -- file.txt

diff --git a/file.txt b/file.txt
index d3e2104..f442643 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-This is the first line
+This line will replace the first line

git diff specific file with master

Comparing files between the master branch and another branch is quite similar to the comparison between commits method. Just like in the commits one, you need to specify the master branch followed by the name of the other branch, and at the end specify the target file to execute the command on. The syntax is as follows:

$ git diff master <branch_name> -- path/to/file

For example, let’s assume that I created a new branch under the demo repository using:

$ git checkout -b development

After switching to this new branch, I’ll create and commit a new file:

$ touch file.txt 
$ echo 'A text under file in dev branch' > file.txt
$ git add file.txt
$ git commit -m "A new file in dev branch"

Now if I try out the syntax as shown, this is what I’m supposed to see:

$ git diff master development -- file.txt

diff --git a/file.txt b/file.txt
index 8ab686e..d97ffea 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello, World!                                       
+A text under file in dev branch

Just as expected.


git diff specific file between branches

This one is pretty much the same as the previous one, the only thing differing is that instead of specifying master, you specify the name of some other branch under your repository, like this:

$ git diff <branch_name> <branch_name> -- path/to/file

For instance, if I had another branch called feature, which also contained a similar file.txt, and compare it with the file under the development branch, this is the command I would’ve used:

$ git diff feature development -- file.txt

Note that the order of the file matters heavily. If instead you wrote the command like this:

$ git diff development feature -- file.txt

git diff specific file between local and remote

If you want to compare two files between a local repository and a remote one, you’d want to use the following syntax:

$ git diff <local_branch_name>:path/to/file <remote_name>/<branch_name>:path/to/file

Take for instance, if I were to compare a file called feature.txt in my repository’s master branch and another file located in the development branch under a remote repository (origin), I’d use the following command:

$ git diff master:feature.txt origin/development:feature.txt

So there you have it! I’ve only scratched the surface with the git diff command and there’s a whole lot more to it than just this. However, for the most part, the methods mentioned here should be enough for 90% of use cases.