Git: البدء في استنساخ مستودعات Git والتعاون عن بُعد
الاستنساخ هو عملية استرداد نسخة كاملة من مستودع Git من خادم بعيد، بما في ذلك جميع الملفات والفروع وسجل الالتزامات الكامل. وهذه هي الخطوة الأولى في التعاون مع الفريق.
1. نظرة عامة على الاستنساخ
(1) ما هو الاستنساخ؟
الاستنساخ هو عملية نسخ مستودع بعيد بالكامل إلى جهازك المحلي. وعلى عكس تنزيل أرشيف ZIP، فإن عملية الاستنساخ تسترد مستودع Git بالكامل، بما في ذلك:
- جميع الملفات: لقطة كاملة لملفات المشروع في لحظة زمنية محددة
- السجل الكامل: جميع سجلات عمليات الالتزام، ومعلومات المؤلف، والطوابع الزمنية
- جميع الفروع: معلومات عن جميع الفروع الموجودة في المستودع البعيد
- الربط عن بُعد: ينشئ اتصالاً تلقائيًّا بمستودع بعيد
(2) الفرق بين الاستنساخ والتنزيل
يسأل الكثيرون: لماذا لا نكتفي بتنزيل ملف ZIP فحسب؟ دعونا نقارن بين الخيارين:
| الإجراء | المحتويات | هل يمكن عرض السجل؟ | هل يمكن نشر التغييرات؟ | هل يمكن التبديل بين الإصدارات؟ |
|---|---|---|---|---|
| git clone | المستودع الكامل | ✅ | ✅ | ✅ |
| تنزيل ملف ZIP | لقطة للملف فقط | ❌ | ❌ | ❌ |
(3) حالات استخدام الاستنساخ
Cloning is primarily used in the following scenarios:
- المشاركة في مشاريع البرمجيات مفتوحة المصدر: قم بنسخ مشروع محليًّا للتعلم والمساهمة
- التعاون بين أعضاء الفريق: الوصول إلى مستودع الكود المشترك للفريق
- نشر المشروع: انسخ الشفرة إلى الخادم من أجل نشرها
- النسخ الاحتياطي للكود: انسخ المستودع البعيد إلى جهازك المحلي كنسخة احتياطية
2. الأمر git clone
(1) قواعد النحو الأساسية
الصيغة الأساسية للأمر git clone:
# Clone to the current directory(Automatically create a folder with the same name)
git clone <Warehouse Address>
# Clone to the specified directory
git clone <Warehouse Address> <Directory Name>
(2) عملية الاستنساخ
فيما يلي عملية الاستنساخ الكاملة:
sequenceDiagram
participant L as Local Warehouse
participant R as Remote Server
L->>R: 1. Initiate a Cloning Request
R->>L: 2. Send Repository Metadata
R->>L: 3. Send All Objects (Documents, Commits)
R->>L: 4. Send Branch Information
L->>L: 5. Create origin/Remote Association
L->>L: 6. Check out the default branch
L->>L: 7. Cloning Complete
▶ مثال: استنساخ مستودع GitHub
# Clone React Project (HTTPS Method)
git clone https://github.com/facebook/react.git
# Output Information:
# Cloning into 'react'...
# remote: Enumerating objects: 156789, done.
# remote: Counting objects: 100% (156789/156789), done.
# remote: Compressing objects: 100% (456/456), done.
# remote: Total 156789 (delta 12345), reused 156333 (delta 12000), pack-reused 0
# Receiving objects: 100% (156789/156789), 45.67 MiB | 5.23 MiB/s, done.
# Resolving deltas: 100% (123456/123456), done.
# Clone to the specified directory
git clone https://github.com/facebook/react.git my-react-project
# Clone to the current directory(Pay attention to the last point)
git clone https://github.com/facebook/react.git .
▶ مثال: عرض المعلومات بعد الاستنساخ
# Clone the repository
git clone https://github.com/user/demo.git
cd demo
# View Remote Repository Associations
git remote -v
# origin https://github.com/user/demo.git (fetch)
# origin https://github.com/user/demo.git (push)
# View All Branches(Including remote branches)
git branch -a
# * main
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/develop
# remotes/origin/feature
# View Commit History
git log --oneline -5
# a1b2c3d (HEAD -> main, origin/main, origin/HEAD) Update README
# e4f5g6h Add new feature
# i7j8k9l Fix bug in login
# m0n1o2p Initial commit
3. خيارات النسخ المتماثل
(1) الاستنساخ السطحي
بالنسبة للمشاريع الكبيرة، قد يستغرق النسخ الكامل وقتًا طويلاً ويتطلب مساحة كبيرة. أما النسخ السطحي فيقتصر على تنزيل أحدث عمليات التسجيل فقط:
# Clone only the most recent commit
git clone --depth 1 <Warehouse Address>
# Clone RecentNNext Submission
git clone --depth 10 <Warehouse Address>
خصائص الاستنساخ السطحي:
- المزايا: سرعات تنزيل عالية، لا تشغل سوى مساحة صغيرة
- العيوب: عدم القدرة على عرض السجل الكامل؛ هناك قيود على بعض العمليات
- حالات الاستخدام: عمليات البناء في إطار CI/CD، عندما لا يهمك سوى أحدث إصدار من الكود
(2) الاستنساخ أحادي الفرع
إذا كنت بحاجة إلى فرع معين فقط، فيمكنك استنساخ هذا الفرع وحده:
# Clone only the specified branch
git clone --branch main --single-branch <Warehouse Address>
# Abbreviated form
git clone -b main --single-branch <Warehouse Address>
(3) الاستنساخ التكراري
إذا كان المشروع يحتوي على وحدات فرعية، فيجب عليك استنساخها بشكل متكرر:
# Recursive Cloning,Clone all submodules at the same time
git clone --recursive <Warehouse Address>
# If you have already cloned it,Submodules can be initialized individually
git clone <Warehouse Address>
cd <Table of Contents>
git submodule update --init --recursive
(4) مقارنة بين خيارات الاستنساخ
| الخيار | الوصف | التنزيلات | حالات الاستخدام |
|---|---|---|---|
| لا توجد خيارات | نسخة مطابقة تمامًا | الكل | التطوير اليومي |
--depth 1 |
نسخة سطحية | أحدث تعديل فقط | CI/CD، النشر |
--single-branch |
فرع واحد | تحديد فرع واحد فقط | فرع معين فقط |
--recursive |
الاستنساخ التكراري | يتضمن الوحدات الفرعية | المشروع يحتوي على وحدات فرعية |
--bare |
خادم عاري | بدون مساحة عمل | صورة الخادم |
▶ مثال: النسخ السطحي لمشروع كبير
# LinuxFull Kernel Cloning(3GB+,It takes a long time)
git clone https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (8234567/8234567), 3.45 GiB | 1.23 MiB/s, done.
# Shallow CloningLinuxKernel(100MB+,Fast)
git clone --depth 1 https://github.com/torvalds/linux.git
# Cloning into 'linux'...
# Receiving objects: 100% (12345/12345), 156.78 MiB | 5.67 MiB/s, done.
# View Historical Differences
cd linux
git log --oneline
# Only1Next Submission(Shallow Cloning)
# Fully cloned repository
git log --oneline | wc -l
# 1M+ Next Submission
4. العنوان البعيد
(1) نوع البروتوكول
يدعم Git بروتوكولات متعددة للوصول إلى المستودعات البعيدة:
graph TB
A[Remote Repository Address] --> B[HTTPS]
A --> C[SSH]
A --> D[Git Protocol]
A --> E[Local Path]
B --> B1[Example: https://...]
C --> C1[Example: git@...]
D --> D1[Example: git://...]
E --> E1[Example: /path/to/...]
style B fill:#d4edda
style C fill:#c3e6cb
style D fill:#fff3cd
style E fill:#f8d7da
(2) HTTPS مقابل SSH
البروتوكولان الأكثر استخدامًا هما HTTPS وSSH:
| ميزة | HTTPS | SSH |
|---|---|---|
| تنسيق العنوان | https://github.com/user/repo.git |
git@github.com:user/repo.git |
| صعوبة التهيئة | سهلة، لا تتطلب أي تهيئة | تتطلب إنشاء مفاتيح SSH وتهيئتها |
| طريقة المصادقة | اسم المستخدم + كلمة المرور/الرمز | مفتاح SSH |
| إجراء النقر | يلزم إدخال بيانات الاعتماد في كل مرة | لا يلزم إدخال كلمة مرور |
| جدار الحماية | متوافق، يستخدم المنفذ 443 | قد يتم حظره، يستخدم المنفذ 22 |
| السيناريوهات الموصى بها | المبتدئون، الاستخدام العرضي | التطوير اليومي، الأتمتة |
(3) استخدام رمز الوصول الشخصي
اعتبارًا من عام 2021، لم يعد GitHub يدعم المصادقة باستخدام كلمة المرور؛ لذا يجب عليك استخدام رمز الوصول الشخصي:
# Use when cloningToken
git clone https://<token>@github.com/user/repo.git
# Or enter it when sending the push notificationTokenAs a password
git clone https://github.com/user/repo.git
# Username: your-username
# Password: ghp_xxxxxxxxxxxx(UsageToken)
▶ مثال: الاستنساخ باستخدام بروتوكولات مختلفة
# HTTPSMethod(Recommended for Beginners)
git clone https://github.com/facebook/react.git
# SSHMethod(Recommended for Experienced Developers)
git clone git@github.com:facebook/react.git
# GitAgreement(Read-only,Used less frequently)
git clone git://github.com/facebook/react.git
# Local Path(Clone the local repository)
git clone /path/to/local/repo.git
# Clone from another user's directory on the same server
git clone file:///home/otheruser/project.git
5. التهيئة بعد النسخ
(1) الروابط التي تم إنشاؤها تلقائيًا
بمجرد اكتمال عملية النسخ، سيقوم Git تلقائيًا بإنشاء التكوين التالي:
graph TB
A[Cloned repository] --> B[.git Directory]
A --> C[Workspace Files]
A --> D[Remote origin Association]
B --> B1[All Commit History]
B --> B2[All Branch Information]
B --> B3[Profile]
D --> D1[fetch Address]
D --> D2[push Address]
D --> D3[Default Branch Tracking]
style A fill:#e1f5ff
style D fill:#d4edda
(2) View Remote Information
# View the name of the remote repository
git remote
# origin
# View Details
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# View details about a remote repository
git remote show origin
# * remote origin
# Fetch URL: https://github.com/user/repo.git
# Push URL: https://github.com/user/repo.git
# HEAD branch: main
# Remote branches:
# main tracked
# develop tracked
# Local branch configured for 'git pull':
# main merges with remote main
# Local ref configured for 'git push':
# main pushes to main (up to date)
(3) فرع مستنسخ
تقوم عملية «الاستنساخ» باسترداد جميع الفروع البعيدة، لكنها تقوم بسحب الفرع الافتراضي فقط:
# View local branches
git branch
# * main (Only the default branch)
# View All Branches(Including remote)
git branch -a
# * main
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/develop
# remotes/origin/feature-login
# Create a local branch to track a remote branch
git checkout -b develop origin/develop
# Or use the abbreviation
git checkout develop
# Branch 'develop' set up to track remote branch 'develop' from 'origin'.
# Switched to a new branch 'develop'
▶ مثال: سير العمل الكامل بعد الاستنساخ
# 1. Clone the repository
git clone https://github.com/user/project.git
cd project
# 2. View Status
git status
# On branch main
# Your branch is up to date with 'origin/main'.
# 3. View Remote Information
git remote -v
# 4. View All Branches
git branch -a
# 5. Switch to another branch
git checkout develop
# 6. View Commit History
git log --oneline --graph --all -10
# 7. Get the latest updates
git pull origin main
❓ أسئلة شائعة
--depth 1 لتنزيل أحدث عمليات الالتزام فقط، مما سيجعل العملية أسرع بكثير. إذا كنت تحتاج إلى فرع معين فقط، فاستخدم الخيار --single-branch. بالإضافة إلى ذلك، يمكنك استخدام موقع مرآة محلي لتسريع العملية، مثل مرآة fastgit التابعة لـ GitHub.git push origin <分支名>.git clone <地址> <新目录名>، أو الدخول إلى المجلد الموجود وتشغيل git pull لتحديثه.📖 ملخص
- الاستنساخ هو عملية استرداد نسخة كاملة من مستودع من مصدر بعيد، بما في ذلك جميع الملفات والسجل ومعلومات الفروع.
git clone <url>هو أمر أساسي يتيح لك تحديد اسم الدليل المستهدف- يُعد «Shallow Clone»
--depth 1مناسبًا للحالات التي لا يُحتاج فيها سوى إلى أحدث نسخة من الكود؛ فهو سريع ولا يشغل سوى مساحة صغيرة. - الاستنساخ أحادي الفرع
--single-branchيقوم بتنزيل الفرع المحدد فقط، مما يقلل من حجم التنزيل - بروتوكول HTTPS بسيط وسهل الاستخدام، في حين يوفر بروتوكول SSH وسيلة مريحة لنقل الملفات؛ ولكل منهما حالات الاستخدام المناسبة له.
- بعد عملية النسخ، يتم إنشاء اتصال عن بُعد بـ
originتلقائيًا، مما يتيح لك الاطلاع على المعلومات والفروع عن بُعد. - لا يكتشف النسخ المتماثل سوى الفرع الافتراضي؛ أما الفروع الأخرى فيجب إنشاؤها وتتبعها يدويًّا.
📝 تمارين
-
تمرين أساسي: انسخ مستودع «Hello-World» على GitHub، واطلع على معلومات المستودع البعيد وجميع الفروع، وافهم بنية الدلائل بعد النسخ.
-
تمرين متقدم: قارن بين النسخ الكامل والنسخ السطحي
--depth 1لمشروع كبير (مثل React)، وقارن بين وقت التنزيل وحجم مجلد .git، وتعرف على حالات استخدام النسخ السطحي. -
التحدي: ابحث عن كيفية استنساخ مستودع محليًّا ثم إرساله إلى مستودع بعيد آخر (على سبيل المثال، الاستنساخ من GitHub والإرسال إلى GitLab)، واكتسب فهمًا لإدارة المستودعات البعيدة وتكوينات المستودعات البعيدة المتعددة.