Basics
Git Diff
Viewing Changes
Git diff shows differences between working directory and staging.
What is Git Diff?
Git diff is a powerful command that allows developers to see the differences between two data sets. Specifically, it shows changes between the working directory, staging area, and the most recent commit. This is essential for tracking modifications and ensuring that only the intended changes are committed to the repository.
Basic Usage of Git Diff
The basic command git diff
can be used to view changes that have been made but not yet staged for commit. These are the differences between the working directory and the staging area.
Viewing Staged Changes
To see the changes that are staged for the next commit, use the --cached
option. This will display the differences between the staging area and the last commit.
Comparing Changes with a Specific Commit
You can compare changes in your working directory with a specific commit by providing its hash. This shows how the working directory differs from that particular commit.
Comparing Two Commits
To see the differences between two specific commits, you can use the following syntax. Replace commit1
and commit2
with the actual commit hashes.
Understanding the Output of Git Diff
The output of git diff
can be overwhelming at first, but it follows a consistent pattern. Lines prefixed with a minus sign (-) indicate deletions, while lines with a plus sign (+) show additions. This format helps you quickly identify what has changed.
Common Options with Git Diff
Here are some common options you can use with git diff
to customize its output:
--name-only
: Show only names of changed files.--color
: Highlight changes in color for better readability.--stat
: Display a summary of changes.