The git diff command is used to compare changes in your Git project. It helps you see exactly what has been added, removed, or modified in files.
git diff shows changes
in the working directory that have not yet been staged.
git diff is used to compare different versions or states of files in a Git repository.
git diff
It is especially useful for reviewing changes before creating a commit.
Before committing changes, you may want to know exactly what you changed.
The git diff command helps you review those changes.
The simplest form is:
git diff
This normally compares the working tree with the staged version of the files.
Git commonly works with three important areas:
Working Directory
↓
Staging Area
↓
Repository
The basic git diff command helps compare changes in the
working directory with the staging area.
A diff output shows lines that have changed. For example:
-Old text
+New text
The minus sign generally represents removed content and the plus sign represents added content.
A line beginning with - in a diff normally represents content that was removed from one side of the comparison.
-Welcome to our website
A line beginning with + normally represents content that was added.
+Welcome to our new website
git diff
Suppose you change:
<h1>Welcome</h1>
to:
<h1>Welcome to Soopro Pathshala</h1>
The diff can show the old and new lines so you can review the change.
A common workflow is:
git status
git diff
The first command shows the repository state, while the second shows unstaged changes.
You can compare changes for a specific file:
git diff -- index.html
The -- separates Git revision arguments from the file path.
Once a change is staged with:
git add index.html
the normal git diff command will not normally show that
staged change.
To inspect staged changes, use:
git diff --staged
The --staged option compares the staging area with the current commit.
git diff --staged
It is useful for reviewing what will be included in the next commit.
git diff --cached is another form used to display staged changes.
git diff --cached
It is equivalent to using git diff --staged.
You can compare the working tree with the current commit using:
git diff HEAD
This includes changes that are both unstaged and staged.
Git can compare two specific commits:
git diff commit1 commit2
Replace the placeholders with actual commit identifiers.
git log --oneline
git diff 1a2b3c4 5d6e7f8
This compares the project states represented by the two specified commits.
You can compare two branches:
git diff main feature-login
This shows differences between the tips of the two specified branches.
If you only want to see which files changed, use:
git diff --name-only
This displays the names of changed files instead of the complete line-by-line differences.
The --stat option displays a summary of changes:
git diff --stat
It can show how many files changed and the number of insertions and deletions.
The --word-diff option can show changes at the word level instead of primarily displaying complete changed lines.
git diff --word-diff
Git can display diff output with color highlighting when the terminal supports it.
git diff --color=always
Terminal color settings can affect how the output is displayed.
A useful workflow is to inspect your changes before committing:
git status
git diff
git add .
git diff --staged
git commit -m "Update project"
This gives you an opportunity to review both unstaged and staged changes.
When lines are removed, diff output can show them with a minus sign.
-This is the old heading
This makes it easier to identify what was removed from a file.
New lines are normally shown with a plus sign.
+This is the new heading
You can compare the added content with the previous version.
Diff output normally includes surrounding unchanged lines to provide context for the changed lines.
<div>
-Old content
+New content
</div>
The exact amount of context depends on the diff options being used.
git diff to show staged changes by default.git diff --staged for staged changes.git diff
git diff --staged
git diff --cached
git diff HEAD
git diff -- index.html
git diff --name-only
git diff --stat
git diff commit1 commit2
git diff main feature-login
# Check status
git status
# View unstaged changes
git diff
# Stage changes
git add .
# View staged changes
git diff --staged
# Commit
git commit -m "Update project"
This is a useful workflow for reviewing changes before creating a commit.
git init
touch index.html
git add index.html
git commit -m "Create homepage"
# Edit index.html
git status
# View unstaged changes
git diff
# Stage changes
git add index.html
# View staged changes
git diff --staged
git commit -m "Update homepage"
This example demonstrates how git diff can be used before
and after staging changes.
The git diff command is an important tool for reviewing changes in a Git repository.
git diff
To review staged changes:
git diff --staged
To compare two commits:
git diff commit1 commit2
Using git diff before committing helps you understand
exactly what changes will be recorded in your project history.
git diff --staged shows staged changes.git diff --cached is another form for viewing staged changes.git diff HEAD compares the working tree with the current commit.git diff commit1 commit2.git diff main feature-login.git diff --name-only shows changed file names.git diff --stat shows a summary of changes.git diff to review changes before committing.Question: Which command is used to view unstaged changes in the working directory?