Git: مقارنة Git Diff وتحليل التغييرات

تعد مقارنة الاختلافات أداة مهمة لفهم التغييرات التي تطرأ على الكود. فمن خلال مقارنة الاختلافات بين الإصدارات المختلفة، يمكنك أن ترى بوضوح ما الذي تم تغييره، ولماذا تم تغييره، ونطاق هذه التغييرات.

1. أساسيات مقارنة الاختلافات

(1) المجالات الثلاثة لـ Git

يحتوي Git على ثلاثة مجالات مهمة، وفهمها هو المفتاح لإتقان قراءة ملفات المقارنة (diffs):

100%
graph TB
    A[Workspace<br/>Working Directory] -->|git add| B[Buffer<br/>Staging Area]
    B -->|git commit| C[Repository<br/>Repository]
    
    A -.->|git diff| B
    B -.->|git diff --staged| C
    A -.->|git diff HEAD| C
    
    style A fill:#fff3cd
    style B fill:#d4edda
    style C fill:#c3e6cb

(2) سيناريوهات لمقارنة الاختلافات

(3) نظرة عامة على الأمر diff

git diff تُستخدم لإظهار الاختلافات. الاستخدام الأساسي:

BASH
git diff [options] [<commit>] [--] [<path>...]


2. مقارنة بين المناطق الثلاث

(1) الدليل العامل مقابل الدليل المؤقت

قارن الفروق بين دليل العمل ومنطقة التجهيز — أي التغييرات التي لم يتم تجهيزها بعد.

▶ مثال: عرض التغييرات التي لم يتم تضمينها في المرحلة

BASH
# Edit File
echo "new line" >> README.md

# View Workspace Changes(Relative to the temporary storage area)
git diff

# Output:
# diff --git a/README.md b/README.md
# index abc1234..def5678 100644
# --- a/README.md
# +++ b/README.md
# @@ -1,3 +1,4 @@
#  # My Project
#  
#  ## Features
# +new line

# View a Specific File
git diff README.md

# View Multiple Files
git diff file1.js file2.js

(2) منطقة التجهيز مقابل المستودع

قارن الفروق بين منطقة التجهيز والمستودع — أي التغييرات التي تم تجهيزها ولكن لم يتم تثبيتها بعد.

▶ مثال: عرض التغييرات المؤقتة

BASH
# Add or modify to the staging area
git add README.md

# View changes in the staging area(Compared to the latest submission)
git diff --staged

# Or use the old syntax
git diff --cached

# Output:
# diff --git a/README.md b/README.md
# index abc1234..def5678 100644
# --- a/README.md
# +++ b/README.md
# @@ -1,3 +1,4 @@
#  # My Project
#  
#  ## Features
# +new line

(3) الدليل العملي مقابل المستودع

قارن الفروق بين دليل العمل والمستودع — أي جميع التغييرات التي لم يتم تسجيلها بعد.

▶ مثال: عرض جميع التغييرات غير المحفوظة

BASH
# View the workspace relative toHEADthe differences
git diff HEAD

# View the differences between the workspace and a specific commit
git diff a1b2c3d

# View the differences in the workspace compared to the last commit
git diff HEAD^

# The output includes all changes, both staged and unstaged.


3. تفسير ناتج الفرق

(1) تنسيق إخراج diff

▶ مثال: فهم مخرجات الأمر diff

BASH
git diff

# Output Format Description:
# diff --git a/file.js b/file.js     <- GitDifferential Head
# index abc1234..def5678 100644      <- File Mode
# --- a/file.js                      <- Original Document(-Indicates deletion)
# +++ b/file.js                      <- New Document(+Indicates addition)
# @@ -1,3 +1,4 @@                     <- Change Location (hunk header)
#  # My Project                      <- Context Line(Starts with a space)
#  
#  ## Features                       <- Context Line
# +new line                          <- Add a row(+Introduction)
# -old line                          <- Delete Row(-Introduction)

(2) تنسيق رأس «Hunk»

تنسيق عنوان «hunk»: @@ -a,b +c,d @@

