The git diff command shows the differences between the files in two commits or between your current repository and a previous commit. This command displays changes denotes by headers and metadata for the files that have changed.
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.
Git Diff Command
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file.
Here’s the syntax for the git diff command:
By default, the git diff command displays any uncommitted changes to your repository.
We can see the removed lines from our original file as well as any lines added to or changed in our original file. Often, Git diff is used for comparing branches in a Git repository.
Git Diff Between Commits
You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch.
git diff <commit1> <commit2>
Let’s compare two commits in our Git repository.
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:
git log --pretty=oneline
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 can do so using this command:
git diff 5141ea9c41cdc7152408bfcab54a910f34441855 749055ee99df2aa6f5adc4cbe4bfc708395f1c2e
The above command will perform a diff operation across our two commits.
Git Diff Between Branches
To compare two Git branches using the diff command, specify the two branches you want to compare as arguments. You need to use two dots between each branch name. These dots indicate that you want to read the latest commit in each of the branches and compare them:
git diff <branch1>..<branch2>
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.
Git Diff Command Example
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.
Prepare Files and Commits
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. Now that we have a basic repository, we can change the contents of the files in our repo. This 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 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.
Run a Git Diff Between Commits
When we run the 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 @@ +We have just added this line to our file.
This is an example of the Git diff feature.
This is a typical result from the git diff command. Our result shows what has been added or removed in our file in a combined diff format.
The git diff command returns a list of all the changes in all the files between our last commit and our current 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. 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.
Git Diff Breakdown
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.
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 file pattern 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.
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!
For more learning resources on Git, check out our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.