← Back to Git Course | Chapter 2: Basic Commands | Lesson 12 of 18

Git diff Command

git diff एक spot-the-difference game जैसा है: यह exactly दिखाता है कि आपने last save के बाद कौन सी lines add या remove कीं।
Syntax
bash
git diff
git diff --staged
git diff commit1 commit2

Unstaged Changes देखना

बिना arguments के git diff आपकी Working Directory में हर वह change दिखाता है जो अभी staged नहीं हुआ, आपकी current files को staging area में पहले से मौजूद के against line-by-line compare करते हुए (या last commit, अगर कुछ भी staged न हो)।

उदाहरण: Viewing Unstaged Changes

bash
git diff

Staged Changes देखना

git diff --staged (या equivalent --cached) opposite view दिखाता है: exactly क्या आपकी अगली commit में जाने वाला है, staging area को आपकी last commit के against compare करते हुए -- actually commit करने से ठीक पहले का review step।

उदाहरण: Viewing Staged Changes

bash
git diff --staged

दो Commits Compare करना

git diff <commit1> <commit2> आपके project की history में किन्हीं भी दो points को सीधे compare करता है, चाहे वे कितने भी दूर हों या किन branches पर हों, आपको उनके बीच पूरा accumulated difference देखने देते हुए।

उदाहरण: Comparing Two Commits

bash
git diff a1b2c3d e4f5g6h

Commits में Files Compare करना

किसी diff command में एक file path append करना -- git diff commit1 commit2 -- path/to/file -- comparison को सिर्फ उस एक file तक narrow करता है, जो एक पूरी commit में changes scroll करने से कहीं ज़्यादा आसान पढ़ता है।

उदाहरण: Comparing Files Across Commits

bash
git diff a1b2c3d e4f5g6h -- path/to/file.txt

Word-Level Diff

Default रूप से git diff पूरी बदली हुई lines highlight करता है भले ही सिर्फ एक word अलग हो; git diff --word-diff इसके बजाय एक line के अंदर बदले specific words highlight करता है, जो prose या लंबे single-line statements के लिए कहीं ज़्यादा clear है।

उदाहरण: Word-Level Diff

bash
git diff --word-diff
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. git add के बाद git diff चलाना और कुछ न देखना, क्योंकि यह working directory को staging area से compare करता है; git diff --staged इस्तेमाल करें।
  2. git diff को git status से confuse करना, जबकि git diff line-level changes दिखाता है और git status सिर्फ file names दिखाता है।
  3. - और + lines को उल्टा पढ़ना, जबकि - एक हटाई गई line mark करता है और + एक add की गई।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.