Lesson 1
Using the "git diff" Command
Introduction

Welcome to the very first lesson of the "Advanced Git Features" course! This lesson focuses on the git diff command, a vital tool in Git that helps you navigate and understand changes in your codebase across different versions. Being able to efficiently compare these versions is crucial for maintaining a clear code history and collaborating effectively within a development team. By mastering git diff, you will enhance your ability to review changes, track modifications across versions, and seamlessly manage your projects. This lesson will guide you through using git diff to handle various states in your repository, whether it's unstaged changes, staged changes, or differences between branches and commits. Let’s dive into the details of using git diff effectively.

Understanding "git diff" Basics

git diff is a command-line tool used to see the differences between various states of a git repository, particularly useful for reviewing changes before they are committed. It provides a detailed view of modifications made to your files but not yet staged for the next commit. This assists developers in identifying which lines have been added, removed, or altered since the last commit.

The basic syntax:

Bash
1git diff

When executed, git diff compares the current files in your working directory with the last committed snapshot. It highlights lines that have been added, removed, or modified. Let's consider this example comparing two versions of a file example.txt:

Version 1:

Plain text
1Hello, World! 2This is a line. 3Goodbye, World!

Version 2:

Plain text
1Hello, Universe! 2This is a line. 3This is an additional line. 4Farewell, World! 5See you soon!
Reading "git diff" Output

When you run git diff, the command produces a detailed output displaying the changes, typically resembling the following format:

Plain text
1diff --git a/example.txt b/example.txt 2index 83db48f..f9c2d9a 100644 3--- a/example.txt 4+++ b/example.txt 5@@ -1,3 +1,5 @@ 6-Hello, World! 7+Hello, Universe! 8 This is a line. 9+This is an additional line. 10-Farewell, World! 11+Goodbye, World! 12+See you soon!

Let's break down the different parts of this output:

  • The diff --git a/example.txt b/example.txt line indicates the file path in the repository and shows that changes are being compared for example.txt.

  • The index 83db48f..f9c2d9a line shows two unique identifiers for the content of the file before and after the changes. Think of them as checkpoints that help Git track which versions of the file are being compared.

  • The 100644 part indicates the file's permissions, telling us that it's a regular file (not executable) and has standard permissions: the owner can read and write, while others can only read.

  • The lines prefixed with --- and +++ indicate the file before and after changes. Specifically, --- a/example.txt represents the original file (a/ is a common convention for the "old" file in diffs), while +++ b/example.txt shows the file with modifications (b/ represents the "new" file).

  • Lines starting with @@ provide the line numbers of the changes in the format @@ -<start line>,<lines count> +<start line>,<lines count> @@. For example, in our output:

    Plain text
    1@@ -1,3 +1,5 @@

    This indicates that from the original file, starting from line 1, a total of 3 lines are being compared (-1,3). In the modified file, starting from line 1, a total of 5 lines are being compared (+1,5) due to the additional lines added.

  • A line beginning with - shows lines removed from the file, while lines with + represent additions.

By default, git diff uses a unified format to display changes, making it easier to review before staging.

Using "git diff HEAD"

So far, we've looked at git diff for viewing unstaged changes. Now, let’s explore how git diff can be used to review both staged and unstaged changes in comparison to the latest commit (HEAD).

The git diff HEAD command compares all current changes in your working directory with the latest commit. This includes both staged and unstaged modifications, giving you a complete view of what's different since your last commit.

Command:

Bash
1git diff HEAD

This command is particularly useful when you want a comprehensive overview of all changes, helping you decide what needs to be committed or adjusted before finalizing your updates.

Viewing Staged Differences

In addition to viewing all changes, Git also lets us focus solely on staged changes — the updates we've marked for the next commit. This is useful for verifying what will be committed without including any unstaged changes.

To view only staged differences, use either git diff --staged or git diff --cached. Both commands serve the same purpose, showing only the staged modifications in comparison to the latest commit.

Commands:

