Git: دليل تفصيلي لتكوين Git

يُعد تكوين Git الخطوة الأولى في استخدامه، ويمكن أن يؤدي التكوين الصحيح إلى جعل عملك أكثر كفاءة واحترافية. سيقدم هذا الدرس شرحًا مفصلاً لجميع جوانب تكوين Git.

1. نظرة عامة على إعدادات Git

يُستخدم تكوين Git (git config) لضبط طريقة عمل Git وتحديد معلومات المستخدم. تُخزَّن معلومات التكوين في ملفات التكوين، ويقوم Git بقراءة هذه التكوينات حسب ترتيب الأولوية.

(1) لماذا يعد التكوين ضروريًا؟

يحتاج Git إلى المعلومات التالية:

(2) القواعد الأساسية لأوامر التهيئة

BASH
git config [<Level>] <Configuration Options> <value>

معلمات المستوى:



2. تكوين هوية المستخدم

تعد هوية المستخدم أهم إعداد في Git؛ حيث يتم تسجيل هذه المعلومات مع كل عملية «كوميت».

(1) لماذا من الضروري تكوين الهوية؟

تخيلوا عالماً بلا هويات:

TEXT 📖 للعرض فقط
Submit1:Added a login feature(Who is the author??)
Submit2:Fixedbug(Who fixed it??)
Submit3:Performance has been optimized(Is there a way to contact the author??)

دور معلومات الهوية:

(2) تكوين اسم المستخدم وعنوان البريد الإلكتروني

BASH
# Configure Global Username(We recommend using your real name or a nickname.)
git config --global user.name "Zhang San"

# Configure a Global Email Account(Recommended for useGitHub/GitLabAccount Email)
git config --global user.email "zhangsan@example.com"

▶ مثال: تكوين معلومات الهوية والتحقق منها

BASH
# Steps1:Configure Username
git config --global user.name "Zhang San"

# Steps2:Set Up Email
git config --global user.email "zhangsan@example.com"

# Steps3:Verify Configuration
git config user.name
# Output:Zhang San

git config user.email
# Output:zhangsan@example.com

# Steps4:View the contents of the configuration file
cat ~/.gitconfig
# Output:
# [user]
#     name = Zhang San
#     email = zhangsan@example.com
💡 نصيحة: نوصي باستخدام عنوان البريد الإلكتروني المرتبط بحسابك على GitHub/GitLab، حتى يتم ربط سجل عمليات الالتزام بحسابك ويتم عرض صورتك الشخصية واسم المستخدم الخاص بك.



3. مستويات التكوين والأولويات

ينقسم تكوين Git إلى ثلاثة مستويات، مرتبة حسب الأولوية من الأعلى إلى الأسفل: محلي > عام > نظامي.

(1) شرح مفصل لمستويات التكوين

100%
graph TB
    A[Local Configuration<br/>--local<br/>Priority:Highest] --> B[Global Configuration<br/>--global<br/>Priority:Intermediate]
    B --> C[System Configuration<br/>--system<br/>Priority:Lowest]
    
    style A fill:#e1f5ff
    style B fill:#fff4e1
    style C fill:#f0f0f0

جدول وصف مستويات التكوين:

المستوى المعلمة موقع الملف النطاق حالة الاستخدام
التكوين المحلي --local .git/config المستودع الحالي التكوين الخاص بالمشروع
الإعدادات العامة --global ~/.gitconfig جميع المستودعات الخاصة بالمستخدم الحالي التفضيلات الشخصية
تكوين النظام --system /etc/gitconfig جميع مستخدمي النظام التكوين الافتراضي للنظام

(2) موقع ملف التكوين

ويندوز:

TEXT 📖 للعرض فقط
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config

أنظمة Linux/Mac:

TEXT 📖 للعرض فقط
System Configuration:/etc/gitconfig
Global Configuration:~/.gitconfig
Local Configuration:Project Directory/.git/config

(3) قواعد الأولوية

عندما يظهر خيار التكوين نفسه في مستويات متعددة، يستخدم Git القيمة ذات الأولوية الأعلى:

