Git: دليل تفصيلي لإضافة الملفات إلى منطقة التجهيز في Git

تُعد منطقة التجهيز مفهومًا فريدًا في Git؛ فهي تقع بين دليل العمل والمستودع، مما يتيح لك التحكم بدقة في محتويات كل عملية إرسال. ويقوم الأمر git add بإضافة التغييرات إلى منطقة التجهيز.

1. نظرة عامة على git add

(1) ما هي منطقة التخزين المؤقت؟

تعد منطقة التجهيز مكونًا أساسيًا في نموذج المناطق الثلاث في Git:

100%
graph TB
    subgraph GitThe Three-Zone Model
        W[Workspace<br/>Working Directory<br/>Files Actually Edited]
        S[Buffer<br/>Staging Area<br/>Changes Ready to Be Submitted]
        R[Repository<br/>Repository<br/>All Commit History]
    end
    
    W -->|git add| S
    S -->|git commit| R
    R -->|git checkout| W
    
    style W fill:#fff3cd
    style S fill:#d4edda
    style R fill:#c3e6cb

(2) لماذا نحتاج إلى مخزن مؤقت؟

توفر منطقة التجهيز تحكماً دقيقاً في عمليات التسجيل:

الميزة الوصف مثال
عمليات التسجيل الانتقائية ليس من الضروري تسجيل جميع التغييرات قم بتسجيل الملفات التي تُصلح الأخطاء فقط؛ ولا تقم بتسجيل الكود التجريبي
الإرسال على دفعات إرسال التغييرات المختلفة بشكل منفصل إرسال الميزة أ والميزة ب بشكل منفصل
مراجعة التغييرات فحوصات ما قبل الالتزام التحقق من صحة التغييرات التي تم وضعها في مرحلة الانتظار
التراجع عن التغييرات لا يؤثر التراجع عن تغيير تم وضعه في قائمة التغييرات المحددة على دليل العمل يمكنك التراجع عن التغييرات التي أُجريت على ملف تم وضعه في قائمة التغييرات المحددة

(3) الغرض من git add

يقوم الأمر git add بنسخ التغييرات من دليل العمل إلى منطقة التجهيز:



2. إضافة عملية

(1) قواعد النحو الأساسية

BASH
# Add a Single File
git add <File Name>

# Add Multiple Files
git add <Documents1> <Documents2> <Documents3>

# Add all files in the directory
git add <Directory Name>

# Add all files in the current directory
git add .

(2) Detailed Explanation of the Add Process

100%
sequenceDiagram
    participant W as Workspace
    participant S as Buffer
    participant R as Repository
    
    Note over W: Document Status: Modified/Untracked
    
    W->>S: git add <file>
    Note over S: Copy the file to the temporary storage area<br/>CalculationSHA-1Hash<br/>Update Index
    
    Note over S: Document Status: Staged
    
    S->>R: git commit
    Note over R: Document Status: Committed

(3) Changes After Addition

After adding a file to the staging area:

▶ مثال: إضافة ملف واحد

BASH
# Create a New File
echo "# My Project" > README.md

# View Status(Not tracked)
git status
# Untracked files:
#   README.md

# Add to Stash
git add README.md

# View Status(Saved)
git status
# Changes to be committed:
#   new file:   README.md

# View the contents of the staging area
git diff --staged
# diff --git a/README.md b/README.md
# new file mode 100644
# index 0000000..e69de29
# --- /dev/null
# +++ b/README.md
# @@ -0,0 +1 @@
# +# My Project

▶ مثال: إضافة ملفات متعددة

BASH
# Create Multiple Files
echo "console.log('app');" > app.js
echo "console.log('test');" > test.js
echo "node_modules/" > .gitignore

# Add Multiple Files
git add app.js test.js .gitignore

# View Status
git status -s
# A  .gitignore
# A  app.js
# A  test.js

# Or add them one by one
git add app.js
git add test.js
git add .gitignore

# The results are the same


3. إضافة خيارات

(1) طريقة الإضافة الجماعية

يوفر Git عدة طرق لإضافة الملفات دفعة واحدة:

BASH
# Add all files in the current directory(Excludes deletions)
git add .

# Add all modifications and deletions to tracked files
git add -u

# Add All Files (Including new files, Edit, Delete)
git add -A
# Or
git add --all

# Interactive Addition(Verify one by one)
git add -p

(2) مقارنة الخيارات

الأمر الملفات الجديدة الملفات المعدلة الملفات المحذوفة النطاق
git add <文件> تحديد الملف
git add . الدليل الحالي
git add -u المستودع بأكمله
git add -A المستودع بأكمله

(3) الجمع التفاعلي

يتيح لك git add -p (وضع التصحيح) مراجعة كل كتلة تعديل على حدة:

BASH
git add -p
# Show Modified Blocks,Ask if you want to save a draft
# Stage this hunk [y,n,q,a,d,e,?]?
# y - yes,Temporarily store this block
# n - no,Do not cache
# q - quit,Exit
# a - all,Cache all blocks in this file
# d - discard,Do not cache any blocks from this file
# e - edit,Edit this block manually
# ? - help,Show Help

▶ مثال: إضافة مقارنات بشكل جماعي

BASH
# Create a Scene
echo "new file" > new.txt          # New Document
echo "content" > tracked.txt
git add tracked.txt
git commit -m "Add tracked"
echo "modified" >> tracked.txt     # Modified
git rm tracked.txt                 # Delete(Simulation)

# Usage git add .
git add .
git status -s
# A  new.txt
# (Do not process deletion)

# Reset
git reset

# Usage git add -u
git add -u
git status -s
# D  tracked.txt
# (Process only modifications and deletions to tracked files)

# Reset
git reset

# Usage git add -A
git add -A
git status -s
# A  new.txt
# D  tracked.txt
# (Process all)


4. إدارة المخزن المؤقت

(1) عرض محتويات منطقة التجهيز

BASH
# View Staged Changes
git diff --staged
# Or
git diff --cached

# View the differences between the staging area and the repository
git diff --staged HEAD

# View the list of files in the staging area
git status

(2) خصائص المنطقة العازلة

الميزات الرئيسية لجهاز «بافر»:

100%
graph TB
    A[Workspace Files] -->|git add| B[Swap Area Snapshot]
    B -->|git commit| C[Repository Commit]
    
    A -->|Continue editing| D[New Version of the Workspace]
    D -->|git add| E[Update the temporary storage area]
    
    B -.->|Different Versions| D
    
    style B fill:#d4edda
    style D fill:#fff3cd

النقاط الرئيسية:

(3) الكتابة فوق منطقة التخزين المؤقتة

سيؤدي تكرار git add إلى استبدال محتويات الحافظة:

BASH
# First time adding
echo "version 1" > file.txt
git add file.txt
# Buffer:version 1

# Edit File
echo "version 2" > file.txt
# Workspace:version 2
# Buffer:version 1(Unchanged)

# Add Again
git add file.txt
# Buffer:version 2(Updated)

▶ مثال: فصل منطقة التجهيز عن منطقة العمل

BASH
# Create and Add Files
echo "Line 1" > file.txt
git add file.txt

# View the contents of the staging area
git diff --staged
# +Line 1

# Continue editing the file
echo "Line 2" >> file.txt

# View the differences between the workspace and the staging area
git diff
# +Line 2

# View Status
git status -s
# AM file.txt
# A:The temporary storage area contains data(Line 1)
# M:There are new changes in the workspace(Line 2)

# Update the temporary storage area
git add file.txt

# The staging area currently contains all changes.
git diff --staged
# +Line 1
# +Line 2


5. التراجع عن عملية الجمع

(1) التراجع عن التخزين المؤقت

إذا قمت بتجهيز الملف الخطأ، فيمكنك إلغاء تجهيزه:

BASH
# Undo Staging for a Single File(New Grammar)
git restore --staged <File Name>

# Undo Staging for a Single File(Old Syntax)
git reset HEAD <File Name>

# Uncheck all checkboxes
git restore --staged .
# Or
git reset

(2) الوضع بعد الإلغاء

بعد إلغاء التجهيز:

(3) الإلغاء مقابل التخلص

احرص على التمييز بين هاتين العمليتين:

100%
graph TB
    A[Saved Files] -->|git restore --staged| B[Undo Temporary Save<br/>Workspace Retained]
    A -->|git restore| C[Discard Changes<br/>Revert to the version in the staging area]
    
    B --> D[Status: Untracked/Modified]
    C --> E[Status: Staged]
    
    style B fill:#d4edda
    style C fill:#f8d7da

▶ مثال: التراجع عن عملية التجهيز

BASH
# Create and Add Files
echo "test content" > test.txt
git add test.txt
git status -s
# A  test.txt

# Undo Temporary Save
git restore --staged test.txt
git status -s
# ?? test.txt
# The file has reverted to an untracked state

# Undo for Tracked Files
echo "# Project" > README.md
git add README.md
git commit -m "Add README"

echo "new content" >> README.md
git add README.md
git status -s
# M  README.md

# Undo Temporary Save
git restore --staged README.md
git status -s
#  M README.md
# The file reverts to its modified state,Changes Retained

▶ مثال: إضافة مشهد بشكل تفاعلي

BASH
# Create a file containing multiple changes
cat > app.js << 'EOF'
function oldFunction() {
  console.log('old');
}

