Inspecting Changes (git diff)
๐ข Beginner
๐ Definition
git diff is the command used to inspect line-by-line changes made to files across your Working Directory, Staging Area, previous commits, or different branches, displaying additions, deletions, and modifications before committing.
๐ Multilingual Explanation
English
git diff compares file contents line-by-line. Running git diff without flags compares your Working Directory changes against the Staging Area. Running git diff --staged (or --cached) compares your Staging Area against the latest commit snapshot (HEAD).
Hindi
git diff command aapke files ke line-by-line badlav (additions/deletions) dikhata hai. Bina flags ke git diff run karne par Working Directory aur Staging Area ke beech ka farak dikhta hai. Staged changes check karne ke liye git diff --staged run karein.
Marathi
git diff mhanje don files madhil kiva commit madhil badal (changes) pahane. Terminal madhye hirvya (green) rangat navin lines (+) aani lal (red) rangat kadhlesha lines (-) distat. Staged files pahanyasathi git diff --staged vaparatat.
Hinglish
Commit karne se pehle kya-kya changes huye hain woh dekhne ke liye git diff use karo. Green lines (+) added code dikhati hain aur Red lines (-) deleted code. git diff --staged se aap Staging Area ka diff dekh sakte hain.
๐ค Why Do We Use It?
Reviewing diffs before staging or committing prevents accidental code deletions, unwanted debug console.log() statements, and formatting mistakes from polluting repository history.
๐ง Simple Explanation
Think of git diff like a Track Changes feature in Microsoft Word or Google Docs. It highlights newly inserted text in green (+) and removed text in red (-).
๐ Key git diff Commands & Modes
# 1. Compare Working Directory vs Staging Area (Unstaged changes)
git diff
2. Compare Working Directory for a single specific file
git diff index.html
3. Compare Staging Area vs Last Commit HEAD (Staged changes)
git diff --staged
4. Compare two specific commits using their SHA hashes
git diff a1b2c3d f9e8d7c
5. Compare two branches (e.g. main vs feature branch)
git diff main feature-login
6. Show summary list of changed files only (without line diffs)
git diff --stat
๐ก Practical Example
Inspecting code edits in terminal:
# 1. Edit index.html
echo "<h2>Subheading Added</h2>" >> index.html
2. View unstaged diff
git diff
๐ Understanding Unified Diff Output Format
diff --git a/index.html b/index.html
index e69de29..b45c21f 100644
--- a/index.html <-- Original file version in Staging/HEAD
+++ b/index.html <-- New file version in Working Directory
@@ -1,3 +1,4 @@ <-- Line numbers chunk header (-1,3 lines changed to +1,4 lines)
<h1>Welcome Page</h1>
<p>Original text</p>
+<h2>Subheading Added</h2> <-- Line added (Green +)
๐ Terminal Output
$ git diff
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
# My Practice Project
+Author: Rahul Sharma
โ ๏ธ Common Mistakes
- Blank Output Confusion: Running
git diffafter executinggit add .and seeing no output at all! - *Reason:* Plain
git diffONLY compares Working Directory against Staging Area. Once files are staged,git diffreturns empty. - *Fix:* Use
git diff --stagedto view staged changes! - Panic in Diff Pager: Getting stuck in terminal view when diff output is long. Press
qto exit.
๐ก๏ธ Safety / Important Notes
- Always run
git difforgit diff --stagedbefore executinggit committo ensure your commit message accurately describes the staged changes.
๐ Real-World Usage
Pull Requests on GitHub, GitLab, and Bitbucket use the git diff engine to present clean visual side-by-side code reviews to reviewers before merging code into main branches.
๐งช Try It Yourself
1. Modify README.md in your git-practice directory by adding a line at the bottom.
2. Run git diff and observe the green + additions.
3. Stage README.md with git add README.md and run git diff. Notice it is empty.
4. Run git diff --staged and see your changes again.
๐ฏ Mini Challenge
Run git diff --stat on a modified file and note down how many insertions (+) and deletions (-) are summarized.
๐ Related Topics
๐งญ Navigation
โ Home | โ Previous: Gitignore | Next: Undoing Changes โ