Understanding "git diff"
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.
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.pyAnd 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.pycommit 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.pyFollowing 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 diffFor 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 --stagedThis 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 def456This 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^ ghi789git diff ghi789~ ghi789git show ghi789Above are the most common ways to compare changes between any commit and it’s parent commit.
5. Comparing Branches
git diff main feature-branchAssume 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.pyThis 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.pyThis 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 def456If 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 ghi789This 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 ghi78910. Generating a Patch
git diff > changes.patchThis command generates a patch file containing all the changes, which can be applied later using git apply.
11. Viewing Statistics
git diff --statThis 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 :)