function newFeature() {
  console.log('new feature');
}

function debugCode() {
  console.log('debug');  // Temporary Debugging Code
}
EOF

git add app.js
git commit -m "Add app"

# Edit File:Includes feature improvements and debugging code
cat > app.js << 'EOF'
function oldFunction() {
  console.log('old');
  console.log('improved');  // Feature Improvements
}

function newFeature() {
  console.log('new feature');
  console.log('enhanced');  // Feature Enhancements
}

function debugCode() {
  console.log('debug');  // Temporary Debugging Code
}
EOF

# Use Interactive Add,Improvements to the Temporary Storage Feature
git add -p
# Confirm each modification block one by one
# y - Improvements to the Temporary Storage Feature
# n - Do not cache debug code

# Submit Feature Improvements
git commit -m "Improve features"

# The debugging code is still in the workspace,Not submitted


❓ أسئلة شائعة

س ما الفرق بين git add . وgit add -A؟
ج يقوم git add . بمعالجة الملفات الموجودة في الدليل الحالي ودلائله الفرعية فقط؛ ولا يشمل عمليات الحذف من الدلائل الأخرى. أما git add -A فيعالج جميع الملفات الموجودة في المستودع بأكمله، بما في ذلك الإضافات والتعديلات وعمليات الحذف في جميع الدلائل. ويكون للأمرين نفس التأثير في الدليل الجذر للمستودع، ولكن هناك اختلاف عند استخدامهما في الدلائل الفرعية.
س ماذا يحدث إذا واصلت تحرير الملف بعد وضعه في مرحلة الانتظار؟
ج سيظهر الملف في كل من منطقة التجهيز ودليل العمل، لكن محتويات كل منهما ستختلف. تحتوي منطقة التجهيز على النسخة كما كانت عند إضافتها، بينما يحتوي دليل العمل على النسخة الجديدة بعد التعديل. سيُعرض الحالة على أنها AM أو MM. يتعين عليك استخدام git add لتحديث منطقة التجهيز مرة أخرى، أو إجراء عملية «commit» لمحتويات منطقة التجهيز قبل إضافة تغييرات جديدة.
س كيف يمكنني عرض محتويات الحافظة؟
ج استخدم git diff --staged أو git diff --cached لعرض التغييرات في منطقة التجهيز. سيُظهر ذلك الاختلافات بين منطقة التجهيز وآخر عملية إرسال. إذا كنت ترغب في عرض محتويات ملفات معينة، فاستخدم git show :filename لعرض الملفات الموجودة في منطقة التجهيز.
س هل يقوم git add بالكتابة فوق المحتوى الذي تم إعداده مسبقًا؟
ج نعم. سيؤدي تشغيل git add مرة أخرى على نفس الملف إلى تحديث محتويات منطقة التجهيز إلى أحدث إصدار. هذه هي الطريقة المعتادة لتحديث منطقة التجهيز؛ وهي ليست مشكلة بل ميزة.
س كيف يمكنني تطبيق بعض التغييرات فقط على ملف ما؟
ج استخدم git add -p للدخول إلى الوضع التفاعلي. سيعرض Git التغييرات جزءًا تلو الآخر، ويمكنك اختيار الحرف "y" لإضافة التغييرات إلى قائمة التغييرات المحددة، أو الحرف "n" لتخطيها، أو الحرف "e" لتحريرها يدويًّا. يتيح لك ذلك التحكم بدقة في التغييرات التي يتم إضافتها إلى قائمة التغييرات المحددة، مما يتيح لك إجراء عمليات التثبيت للتغييرات المختلفة في ملف واحد بشكل منفصل.

📖 ملخص


📝 تمارين

  1. تمرين أساسي: أنشئ مستودع Git، وأضف عدة ملفات إلى منطقة التجهيز، واستخدم git diff --staged لعرض المحتوى المُجهَّز، ثم قم بالتثبيت. راقب التغيرات في الحالة في كل خطوة.

  2. تمرين متقدم: أنشئ ملفًا، وأضفه إلى منطقة التجهيز، واستمر في تعديله. لاحظ الفروق بين دليل العمل ومنطقة التجهيز. جرب استخدام الأمر التفاعلي git add -p لإضافة جزء فقط من التغييرات إلى منطقة التجهيز.

  3. التحدي: قم بمحاكاة سيناريو واقعي: لقد أصلحت خطأين، وأضفت ميزة جديدة، وكتبت بعض التعليمات البرمجية المؤقتة لتصحيح الأخطاء. استخدم عمليات التسجيل التفاعلية وعمليات التسجيل المتعددة لفصل التغييرات المختلفة عن بعضها والحفاظ على سجل تسجيل واضح.

Web-Tutorial.com

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

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

100%