MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Comparing Changes with git diff (Working Tree vs Staged vs HEAD)0%
Study Materials
Git & GitHub BasicsProgress 0%
1.1What is Version Control? (Local, Centralized vs Distributed VCS)1.2Why Git? History, Architecture & Core Benefits1.3The Three Trees: Working Directory, Staging Area & Git Repository1.4Installing Git and Initial Configuration (git config user.name / email)
3.1Inspecting Commit History with git log and Formatting Flags3.2Comparing Changes with git diff (Working Tree vs Staged vs HEAD)3.3Ignoring Unwanted Files and Secrets using .gitignore Rules
HomeTutorialsGit & GitHub BasicsComparing Changes with git diff (Working Tree vs Staged vs HEAD)
Ignoring Unwanted Files and Secrets using .gitignore Rules

Comparing Changes with git diff (Working Tree vs Staged vs HEAD)

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Before staging or committing code, careful developers verify their exact modifications. The git diff command is Git's microscope: it shows line-by-line additions, deletions, and alterations using unified diff format.


1. The Three Forms of git diff

Because Git has three trees (Working Directory, Staging Area, and HEAD Commit), git diff provides three primary comparison targets:

1[ Working Directory ] <====== 1. git diff ==============> [ Staging Area ]
2 ^
3 |
4 2. git diff --staged
5 |
6 v
7 <====== 3. git diff HEAD =========> [ HEAD (Last Commit) ]

1. git diff (Working Directory vs. Staging Area)

Shows changes you have made in files that have NOT yet been staged:

1git diff

2. git diff --staged (Staging Area vs. Last Commit)

Shows changes that are currently staged and ready to be included in the next commit:

1git diff --staged
2# Synonym:
3git diff --cached

3. git diff HEAD (Working Directory vs. Last Commit)

Shows all changes on your disk (both staged and unstaged combined) compared against the last recorded commit:

1git diff HEAD

2. Reading Unified Diff Syntax

When you run git diff, Git outputs unified diff notation:

1diff --git a/src/app.py b/src/app.py
2index e69de29..b2d9a10 100644
3--- a/src/app.py
4+++ b/src/app.py
5@@ -10,7 +10,8 @@ def calculate_discount(price, customer_type):
6- if customer_type == "regular":
7- return price * 0.05
8+ if customer_type == "vip":
9+ return price * 0.20
10+ elif customer_type == "regular":
11+ return price * 0.10
12 return 0

Decoding the Diff:

  • --- a/src/app.py : Original version before changes (pre-image).
  • +++ b/src/app.py : Modified version after changes (post-image).
  • @@ -10,7 +10,8 @@ : Hunk header. In the original file, chunk begins at line 10 and spans 7 lines; in the new file, chunk begins at line 10 and spans 8 lines.
  • - (Red) : Removed / deleted lines.
  • + (Green) : Added / inserted lines.
  • White / uncolored text : Unchanged context lines around the modification.

3. Targeted Diffs

Compare a Specific File:

1git diff src/components/Button.jsx

Compare Two Different Branches:

See what changes exist in feature-login compared to main:

1git diff main..feature-login

Compare Two Commits by Hash:

1git diff 3a89e12..7b3f81e

Word-Level Diff (--word-diff):

When you change only one word inside a long paragraph, viewing the entire line as deleted and re-added can be noisy:

1git diff --word-diff

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does running git diff without any arguments compare?
Question 2
Which command shows the exact diff of changes that are already staged and about to be committed?
Question 3
In a Git diff output, what do lines beginning with a green + signify?
Question 4
What does git diff HEAD display?
Question 5
How can you view the changes introduced between two branches named dev and main?
Next Lesson

Ignoring Unwanted Files and Secrets using .gitignore Rules

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Inspecting Commit History with git log and Formatting FlagsIgnoring Unwanted Files and Secrets using .gitignore Rules

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Git & GitHub Basics with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext