Learning Git: What is a Diff?

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

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

  • 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

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

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

Viewing the Diff of Two Branches

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

Code with Confidence with GitKraken

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

GitKraken, the downright luxurious Git GUI client for Windows, Mac and Linux. GitKraken makes Git commands and processes easy, fast, and intuitive.