Git: دليل تفصيلي لتكوين Git
يُعد تكوين Git الخطوة الأولى في استخدامه، ويمكن أن يؤدي التكوين الصحيح إلى جعل عملك أكثر كفاءة واحترافية. سيقدم هذا الدرس شرحًا مفصلاً لجميع جوانب تكوين Git.
1. نظرة عامة على إعدادات Git
يُستخدم تكوين Git (git config) لضبط طريقة عمل Git وتحديد معلومات المستخدم. تُخزَّن معلومات التكوين في ملفات التكوين، ويقوم Git بقراءة هذه التكوينات حسب ترتيب الأولوية.
(1) لماذا يعد التكوين ضروريًا؟
يحتاج Git إلى المعلومات التالية:
- هوية المستخدم: يتم تسجيل معلومات المؤلف (الاسم، عنوان البريد الإلكتروني) مع كل مشاركة.
- اختيار المحرر: محرر النصوص المستخدم عند كتابة رسائل الالتزام
- تفضيلات السلوك: أسماء الفروع الافتراضية، ومعالجة فواصل الأسطر، والإخراج الملون، وما إلى ذلك.
- أدوات الإنتاجية: الأسماء المستعارة للأوامر، والإكمال التلقائي، وما إلى ذلك.
(2) القواعد الأساسية لأوامر التهيئة
git config [<Level>] <Configuration Options> <value>
معلمات المستوى:
--system: على مستوى النظام (جميع المستخدمين)--global: المستوى العالمي (المستخدم الحالي)--local: المستوى المحلي (المستودع الحالي)- إذا لم يتم تحديد أي مستوى، فإن القيمة الافتراضية هي
--local
2. تكوين هوية المستخدم
تعد هوية المستخدم أهم إعداد في Git؛ حيث يتم تسجيل هذه المعلومات مع كل عملية «كوميت».
(1) لماذا من الضروري تكوين الهوية؟
تخيلوا عالماً بلا هويات:
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??)
دور معلومات الهوية:
- سجل عمليات الالتزام: تسجل كل عملية التزام معلومات المؤلف والمُرسِل
- التعاون بين أعضاء الفريق: يمكن لأعضاء الفريق معرفة من قام بإجراء التغييرات
- المساءلة: القدرة على تحديد الشخص المسؤول بسرعة عند ظهور مشكلة ما
- ربط الحساب: سيقوم GitHub/GitLab بربط حسابك بناءً على عنوان بريدك الإلكتروني
(2) تكوين اسم المستخدم وعنوان البريد الإلكتروني
# 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"
▶ مثال: تكوين معلومات الهوية والتحقق منها
# 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
3. مستويات التكوين والأولويات
ينقسم تكوين Git إلى ثلاثة مستويات، مرتبة حسب الأولوية من الأعلى إلى الأسفل: محلي > عام > نظامي.
(1) شرح مفصل لمستويات التكوين
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) موقع ملف التكوين
ويندوز:
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config
أنظمة Linux/Mac:
System Configuration:/etc/gitconfig
Global Configuration:~/.gitconfig
Local Configuration:Project Directory/.git/config
(3) قواعد الأولوية
عندما يظهر خيار التكوين نفسه في مستويات متعددة، يستخدم Git القيمة ذات الأولوية الأعلى:
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)
▶ مثال: التكوين على مستويات مختلفة
# 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 بتشغيل محرر نصوص كلما تطلب الأمر إدخال نص (مثل عند كتابة رسالة الالتزام).
# 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) تكوين الفاصل التلقائي للسطر
تستخدم أنظمة التشغيل المختلفة أحرفًا مختلفة لفصل الأسطر:
- ويندوز: CRLF (
\r\n) - لينكس/ماك: LF (
\n)
# 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) تكوين الأسماء المستعارة للأوامر
تتيح لك الأسماء المستعارة تعيين أسماء مختصرة للأوامر المستخدمة بشكل متكرر، مما يسهم في تحسين الكفاءة بشكل كبير.
# 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
▶ مثال: تركيبات التكوين العملية
# 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) عرض جميع الإعدادات
# 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
# 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) تعديل إعدادات التكوين
# Change Username(Overwrite directly)
git config --global user.name "New Name"
# Change Email Address
git config --global user.email "new.email@example.com"
(4) حذف عنصر تكوين
# 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
▶ مثال: عرض معلومات التكوين
# 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
❓ أسئلة شائعة
git config --local --list للتحقق من الإعداد المحلي، أو استخدم git config --show-origin user.name لعرض مصدر الإعداد.git config --global --unset 配置项名称 — على سبيل المثال، git config --global --unset user.name — لحذف الإعدادات العامة لاسم المستخدم.git config لتجنب أخطاء بناء الجملة.📖 ملخص
- تُستخدم إعدادات Git لتعيين هوية المستخدم والمحرر وتفضيلات السلوك وغيرها؛ وهي الخطوة الأولى في استخدام Git.
- يتم تنظيم الإعدادات في ثلاثة مستويات: محلي (الأولوية الأعلى) → عام → النظام (الأولوية الأدنى)
- يجب عليك تكوين اسم المستخدم وعنوان البريد الإلكتروني الخاصين بك؛ حيث سيتم تسجيل هذه المعلومات مع كل عملية إرسال.
- تشمل الإعدادات الشائعة ما يلي: المحرر، ومعالجة نهايات الأسطر، وأسماء المستعارة للأوامر، وأسماء الفروع الافتراضية، وما إلى ذلك.
- استخدم
git config --listلعرض جميع التكوينات، وgit config <key>لعرض تكوين واحد - موقع ملف التكوين: على المستوى العام في
~/.gitconfig، وعلى المستوى المحلي في.git/config
📝 تمارين
-
تمرين أساسي (مستوى الصعوبة: ⭐): قم بتكوين بيانات اعتماد Git العامة (اسم المستخدم وعنوان البريد الإلكتروني)، وتأكد من نجاح عملية التكوين.
-
تمرين متقدم (مستوى الصعوبة: ⭐⭐): قم بإعداد ثلاثة خيارات تكوين مفيدة على الأقل (مثل أسماء الفروع الافتراضية، وإخراج الألوان، وأسماء المستعارة للأوامر)، وشرح الغرض من كل خيار.
-
التحدي (الصعوبة: ⭐⭐⭐): أنشئ مستودعًا تجريبيًّا، وقم بتكوين هوية محلية في ذلك المستودع (تختلف عن الهوية العامة)، وتأكد من أن التكوين المحلي يتجاوز التكوين العام، وشرح قواعد الأولوية.