BASH
@@ -10,5 +10,7 @@ function login() {

وهذا يعني أن الأسطر الخمس التي تبدأ من السطر 10 في الملف الأصلي أصبحت 7 أسطر تبدأ من السطر 10 في الملف الجديد.

(3) المعلومات الإحصائية

▶ مثال: عرض ملخص للإحصائيات

BASH
# Display Statistics
git diff --stat

# Output:
#  README.md | 1 +
#  file1.js  | 5 ++---
#  file2.js  | 3 +++
#  3 files changed, 5 insertions(+), 3 deletions(-)

# Show brief statistics
git diff --shortstat

# Output:
# 3 files changed, 5 insertions(+), 3 deletions(-)

# Display Numerical Statistics
git diff --numstat

# Output:
# 1       0       README.md
# 2       3       file1.js
# 3       0       file2.js


4. مقارنة الإصدارات المختلفة

(1) قارن بين العمليتين

▶ مثال: مقارنة بين الطلبات

BASH
# Compare the two commits
git diff a1b2c3d d4e5f6g

# Compare a commit to the current workspace
git diff a1b2c3d

# Compare a commit to the staging area
git diff --staged a1b2c3d

# Compare Adjacent Commits
git diff HEAD^ HEAD

# View the changes introduced by a specific commit
git show a1b2c3d

# Equivalent to
git diff a1b2c3d^ a1b2c3d

(2) مقارنة الفروع

▶ مثال: مقارنة الفروع

BASH
# Compare Two Branches
git diff main feature

# ViewfeatureBranch relative tomainChanges to
git diff main...feature

# ViewmainBranch relative tofeatureChanges to
git diff feature...main

# Compare a specific file across branches
git diff main feature -- file.js

# View changes made after the branch was forked
git diff $(git merge-base main feature) feature

(3) علامات المقارنة

▶ مثال: مقارنة الإصدارات

BASH
# Compare the Two Versions
git diff v1.0.0 v2.0.0

# View the changes in a specific version
git diff v1.0.0 HEAD

# View the release notes
git diff v1.0.0 v2.0.0 --stat

# Output:
#  src/auth.js   | 25 +++++++++++++++++++++++++
#  src/user.js   | 10 +++++-----
#  package.json  |  2 +-
#  3 files changed, 32 insertions(+), 5 deletions(-)


5. شرح تفصيلي لخيار diff

(1) الخيارات الشائعة

الخيار الوصف الغرض
--staged منطقة التجهيز مقابل المستودع عرض التغييرات المُجهَّزة
--stat عرض الإحصائيات عرض الإحصائيات وتعديلها
--shortstat إحصائيات موجزة عرض إجمالي التغييرات
--name-only عرض أسماء الملفات فقط عرض قائمة الملفات التي تم تعديلها
--name-status حالة العرض عرض حالة تعديل الملف
-w تجاهل المسافات تجاهل الاختلافات بين المسافات وعلامات الجدولة

▶ مثال: استخدام الخيار «diff»

BASH
# Show only the names of modified files
git diff --name-only

# Output:
# README.md
# file1.js
# file2.js

# Display File Modification Status
git diff --name-status

# Output:
# M  README.md     <- Edit
# A  file1.js      <- New
# D  file2.js      <- Delete
# R  old.js new.js <- Rename

# Ignore differences in whitespace characters
git diff -w

# Ignore all spaces
git diff -w --ignore-blank-lines

# Show differences at the word level
git diff --word-diff

# Highlight in color
git diff --color-words

(2) Context Control

▶ Example: Controlling the Number of Lines in the Context

BASH
# Reduce the number of context lines
git diff -U1

# The output displays only1Line Context:
# @@ -10,3 +10,4 @@ function login() {
#   const user = authenticate();
# +  if (!user) return;
#   return user;

# Do not display context
git diff -U0

# Increase the number of context lines
git diff -U10

(3) Path Restrictions

▶ Example: Limiting the Comparison Range

BASH
# Compare only specific files
git diff -- file.js

# Compare only specific directories
git diff -- src/

# Exclude certain files
git diff -- . ':!*.test.js'

# Compare only a specific type of file
git diff -- '*.js'


6. Real-World Applications

(1) Pre-submission Check

▶ Example: Thoroughly check before submitting

BASH
# 1. View Uncommitted Changes
git diff

# 2. View Staged Changes
git diff --staged

# 3. View Statistics on All Changes
git diff HEAD --stat

# 4. Submit after verifying that everything is correct
git commit -m "feat: Add a New Feature"

(2) Code Review

▶ Example: Reviewing Someone Else's Code

BASH
# View the changes in a specific commit
git show a1b2c3d

# View statistics for a specific commit
git show --stat a1b2c3d

# View the list of files modified in a specific commit
git show --name-only a1b2c3d

# Compare the differences between the two branches
git diff main...feature

# ViewPull Requestthe differences
git diff origin/main...origin/feature

(3) Conflict Analysis

▶ Example: Analyzing Merge Conflicts

BASH
# View the content to be merged
git diff main...feature

# View common ancestors up tofeatureChanges to
git diff $(git merge-base main feature) feature

# View files that may be in conflict
git diff --name-only main...feature

# View the specific details of the conflict
git diff main feature -- file.js

(4) Confirmation of Version Rollback

▶ مثال: التحقق من محتوى التراجع

BASH
# View content that would be lost if you undo the action
git diff HEAD HEAD~1

# View rollback statistics
git diff HEAD HEAD~1 --stat

# View the revision history for a specific file
git diff HEAD HEAD~1 -- file.js

# Execute the rollback after confirmation
git reset --hard HEAD~1


❓ أسئلة شائعة

س ما الفرق بين git diff و git diff --staged و git diff HEAD؟
ج git diff مقارنة دليل العمل ومنطقة التجهيز (التغييرات غير المُجهَّزة)؛ git diff --staged مقارنة منطقة التجهيز والمستودع (التغييرات المُجهَّزة)؛ git diff HEAD مقارنة دليل العمل والمستودع (جميع التغييرات غير المُرسلة).
س كيف يمكنني عرض أسماء الملفات التي تم تعديلها فقط دون الاطلاع على محتوياتها الفعلية؟
ج استخدم git diff --name-only لعرض أسماء الملفات فقط، أو استخدم git diff --stat لعرض أسماء الملفات وإحصائيات التعديل.
س ما معنى الرمزين "+" و"-" في ناتج أداة المقارنة؟
ج تشير الأسطر التي تبدأ بعلامة "+" إلى محتوى جديد، وتشير الأسطر التي تبدأ بعلامة "-" إلى محتوى محذوف، أما الأسطر التي تبدأ بمسافة فهي النص الأصلي (دون تغيير).
س كيف يمكنني مقارنة الاختلافات بين فرعين؟
ج استخدم git diff main feature لمقارنة فرعين، واستخدم git diff main...feature لعرض التغييرات في فرع الميزة مقارنةً بالفرع الرئيسي (بدءًا من نقطة الانقسام).
س كيف يمكنني تجاهل الاختلافات في المسافات؟
ج استخدم git diff -w لتجاهل الاختلافات بين المسافات وعلامات الجدولة، واستخدم git diff --ignore-space-at-eol لتجاهل الاختلافات في المسافات الختامية.

📖 ملخص


📝 تمارين

  1. تمرين أساسي: أنشئ مستودع Git. بعد تعديل ملف ما، استخدم git diff لعرض التغييرات التي لم يتم إعدادها للنشر. بعد إعداد التغييرات للنشر، استخدم git diff --staged لعرض التغييرات المعدة للنشر.

  2. تمرين متقدم: أنشئ فرعين، وقم بإجراء تغييرات على نفس الملف في كل من الفرعين، واستخدم git diff لمقارنة الاختلافات بين الفرعين، وقم بتحليل التضاربات التي قد تنشأ.

  3. التحدي: استخدم الخيارات المختلفة لـ git diff (مثل --stat، و--name-status، و-w، وما إلى ذلك) لتحليل التغييرات التي أُجريت على مشروع حقيقي وإنشاء تقرير كامل بالتغييرات.

Web-Tutorial.com

فريق Web-Tutorial التقني

منصة دروس برمجية يديرها عدة مطورين. كل درس يتم كتابته ومراجعته بواسطة مطورين متخصصين في المجال. نعمل على ضمان دقة وموثوقية المحتوى — إذا لاحظت أي مشكلة، فيرجى إخبارنا.

100%