Lesson 15 of 60 – Git Diff
25%

Git Diff

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.

Note: By default, git diff shows changes in the working directory that have not yet been staged.

1. What is git diff?

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.

2. Why Use git diff?

Before committing changes, you may want to know exactly what you changed.

The git diff command helps you review those changes.

  • Find modified lines
  • Review additions
  • Review deletions
  • Compare file versions
  • Review changes before committing

3. Basic Syntax

The simplest form is:

git diff

This normally compares the working tree with the staged version of the files.

4. Working Directory and Staging Area

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.

5. Understanding Diff Output

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.

6. The Minus Sign in Diff

A line beginning with - in a diff normally represents content that was removed from one side of the comparison.

-Welcome to our website

7. The Plus Sign in Diff

A line beginning with + normally represents content that was added.

+Welcome to our new website

8. Example of git diff

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.

9. Diff Before Staging

A common workflow is:

git status
git diff

The first command shows the repository state, while the second shows unstaged changes.

10. Diff for a Specific File

You can compare changes for a specific file:

git diff -- index.html

The -- separates Git revision arguments from the file path.

11. Diff After Staging

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

12. 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.

13. git diff --cached

git diff --cached is another form used to display staged changes.

git diff --cached

It is equivalent to using git diff --staged.

14. Comparing Working Directory with Last Commit

You can compare the working tree with the current commit using:

git diff HEAD

This includes changes that are both unstaged and staged.

15. Comparing Two Commits

Git can compare two specific commits:

git diff commit1 commit2

Replace the placeholders with actual commit identifiers.

16. Example of Comparing Commits

git log --oneline

git diff 1a2b3c4 5d6e7f8

This compares the project states represented by the two specified commits.

17. Comparing Branches

You can compare two branches:

git diff main feature-login

This shows differences between the tips of the two specified branches.

18. Viewing File Names Only

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.

19. Viewing Change Statistics

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.

20. Viewing Word-Level Differences

The --word-diff option can show changes at the word level instead of primarily displaying complete changed lines.

git diff --word-diff

21. Diff with Color

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.

22. Reviewing Before Commit

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.

23. Git Diff and Deleted Lines

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.

24. Git Diff and Added Lines

New lines are normally shown with a plus sign.

+This is the new heading

You can compare the added content with the previous version.

25. Git Diff and Context Lines

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.

26. Common git diff Mistakes

  • Expecting git diff to show staged changes by default.
  • Forgetting to use git diff --staged for staged changes.
  • Comparing the wrong branches or commits.
  • Running the command outside a Git repository.
  • Confusing working-directory changes with committed changes.
  • Not reviewing changes before committing.

27. Useful git diff Commands

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

28. Complete Diff Workflow

# 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.

29. Practical Example

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.

30. Summary of git diff

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.

📌 Key Points

  • git diff compares changes in a Git repository.
  • By default, it shows unstaged working-directory changes.
  • 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.
  • You can compare two commits using git diff commit1 commit2.
  • You can compare branches using git diff main feature-login.
  • git diff --name-only shows changed file names.
  • git diff --stat shows a summary of changes.
  • Use git diff to review changes before committing.

🧠 Quick Quiz

Question: Which command is used to view unstaged changes in the working directory?