Git Diff is a command that helps us to see the changes that we have made to a particular entity. We can compare the changes made to files, commits, or even different branches. When working with multiple files, we may forget the changes that we have made since the last commit or we just want to check the changes that we have made before finally committing them. Git Diff helps us in doing all this.
Diff is short for the word difference. It is a function that takes two files as input and returns the difference between them.
Git Diff also uses a diff function to tell us about the changes we have made to our files.
In most cases, the two files that are given as input to Git Diff will be two different versions of the same file.
When working with hundreds of lines of code and altering just a few lines among them, it becomes a tedious task to find those few lines that we had altered. Git Diff helps us in viewing these changes and assists us to better understand how our project changed over time.
Git Diff Command
Git Diff can be used to compare the changes between different entities. These entities can be files, commits, or branches. Let’s take a look at how to use the Git Diff command for these entities.
To view the changes made to a file since the last time it was staged:
$ git diff <file-name>
The above command will not be able to tell us about the changes made to a staged file. To compare a staged file with its last committed version we can use the --staged option.
$ git diff --staged <file-name>
Running the above two commands without giving the file name will return the changes made to all the files.
We can compare a file that has been committed several times and see the changes made to the files for each commit. We need to know the hash of the two commits that we want to compare.
$ git diff <hash-1> <hash-2>
We can use the HEAD to see the difference in the versions of files for a previous commit and the most recent commit.
$ git diff <commit-hash> HEAD
To see the changes between the versions of files that were previously committed and the version on which you are currently working:
$ git diff <commit-hash>
We can also pass a specific file name in the above commands to compare the differences for a single file.
We can also compare the differences between different branches by using the git diff command.
To compare the latest commit of one branch with the latest commit of some other branch, we can use the .. (double dot) notation. We can also use just a single space between the names of two branches to do the same thing.
$ git diff <branch-1>..<branch-2>
To compare the latest commit of one branch with the commit of the common ancestor of the two branches we use the … (triple dot) notation. This is just a way to compare the branch with the commit point from which it originated to see what all we have done with the branch since its creation.
$ git diff <branch-1>...<branch-2>
We can also pass the name of a file to the above commands to compare the versions of just a single file.
Understanding the Output
The output returned by the Git Diff command can be a little complicated to understand. Git gives us a lot of information about the differences but in a rather complex way. Let’s look at the output of the Git Diff command and try to understand it.
The first line denotes the two versions of the file we are comparing that we have given as input to the diff function of Git Diff. Git always names the two files as Version A and Version B.
Git assigns the symbols --- and +++ to the two versions(A and B) so that we can distinguish them later on. --- is assigned to version A and +++ is assigned to version B.
Instead of showing the entire file, Git just displays the lines that were altered. Each part of the file that was altered is displayed as a Diff Chunk.
The first line of a chunk consists of a Chunk Header that describes the changes that were done in that chunk. It tells us about the lines that were added or removed from the two versions.
In the following example, two lines have been extracted from version A starting from line number 2. And 7 lines have been added to version B starting from line number 2.
Each chunk displays the changes that were made to a part of the file. Lines that start with - represent version A and lines that start with + represent version B.
Consider the following example, in which the one line was deleted from version A(line in red color) and two lines were added to version B(lines in green color)
We would often find ourselves in situations where we need to compare our file to its previous versions. These previous versions could be saved on the last commit, some other previous commit, or even on some other branch. Git Diff is the command that will come to our rescue and help us to understand the differences between files. Git Diff uses a diff function that takes the two files as input and returns their differences. Understanding the output of the Git Diff command can also be a challenge when you first start using it. The Git Diff command displays the differences in the form of chunks and uses the + and - symbols to denote the two versions of the files.