Sitemap
Darshan Chaudhari

Darshan Chaudhari

Software Developer

Understanding "git diff"

3 min readJun 27, 2024

--

At its core, the git diff command is used to display changes between different commits, branches, files, or the working directory and the staging area. It compares the content and shows the differences line by line, highlighting what has been added, modified, or deleted.

Press enter or click to view image in full size

Example Project

Assume we have a Git repository with the following structure and initial content:

MyApp/
├── README.md
├── src/
│ ├── main.py
│ └── utils.py
└── test/
└── test_main.py

And you have 3 commits so far as below-

commit abc123
Author: Dev One
Date: 2023-01-01

Initial commit

README.md
src/main.py
src/utils.py
test/test_main.py
commit def456
Author: Dev Two
Date: 2023-01-02

Added new feature

src/main.py
src/utils.py

commit ghi789
Author: Dev Three
Date: 2023-01-03

Bug fixes and refactoring

src/utils.py
test/test_main.py

Following the above example, we will understand all the important use cases for the git diff command.

1. See changes in the working directory that are not yet staged.

git diff

For example, if we modified src/main.py but haven't staged the changes, running git diff will show those modifications.

2. Comparing staged changes

git diff --staged

This command shows the differences between the staging area and the last commit. If we staged changes in src/main.py, this command would display those changes.

3. Comparing Different Commits

git diff abc123 def456

This command compares changes between the initial commit (abc123) and the second commit (def456). It will show what was added, removed, or modified in that range.

4. Comparing the Most Recent Commit with Its Previous Commit

git diff ghi789^ ghi789
git diff ghi789~ ghi789
git show ghi789

Above are the most common ways to compare changes between any commit and it’s parent commit.

5. Comparing Branches

git diff main feature-branch

Assume we have a branch called feature-branch. This command compares the changes between the main branch and feature-branch.

6. Comparing with a Specific File

git diff src/main.py

This shows the changes in src/main.py between the working directory and the staging area.

7. Comparing a file between commits

git diff abc123 def456 -- src/utils.py

This command compares the changes in src/utils.py between the initial commit (abc123) and the second commit (def456).

8. Comparing the Working Directory with a commit

git diff def456

If we run this command, it will compare the current working directory with the state of the repository at the commit def456.

9. Comparing the staging area with the commit

git diff --cached ghi789

This command compares the staged changes with the state of the repository at the commit ghi789. Alternatively, you can use the — staged flag instead of — cached.

git diff --staged ghi789

10. Generating a Patch

git diff > changes.patch

This command generates a patch file containing all the changes, which can be applied later using git apply.

11. Viewing Statistics

git diff --stat

This command provides a summary of changes, showing the number of insertions and deletions for each file.

Get Darshan Chaudhari’s stories in your inbox

Join Medium for free to get updates from this writer.

And that’s it.
You just mastered git diff :)

--

--