When you’re working with the Git version control system, you may want to compare data in your repository with another data source. For instance, you may want to compare two different commits with each other, or two files.
That’s where the diff function comes in. Diffing is a function that accepts two inputs and presents the changes that exist between those data sources. Diff functions can be executed on branches, files, and commits.
This tutorial will discuss, with examples, the basics of diffing with Git and how to use the git diff command. By the end of reading this tutorial, you’ll be an expert at using the git diff command to compare two Git data sources with each other.
Diffs in Git
The git diff command is used to perform a diff operation between two data sets in a Git repo.
Let’s suppose we have initialized a blank repository, and we want to start our repository with a README.md file. We have created a README.md file in our repository that contains the following sentence:
This is an example of the Git diff feature.
We are going to create a commit with this file using the git commit command:
This allows us to save the changes we have made to our repository. The git command returns:
[master (root-commit) 749055e] docs: Create README.md 1 file changed, 1 insertion(+) create mode 100644 README.md
If we run the git diff command at this stage, nothing will happen. This is because our repository has been initialized, and there are no changes between any files in our repository as of yet. But now that we have a basic repository, we can change the contents of the files in our repo, which will allow us to see the git diff command in action.
Suppose we want to add the sentence “We just added this line to our file.” to the README.md file. We could do so using this command:
echo "We just added this line to our file." >> README.md
The above command adds our sentence to the README.md file.
This means that there is now a difference between our initial README.md file — which only contained one sentence — and the current README.md file in our repository.
By executing the git diff command, we can see the differences between these two files. By default, the git diff command produces a diff for all files between the latest commit and the current state of the repository.
Here’s the syntax for the git diff command:
When we run this command, the following response is returned:
diff --git a/README.md b/README.md index f808522..f08e544 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ This is an example of the Git diff feature. +We have just added this line to our file.
This is a typical result from the git diff command which shows what has been added or removed in our file in a combined diff format.
The git diff command, when executed, returns a list of all the changes in all the files between our last commit and our current working copy of a repository.
If you want to retrieve the changes made to a specific file in a repository, you can specify that file as a third parameter when using the git diff command. Suppose we only want to see the changes made to the README.md file.
In this example, we have only changed the README.md file, so only those changes would be shown anyway. But if we were working with a larger repo, we may want to compare only the changes in one file. To do so, we could use this command:
git diff README.md
This command allows us to compare our current version of the README.md file with the last version committed to our repository.
Breaking Down a Git Diff
Git diffs feature a number of components that we can use to analyze the changes between a file in a repository. Let’s break them down, with reference to our previous example.
The first component of our diff is the input files. This part of the diff tells us which files are being compared in the diff.
In this case, we are comparing the README.md file in the current version of our code to the README.md file in the last version of our code. This is stated on the first line of our code:
diff --git a/README.md b/README.md
Next, our diff contains the metadata for our Git repository. This metadata displays the object version labels used by Git to track the changes you have made to a file. This information is rarely used in most cases of the diff command. In our above example, the metadata looks like this:
index f808522..f08e544 100644
The next part of the Git diff output is the change markers. These markers tell us what type of changes have been made to our files. The change markers for our above example are:
--- a/README.md +++ b/README.md
These markers tell us that changes from a/README.md have been made (denoted by the minus signs), which are reflected in b/README.md (denotes by the plus signs).
Finally, our diff returns a list of the changes made to our code. Unlike a full file comparison, diffs only show the sections of a file that have been changed. In our example, we added one line to our file, which returned the following result:
@@ -1 +1,2 @@ This is an example of the Git diff feature. +We have just added this line to our file.
The first line is a summary of the changes made to our file. This tells us that we have added one line of code to our file (+1) starting on the second line (2).
Then, we are shown a list of the changes made. As you can see, because we added the line “We have just added this line to our file.”, that line appears in our diff. The plus sign tells us that we added that line to the file.
Comparing Files From Two Commits
In the above example, we discussed how to compare your uncommitted changes to a file with the last committed version of a file. However, you can also use git diff to compare a file across two commits.
To do so, you first need to retrieve the ID of the commits whose files you want to compare. You can accomplish this task using the git log –pretty=oneline command, which returns a brief summary of all the commits in a repo:
This command returns:
5141ea9c41cdc7152408bfcab54a910f34441855 (HEAD -> master) feat: Update README.md 749055ee99df2aa6f5adc4cbe4bfc708395f1c2e docs: Create README.md
Now, suppose we want to compare these two commits. We could do so using the following syntax:
git diff <commit one> <commit two>
In this syntax, “commit one” refers to the ID hash of the first commit which you want to compare with the second commit, and “commit two” refers to the ID hash of the second commit which you want to compare with the first one.
So, if we wanted to compare these two commits, we would use this command:
git diff 5141ea9c41cdc7152408bfcab54a910f34441855 749055ee99df2aa6f5adc4cbe4bfc708395f1c2e
The above command will perform a diff operation across our two commits.
Comparing Two Branches
In addition, the git diff command can be used for comparing branches and the changes made across those branches. The syntax for doing so is:
git diff <first branch> <second branch>
Suppose we wanted to compare the “master” branch with a branch called “dev-v0.9” in our repository. We could do so using this command:
git diff master dev-v0.9
When this command is executed, a diff will be run between the “master” and “dev-v0.9” branches in our codebase.
Similarly, you can compare specific files across two different branches. To do so, you can use the same syntax as above and additionally specify the file which you want to compare.
Suppose we wanted to compare the file README.md across our “master” and “dev-v0.9” branches. We could do so using this code:
git diff master dev-v0.9 ./README.md
This will compare the file README.md (which is in our current directory, denoted by the “./” syntax) across the “master” and “dev-v0.9” branches.
Diffing is a useful function in Git that allows you to compare two files, branches, or commits in a Git repository. This allows you to see what changes have been made since a certain point in your repository.
This tutorial discussed the basics of diffing with Git and how to use the git diff command to perform a diff operation. Now you’re equipped with the knowledge you need to start using the git diff command like an expert!