TEXT 📖 للعرض فقط
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)

▶ مثال: التكوين على مستويات مختلفة

BASH
# Scene:Global Use"Zhang San",But a certain project requires the use of"Li Si"

# Steps1:Configure Global Identity(Applicable to all projects)
git config --global user.name "Zhang San"
git config --global user.email "zhangsan@example.com"

# Steps2:Go to a specific project directory
cd /path/to/special-project

# Steps3:Configure Local Identity(Current project only)
git config --local user.name "Li Si"
git config --local user.email "lisi@company.com"

# Steps4:Verify the identity used for the current project
git config user.name
# Output:Li Si(Local settings override global settings.)

# Steps5:Verify Global Identity(In other projects)
cd /path/to/other-project
git config user.name
# Output:Zhang San(Using Global Settings)
⚠️ ملاحظة: بشكل عام، ما عليك سوى تكوين هذا الإعداد على المستوى العام، ما لم يتطلب مشروع معين عنوان بريد إلكتروني خاصًا (على سبيل المثال، بريد إلكتروني تابع للشركة لمشاريع الشركة، وبريد إلكتروني شخصي للمشاريع الشخصية).



4. خيارات التكوين الشائعة

بالإضافة إلى حسابات المستخدمين، هناك العديد من الإعدادات المفيدة التي يمكن أن تساعدك على العمل بكفاءة أكبر.

(1) إعدادات المحرر

يقوم Git بتشغيل محرر نصوص كلما تطلب الأمر إدخال نص (مثل عند كتابة رسالة الالتزام).

BASH
# Set the default editor toVS Code(Recommendations)
git config --global core.editor "code --wait"

# Set the default editor toVim
git config --global core.editor "vim"

# Set the default editor toNano
git config --global core.editor "nano"

# Windows:Set the default editor toNotepad++
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

(2) تكوين الفاصل التلقائي للسطر

تستخدم أنظمة التشغيل المختلفة أحرفًا مختلفة لفصل الأسطر:

BASH
# WindowsRecommended User Configurations
# Convert upon submission toLF,Convert to upon detectionCRLF
git config --global core.autocrlf true

# Linux/MacRecommended User Configurations
# Convert upon submission toLF,Do not convert when detected
git config --global core.autocrlf input

# Disable Auto-Conversion(Not recommended)
git config --global core.autocrlf false

(3) تكوين الأسماء المستعارة للأوامر

تتيح لك الأسماء المستعارة تعيين أسماء مختصرة للأوامر المستخدمة بشكل متكرر، مما يسهم في تحسين الكفاءة بشكل كبير.

BASH
# Set Aliases for Common Commands
git config --global alias.st status        # git st = git status
git config --global alias.co checkout      # git co = git checkout
git config --global alias.br branch        # git br = git branch
git config --global alias.ci commit        # git ci = git commit
git config --global alias.unstage 'reset HEAD --'  # git unstage

# Set an alias with options
git config --global alias.last 'log -1 HEAD'  # git last = View the last commit
git config --global alias.lg "log --graph --oneline --decorate --all"  # Formatted Logs

▶ مثال: تركيبات التكوين العملية

BASH
# Steps1:Set the default branch name tomain(rather thanmaster)
git config --global init.defaultBranch main

# Enable Color Output (make git output easier to read)
git config --global color.ui auto

# Steps3:Set to use when pullingmerge(rather thanrebase)
git config --global pull.rebase false

# Steps4:Configure push settings to push only to the current branch
git config --global push.default simple

# Steps5:Turn on AutoCorrect(Automatically attempt to correct a mistyped command)
git config --global help.autocorrect 1

# Steps6:Verify all configurations
git config --global --list | grep -E "(init|color|pull|push|help)"
# Output:
# init.defaultbranch=main
# color.ui=auto
# pull.rebase=false
# push.default=simple
# help.autocorrect=1


5. عرض التكوينات وإدارتها

تعد معرفة كيفية عرض التكوينات وإدارتها مهارة أساسية لاستخدام Git.

(1) عرض جميع الإعدادات

