What does ‘diff’ mean in Git? In this beginner Git tutorial, we will walk you through the concept of a diff and the benefits of using a robust diff tool, like that offered in the GitKraken Git GUI.
What is a Diff in Git?
Get the latest version of the GitKraken Git GUI:
A diff takes two data sets and shows you what has changed between them. Data sets can be files, commits, branches, etc.
Viewing the Diff Between Commits
Let’s look at an example using two commits; let’s say we want to see what has changed between Commit 1 and Commit 3.
The diff will compare the differences between the two commits, showing what files were added, deleted, or modified.
In this example, Commit 1 is the baseline. You may also notice how the diff looks identical to the changes from Commit 3, and that’s no mistake! The literal difference between Commit 1 and Commit 3 are the changes introduced by Commit 3.
But what about the diff of each file? When examining the diff of each file, added lines of text are commonly highlighted in green or denoted with a + sign. Similarly, deleted lines of text are commonly highlighted in red or donated with a - sign.
The file diff will likely be your most useful tool for understanding what changed, and how it changed
The Benefits of Viewing a Diff
Diffs provide useful information about your repository in a legible format, making it easier to perform the following actions:
- Review changes while staging.
- Decide whether to rebase or merge.
- Copy and paste code from a file.
Giving you even more confidence in your code game. 💥
Accessing the Diff in GitKraken
Let’s see how this all works in the GitKraken Git GUI.
If you click a commit in the graph, the GitKraken Git GUI will list all of the files changed on the right commit panel.
Click on any file to open that file’s diff. Lines that were added to that file will show in green, and lines that were deleted will show in red. If code has been added and deleted from the same line, the GitKraken Git GUI will show both together.
Hunk, Inline, and Split Views
By default, the diff will display in Hunk View, which displays the file changes in blocks of code, helping reduce the noise of seeing the entire file.
However, you can toggle between Inline View, which shows the changes within the context of the entire file, or Split View, which will show you how the file appeared before and after the changes.
View the Diff when Staging Files
These options are also available when staging files. For example, try reviewing the file diffs and switching between Hunk, Inline, or Split views to make the most informed commits.
Viewing the Diff of Two Branches
There are a few more things you can do with the diff in the GitKraken Git GUI.
From the central graph, hold down the
Ctrl key to select the head commit of two branches. From here, you will have the option to view the diff between these two branches.
But what happens when you change the order of the branch selection? When you give that a try, you may see deleted files show as “added” or vice versa. That’s because direction matters when viewing a diff. So keep that in mind as you click the order of branch or commit diffs.
Customize Editor Preferences
Finally, navigate to Preferences → Editor Preferences, where you can customize your preferences for syntax highlighting, line count, and word wrap. This should help make your diff easier to read. 😅
Code with Confidence with GitKraken
Stay on the cutting edge of software development by getting innovative tips, trends and stories delivered to your inbox every month!