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:

BASH
# 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) عملية الاستنساخ

فيما يلي عملية الاستنساخ الكاملة:

100%
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

BASH
# 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 .

▶ مثال: عرض المعلومات بعد الاستنساخ

BASH
# 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) الاستنساخ السطحي

بالنسبة للمشاريع الكبيرة، قد يستغرق النسخ الكامل وقتًا طويلاً ويتطلب مساحة كبيرة. أما النسخ السطحي فيقتصر على تنزيل أحدث عمليات التسجيل فقط:

BASH
# Clone only the most recent commit
git clone --depth 1 <Warehouse Address>

# Clone RecentNNext Submission
git clone --depth 10 <Warehouse Address>

خصائص الاستنساخ السطحي:

(2) الاستنساخ أحادي الفرع

إذا كنت بحاجة إلى فرع معين فقط، فيمكنك استنساخ هذا الفرع وحده:

BASH
# Clone only the specified branch
git clone --branch main --single-branch <Warehouse Address>

# Abbreviated form
git clone -b main --single-branch <Warehouse Address>

(3) الاستنساخ التكراري

إذا كان المشروع يحتوي على وحدات فرعية، فيجب عليك استنساخها بشكل متكرر:

BASH
# 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 خادم عاري بدون مساحة عمل صورة الخادم

▶ مثال: النسخ السطحي لمشروع كبير

BASH
# 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 بروتوكولات متعددة للوصول إلى المستودعات البعيدة:

100%
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 يدعم المصادقة باستخدام كلمة المرور؛ لذا يجب عليك استخدام رمز الوصول الشخصي:

BASH
# 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)

▶ مثال: الاستنساخ باستخدام بروتوكولات مختلفة

BASH
# 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 تلقائيًا بإنشاء التكوين التالي:

100%
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

BASH
# 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) فرع مستنسخ

تقوم عملية «الاستنساخ» باسترداد جميع الفروع البعيدة، لكنها تقوم بسحب الفرع الافتراضي فقط:

BASH
# 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'

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

BASH
# 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


❓ أسئلة شائعة

س ما الفرق الأساسي بين نسخ ملف ZIP وتنزيله؟
ج يؤدي النسخ (Cloning) إلى إنشاء مستودع Git كامل يتضمن السجل بأكمله، ومعلومات الفروع، وبيانات التعريف الخاصة بالتحكم في الإصدارات. يمكنك عرض السجل، والتبديل بين الإصدارات، وإنشاء فروع، ودفع التغييرات. أما تنزيل ملف ZIP فيوفر مجرد لقطة من الملفات؛ ولا يتضمن وظائف التحكم في الإصدارات، لذا لا يمكنك تنفيذ أي عمليات Git.
س ماذا يمكنني أن أفعل إذا كان استنساخ مستودع كبير يستغرق وقتًا طويلاً؟
ج يمكنك استخدام النسخ السطحي --depth 1 لتنزيل أحدث عمليات الالتزام فقط، مما سيجعل العملية أسرع بكثير. إذا كنت تحتاج إلى فرع معين فقط، فاستخدم الخيار --single-branch. بالإضافة إلى ذلك، يمكنك استخدام موقع مرآة محلي لتسريع العملية، مثل مرآة fastgit التابعة لـ GitHub.
س كيف يمكنني إرسال التغييرات إلى مستودع بعيد بعد النسخ؟
ج تحتاج إلى أذونات الإرسال. لا يمكن سوى استنساخ المستودعات العامة، ولا يمكن إرسال البيانات إليها. أما بالنسبة للمستودعات التي تتطلب أذونات، فستحتاج إلى إدخال اسم المستخدم ورمز التوثيق عند استخدام HTTPS، أو تكوين مفتاح SSH عند استخدام SSH. أمر الإرسال هو git push origin <分支名>.
س كيف يمكنني استنساخ مستودع خاص؟
ج تتطلب المستودعات الخاصة المصادقة. بالنسبة لبروتوكول HTTPS، استخدم اسم المستخدم ورمز الوصول الشخصي (Personal Access Token)؛ أما بالنسبة لبروتوكول SSH، فاستخدم مفتاح SSH الذي تم تكوينه. تأكد من أن حسابك يتمتع بأذونات الوصول؛ وإلا فستظهر لك رسالة «فشل المصادقة».
س ماذا أفعل إذا ظهرت لي الرسالة التالية: "خطأ فادح: مسار الوجهة موجود بالفعل"؟
ج المجلد المستهدف موجود بالفعل. يمكنك حذف المجلد الموجود وإعادة نسخه، أو نسخه إلى مجلد آخر git clone <地址> <新目录名>، أو الدخول إلى المجلد الموجود وتشغيل git pull لتحديثه.

📖 ملخص


📝 تمارين

  1. تمرين أساسي: انسخ مستودع «Hello-World» على GitHub، واطلع على معلومات المستودع البعيد وجميع الفروع، وافهم بنية الدلائل بعد النسخ.

  2. تمرين متقدم: قارن بين النسخ الكامل والنسخ السطحي --depth 1 لمشروع كبير (مثل React)، وقارن بين وقت التنزيل وحجم مجلد .git، وتعرف على حالات استخدام النسخ السطحي.

  3. التحدي: ابحث عن كيفية استنساخ مستودع محليًّا ثم إرساله إلى مستودع بعيد آخر (على سبيل المثال، الاستنساخ من GitHub والإرسال إلى GitLab)، واكتسب فهمًا لإدارة المستودعات البعيدة وتكوينات المستودعات البعيدة المتعددة.

Web-Tutorial.com

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

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

100%