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:
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:
Version 2:
Reading "git diff" Output
When you run git diff, the command produces a detailed output displaying the changes, typically resembling the following format:
Let's break down the different parts of this output:
-
The
diff --git a/example.txt b/example.txtline indicates the file path in the repository and shows that changes are being compared forexample.txt. -
The
index 83db48f..f9c2d9aline 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
100644part 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.txtrepresents the original file (a/is a common convention for the "old" file in diffs), while+++ b/example.txtshows 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:plaintextThis 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.
