Git: التحقق من حالة Git وإدارة حالة الملفات

git status is one of the most commonly used commands in Git. It helps you understand the status of your current working directory and see which files have been modified, which have been staged, and which are not being tracked.

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

(1) لماذا من الضروري التحقق من الحالة؟

في سير عمل Git، عليك أن تضع في اعتبارك دائمًا ما يلي:

يوفر git status كل هذه المعلومات ويُعد بمثابة «لوحة تحكم» لسير عمل Git.

(2) الاستخدام الأساسي

BASH
# View Full Status
git status

# View Simplified View
git status -s
# Or
git status --short

(3) قيمة المعلومات الحكومية

تساعدك معلومات الحالة على:



2. حالة الملف

(1) أربع حالات للملفات

تتخذ الملفات في Git أربع حالات رئيسية:

100%
stateDiagram-v2
    [*] --> Untracked: Create a New File
    Untracked --> Staged: git add
    Untracked --> Untracked: Edit
    Staged --> Committed: git commit
    Committed --> Modified: Edit File
    Modified --> Staged: git add
    Staged --> Modified: Edit the temporary file
    Committed --> Committed: git commit

(2) شرح مفصل للحالات

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

(3) عملية انتقال الحالة

فهم انتقالات الحالات هو مفتاح إتقان استخدام Git:

100%
graph TB
    A[Create a New File] -->|Untracked| B[Not Tracked Status]
    B -->|git add| C[Saved]
    C -->|git commit| D[Submitted status]
    D -->|Edit File| E[Status: Modified]
    E -->|git add| C
    C -->|Edit the temporary file| F[Partial Cache<br/>MMStatus]
    F -->|git add| C
    
    style B fill:#f8d7da
    style C fill:#d4edda
    style D fill:#c3e6cb
    style E fill:#fff3cd
    style F fill:#e2e3e5

▶ مثال: مراقبة تغيرات الحالة

BASH
# Initialize the repository
mkdir status-demo && cd status-demo
git init

# Create a New File
echo "# Demo Project" > README.md
echo "console.log('Hello');" > app.js

# View Status - Not tracked
git status
# On branch main
# 
# No commits yet
# 
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         README.md
#         app.js
# 
# nothing added to commit but untracked files present

# Add a file to the staging area
git add README.md

# View Status - Partial Cache
git status
# On branch main
# 
# No commits yet
# 
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#         new file:   README.md
# 
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         app.js


3. شرح تفصيلي لمخرجات الحالة

(1) بنية المخرجات

ينقسم ناتج git status إلى عدة أجزاء:

TEXT 📖 للعرض فقط
On branch main                    ← Current branch
Your branch is up to date with 'origin/main'.  ← Long-Distance Relationships

Changes to be committed:          ← Saved Changes(Green)
  (use "git restore --staged <file>..." to unstage)
        modified:   file1.txt
        new file:   file2.txt

Changes not staged for commit:    ← Unsaved changes(Red)
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   file3.txt

Untracked files:                  ← Untracked files(Red)
  (use "git add <file>..." to include in what will be committed)
        file4.txt

no changes added to commit        ← Summary Information

(2) فهم معنى كل قسم

معلومات عن الفرع:

منطقة التخزين المؤقت:

منطقة غير محمية:

الملفات غير المسجلة:

▶ مثال: تحليل مخرجات الحالة الكاملة

BASH
# Creating Complex Scenes
git init
echo "# Project" > README.md
git add README.md
git commit -m "Initial commit"

# Create files in various states
echo "new file" > new.txt           # Not tracked
echo "change" >> README.md          # Edited but not saved
echo "staged" > staged.txt
git add staged.txt                  # New files that have been cached
echo "more" >> staged.txt           # Saved and then modified

# View Full Status
git status
# On branch main
# Changes to be committed:
#   new file:   staged.txt
# 
# Changes not staged for commit:
#   modified:   README.md
#   modified:   staged.txt
# 
# Untracked files:
#   new.txt


