Learning Git: What is a Diff?

GitKraken
4 min readJul 1, 2020

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?

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, GitKraken 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, GitKraken 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 GitKraken.

From the central graph, hold down the Cmd/ 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

Thanks for watching. 👋 If you liked this video, please subscribe to the GitKraken YouTube channel or watch other videos from our Learning Git with GitKraken series .

Originally published at https://blog.axosoft.com on July 1, 2020.

--

--

GitKraken

Creators of the world's most powerful suite of Git tools: GitKraken Client, GitKraken CLI, Git Integration for Jira, GitLens for VS Code & IntelliJ (soon).