Bash
1git diff --staged

or equivalently:

Bash
1git diff --cached

By running one of these commands, you can confirm that all intended changes are staged and ready for commit, ensuring you’re in control of what enters your project history.

Comparing Specific Branches Using "git dif"

Beyond viewing changes in your working directory, git diff also allows you to compare versions between branches or specific commits. This feature is invaluable for understanding differences in code across multiple versions and evaluating what would change if branches were merged.

To see the differences between two branches, use the following syntax:

Bash
1git diff branch1..branch2

This command shows what would change if you merged branch2 into branch1. It's a helpful way to assess and review differences before performing a merge or rebase, allowing you to better manage your project's version history.

An alternative way to achieve the same result without using the .. syntax is to directly specify the two branch names, separated by a space:

Bash
1git diff branch1 branch2

This alternative command also compares the differences in branch2 against branch1, allowing you to evaluate what changes exist before a potential merge.

Comparing Specific Commits Using "git diff"

Similarly, you can compare two specific commits using:

Bash
1git diff commit1..commit2

This command highlights changes made between two points in your project's history. It’s useful for debugging or tracking the development of specific features over time. The commits don’t have to be on the same branch; you can use this command to compare any two commits in your repository, regardless of their branch.

By mastering these commands, you can easily evaluate and manage the evolution of your project, ensuring smooth and predictable integrations between different versions.

Specifying File Names

You can use git diff to focus on changes in specific files, allowing you to narrow down your review to targeted parts of your codebase. This is especially helpful in larger projects where focusing on relevant files can save time and reduce noise in your comparison.

To specify a file, use:

Bash
1git diff HEAD -- file-name.txt

This command compares changes in file-name.txt between your working directory and the latest commit (HEAD). It allows you to focus on modifications in specific files without viewing all changes in the repository.

Handling Non-Existent Files

When using git diff, you may encounter files that have been newly added or deleted. Here’s how git diff represents these changes:

  • New Files: If a file is newly created and hasn’t been in a previous commit, git diff shows its entire content as additions. For example, if new-file.txt is added, the output might look like this:

    Plain text
    1diff --git a/new-file.txt b/new-file.txt 2new file mode 100644 3index 0000000..e69de29 4--- /dev/null 5+++ b/new-file.txt 6@@ -0,0 +1,3 @@ 7+This is the first line of the new file. 8+Here’s another line. 9+And one more for good measure.

    In this example:

    • new file mode 100644 indicates that a new file with default permissions has been created.
    • --- /dev/null indicates that there was no previous version of this file (non-existent).
    • Lines prefixed with + show the content of the new file as additions.
  • Deleted Files: If a file has been deleted, git diff displays the entire file content as deletions. For example, if old-file.txt is deleted, the output might look like this:

    Plain text
    1diff --git a/old-file.txt b/old-file.txt 2deleted file mode 100644 3index e69de29..0000000 4--- a/old-file.txt 5+++ /dev/null 6@@ -1,3 +0,0 @@ 7-This is the first line of the deleted file. 8-This line is also in the deleted file. 9-Goodbye, old file.

    In this example:

    • deleted file mode 100644 indicates that the file has been removed.
    • +++ /dev/null represents the absence of the file after deletion.
    • Lines prefixed with - show the content of the deleted file as removed.

These examples make it easy to understand how git diff tracks changes for newly added or deleted files, allowing you to confirm additions and removals within your project.

For more detailed information and additional options, you can refer to the git diff documentation.

Summary

In this lesson, we've delved into the git diff command's capabilities, learning how to inspect changes in various contexts — from checking in-progress work to evaluating branch divergences. Understanding these concepts is a big step towards maintaining clean and understandable project histories. Now that you're familiar with git diff, We encourage you to proceed to the hands-on practice exercises on the CodeSignal IDE to cement your skills. As you explore different scenarios, remember that git diff is your tool for insight, ensuring your projects' integrity and facilitating teamwork. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.