4. نظرة عامة على الحالة

(1) تنسيق الإخراج الموجز

استخدم الخيارين -s أو --short للحصول على ناتج موجز:

BASH
git status -s

يتكون تنسيق الإخراج من عمودين: العلامات وأسماء الملفات:

TEXT 📖 للعرض فقط
XY filename

(2) معنى مؤشرات الحالة

التسمية العمود X (المنطقة المؤقتة) العمود Y (منطقة العمل) الوصف
?? - - الملفات غير المتعقبة
A تمت الإضافة - أُضيفت حديثًا إلى بيئة الاختبار
M تم التعديل - تم التعديل وحفظه كمسودة
M - تم التعديل تم التعديل ولكن لم يتم الحفظ
MM تم التعديل تم التعديل تم النشر ثم التعديل
D تم الحذف - تم الحذف وحفظه في المسودات
D - محذوف محذوف ولم يتم حفظه كمسودة
AD تمت الإضافة تم الحذف تم الحذف من مساحة العمل بعد الإضافة
R تم تغيير الاسم - تم تغيير الاسم وحفظه كمسودة
C تم نسخه - تم نسخه وحفظه مؤقتًا

(3) مزايا النص الموجز

▶ مثال: مقارنة بين النتائج التفصيلية والموجزة

BASH
# Create Multiple States
echo "untracked" > file1.txt
echo "content" > file2.txt
git add file2.txt
echo "more" >> file2.txt
echo "tracked" > file3.txt
git add file3.txt
git commit -m "Add files"
echo "modified" >> file3.txt

# Detailed Output
git status
# On branch main
# Changes to be committed:
#   new file:   file2.txt
# 
# Changes not staged for commit:
#   modified:   file3.txt
# 
# Untracked files:
#   file1.txt

# Concise Output
git status -s
# A  file2.txt
#  M file3.txt
# ?? file1.txt


5. تتبع تغييرات الحالة

(1) التغيرات النموذجية في حالة سير العمل

دعونا نستعرض سير عمل Git الكامل:

100%
sequenceDiagram
    participant W as Workspace
    participant S as Buffer
    participant R as Repository
    
    Note over W: 1. Create a New File
    W->>W: Status: Untracked
    
    Note over W,S: 2. git add
    W->>S: Move to the staging area
    S->>S: Status: Staged
    
    Note over S,R: 3. git commit
    S->>R: Commit to the repository
    R->>R: Status: Committed
    
    Note over W: 4. Edit File
    W->>W: Status: Modified
    
    Note over W,S: 5. git add
    W->>S: Update the temporary storage area
    S->>S: Status: Staged
    
    Note over S,R: 6. git commit
    S->>R: Submit Changes
    R->>R: Status: Committed

(2) Special Status: Partially Cached

عندما يكون الملف قد تم وضعه في قائمة الانتظار، ولكن تم إجراء تغييرات جديدة في مساحة العمل، تظهر حالة خاصة:

BASH
# Create and stage a file
echo "version 1" > file.txt
git add file.txt
git status -s
# A  file.txt

# Continue editing the file
echo "version 2" >> file.txt
git status -s
# AM file.txt
# AIndicates that it has been saved temporarily(version 1)
# MIndicates that there are new changes in the workspace(version 2)

# Add Again
git add file.txt
git status -s
# A  file.txt
# The temporary storage area is currentlyversion 2

(3) العلاقة بين أوامر State وأوامر Git

الحالة الحالية الأوامر المتاحة النتيجة
غير مُتتبع git add أصبح في مرحلة الإعداد
غير متتبع git clean حذف الملف
Modified git add Becomes Staged
Modified git restore Reverted the change; status changed to Committed
تم تنظيمه git commit أصبح ملتزمًا
مُدرج git restore --staged إلغاء الإدراج، أو التغيير إلى «مُعدَّل» أو «غير مُتتبَّع»
تم الالتزام تعديل الملف تم وضع علامة "تم التعديل"

