git diff Command
git diff
Command
The git diff
command is used to show differences between various states of the repository. It compares changes between files or commits, providing a detailed view of what has been added, modified, or deleted.
What Does git diff
Show?
When you run git diff
, Git provides a line-by-line comparison of the differences between two sets of files. Depending on the context, it can compare:
- Working Directory vs. Staging Area: Shows changes made in the working directory that have not yet been staged.
- Staging Area vs. Last Commit: Shows changes that are staged but not yet committed.
- Two Commits: Shows differences between two specific commits.
- Branch Differences: Shows differences between two branches.
Basic Syntax
git diff
This default command shows the changes between the working directory and the staging area.
Examples
Show Unstaged Changes
To see what has been modified in your working directory but not yet staged:
git diff
This shows the differences between the files in your working directory and the files in the staging area.
Show Staged Changes
To see what changes are staged and will be included in the next commit:
git diff --cached
or
git diff --staged
This compares the staged changes to the last commit.
Compare Two Commits
To see the differences between two specific commits:
git diff <commit1> <commit2>
Example:
git diff HEAD~2 HEAD
This compares the commit two steps before the current HEAD to the current HEAD.
Compare Two Branches
To see the differences between two branches:
git diff <branch1>..<branch2>
Example:
git diff main..feature-branch
This compares the main
branch to the feature-branch
.
Show Differences for a Specific File
To see the changes made to a specific file:
git diff <file>
Example:
git diff src/app.js
This shows the changes made to src/app.js
that are not yet staged.
Show Differences with a Specific Output Format
Unified Format: The default format shows lines with
+
for additions and-
for deletions.Side-by-Side Format: Use the
--side-by-side
option to see differences in a side-by-side format:git diff --side-by-side
Word-wise Differences: Use the
--word-diff
option to see differences at the word level:git diff --word-diff
Diff with Color or Ignore Whitespace
Color Output: By default,
git diff
uses color to highlight differences. You can force color output with--color
:git diff --color
Ignore Whitespace Changes: To ignore changes that only affect whitespace:
git diff -w
Summary
- Purpose:
git diff
shows the differences between various states of your files or commits, allowing you to review what has changed. - Basic Syntax:
git diff
(for working directory vs. staging area),git diff --cached
(for staged vs. last commit),git diff <commit1> <commit2>
(for comparing commits),git diff <branch1>..<branch2>
(for comparing branches). - Usage: Use
git diff
to review changes before staging or committing, compare different commits or branches, and view differences at a granular level.
The git diff
command is a powerful tool for examining and understanding the changes in your repository, helping you to track modifications and ensure that your commits reflect the intended updates.