BASH
# View All Configurations(Results of the Merger of the Three Levels)
git config --list

# View Global Settings
git config --global --list

# View Local Configuration
git config --local --list

# View System Configuration
git config --system --list

(2) View a single configuration item

BASH
# View Username
git config user.name

# Check Email
git config user.email

# View Editor
git config core.editor

# View the default branch name
git config init.defaultBranch

(3) تعديل إعدادات التكوين

BASH
# Change Username(Overwrite directly)
git config --global user.name "New Name"

# Change Email Address
git config --global user.email "new.email@example.com"

(4) حذف عنصر تكوين

BASH
# Delete Global Configuration Items
git config --global --unset user.name

# Delete Local Configuration Entries
git config --local --unset user.name

# Delete Alias
git config --global --unset alias.st

▶ مثال: عرض معلومات التكوين

BASH
# Steps1:View All Configurations
git config --list
# Output Example:
# user.name=Zhang San
# user.email=zhangsan@example.com
# core.editor=code --wait
# init.defaultbranch=main
# color.ui=auto
# alias.st=status
# alias.co=checkout
# alias.br=branch
# alias.ci=commit

# Steps2:View a Single Configuration
git config user.name
# Output:Zhang San

# Steps3:View Configuration Source(Show which file the configuration comes from)
git config --show-origin user.name
# Output:file:C:/Users/Zhang San/.gitconfig    Zhang San

# Steps4:View all configurations and their sources
git config --list --show-origin
# Output Example:
# file:C:/ProgramData/Git/config    core.symlinks=false
# file:C:/Users/Zhang San/.gitconfig    user.name=Zhang San
# file:.git/config    core.repositoryformatversion=0


❓ أسئلة شائعة

س هل يجب أن يتطابق عنوان البريد الإلكتروني الذي أقوم بتعيينه مع عنوان بريدي الإلكتروني على GitHub؟
ج هذا ليس إلزاميًا، لكننا نوصي بشدة بالاتساق. إذا استخدمت نفس اسم المستخدم، فسيربط GitHub سجل الالتزامات بحسابك، ويعرض صورة ملفك الشخصي واسم المستخدم الخاص بك، ويجعل سجل مساهماتك أكثر وضوحًا.
س لماذا يظهر اسم المستخدم الخاطئ في الطلب الذي قدمته؟
ج ربما يكون الإعداد المحلي يتجاوز الإعداد العام. استخدم git config --local --list للتحقق من الإعداد المحلي، أو استخدم git config --show-origin user.name لعرض مصدر الإعداد.
س كيف يمكنني حذف عنصر تكوين؟
ج استخدم git config --global --unset 配置项名称 — على سبيل المثال، git config --global --unset user.name — لحذف الإعدادات العامة لاسم المستخدم.
س هل يمكن تعديل ملفات التكوين يدويًّا؟
ج نعم. ملف التكوين هو ملف نص عادي، لذا يمكنك فتحه وتحريره مباشرةً في محرر نصوص. ومع ذلك، نوصي باستخدام الأمر git config لتجنب أخطاء بناء الجملة.

📖 ملخص


📝 تمارين

  1. تمرين أساسي (مستوى الصعوبة: ⭐): قم بتكوين بيانات اعتماد Git العامة (اسم المستخدم وعنوان البريد الإلكتروني)، وتأكد من نجاح عملية التكوين.

  2. تمرين متقدم (مستوى الصعوبة: ⭐⭐): قم بإعداد ثلاثة خيارات تكوين مفيدة على الأقل (مثل أسماء الفروع الافتراضية، وإخراج الألوان، وأسماء المستعارة للأوامر)، وشرح الغرض من كل خيار.

  3. التحدي (الصعوبة: ⭐⭐⭐): أنشئ مستودعًا تجريبيًّا، وقم بتكوين هوية محلية في ذلك المستودع (تختلف عن الهوية العامة)، وتأكد من أن التكوين المحلي يتجاوز التكوين العام، وشرح قواعد الأولوية.

Web-Tutorial.com

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

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

100%