▶ مثال: تتبع سير العمل بالكامل

BASH
# Initialization
git init status-workflow
cd status-workflow

# Steps1:Create a File(Untracked)
echo "# Project" > README.md
git status -s
# ?? README.md

# Steps2:Add to Stash(Staged)
git add README.md
git status -s
# A  README.md

# Steps3:Submit(Committed)
git commit -m "Add README"
git status
# nothing to commit, working tree clean

# Steps4:Edit File(Modified)
echo "## Features" >> README.md
git status -s
#  M README.md

# Steps5:Save Changes Temporarily(Staged)
git add README.md
git status -s
# M  README.md

# Steps6:Revise Again(Partial Cache)
echo "## Usage" >> README.md
git status -s
# MM README.md

# Steps7:Commit Staged Changes
git commit -m "Add features"
git status -s
#  M README.md  (There are still changes to be made in the workspace.)

# Steps8:Submit the remaining changes
git add README.md
git commit -m "Add usage"
git status
# nothing to commit, working tree clean


❓ أسئلة شائعة

س لماذا يعرض git status بعض الملفات باللون الأحمر والبعض الآخر باللون الأخضر؟
ج The color indicates the file's status. Green means the file is staged and will be included in the next commit. Red means the file is not staged (either modified or untracked) and must be added with git add before it can be committed.
س What does "working tree clean" mean?
ج يشير إلى أن مساحة العمل خالية من أي تغييرات لم يتم تثبيتها. فقد تم تثبيت جميع التغييرات في المستودع، ولا توجد ملفات غير متتبعة أو غير معدة للتثبيت أو معدة للتثبيت ولكن لم يتم تثبيتها. وهذه هي حالة العمل المثالية.
س كيف يمكنني عرض الملفات الموضوعة في المرحلة الانتقالية فقط؟
ج استخدم git diff --staged أو git diff --cached لعرض التغييرات التي قمت بوضعها في قائمة التغييرات المؤقتة. إذا كنت ترغب في عرض قائمة الملفات، فاستخدم git status -s لعرض الملفات المحددة على اليسار فقط.
س ماذا يمكنني أن أفعل إذا كان git status يعمل ببطء؟
ج في المستودعات الكبيرة، قد يعمل git status ببطء بسبب العدد الكبير من الملفات. يمكنك استخدام git status -s للحصول على مخرجات موجزة، أو تهيئة git config core.ignoreStat true لتخطي فحوصات حالة الملفات. كما يمكنك استخدام git status --untracked-files=no لتجاهل الملفات غير المتعقبة.
س كيف يمكنني استبعاد ملفات معينة من العرض بواسطة git status؟
ج قم بإنشاء ملف باسم .gitignore في الدليل الجذري للمشروع، وقم بإدراج أنماط الملفات المراد تجاهلها فيه. على سبيل المثال، يتجاهل الملف *.log جميع ملفات السجلات، بينما يتجاهل الملف node_modules/ دلائل التبعيات. ولن تظهر الملفات التي تم تجاهلها في الملف git status.

📖 ملخص


📝 تمارين

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

  2. تمرين متقدم: قم بإنشاء ملفات في حالات مختلفة (غير مُتتبَّعة، مُعدَّلة، مُعدَّة للنقل، مُعدَّة جزئيًا للنقل)، وقارن بين النتائج التفصيلية لـ git status والنتائج الموجزة لـ git status -s، وافهم معنى علامات الحالة.

  3. التحدي: اكتب برنامجًا نصيًّا لتحليل مخرجات git status -s وحساب عدد الملفات في كل حالة (غير مُدرجة في المتابعة، مُعدة للنشر، مُعدَّلة، إلخ) بهدف التحقق تلقائيًّا من حالة المستودع.

Web-Tutorial.com

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

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

100%