Git: دليل تفصيلي لإدارة المستودعات البعيدة في Git

تُعد المستودعات البعيدة أساس التعاون بين أعضاء الفريق. فمن خلال المستودعات البعيدة، يمكن لأعضاء الفريق مشاركة الكود ومزامنة التغييرات والتعاون في عملية التطوير. ويدعم Git وجود مستودعات بعيدة متعددة، مما يتيح التكوين المرن لنماذج التعاون المختلفة.

1. أساسيات المستودع البعيد

(1) ما هو المستودع البعيد؟

المستودع البعيد هو مستودع للتحكم في الإصدارات يتم استضافته على خادم ويب، ويُستخدم من أجل:

100%
graph TB
    subgraph Local
        A[DeveloperALocal Warehouse]
        B[DeveloperBLocal Warehouse]
        C[DeveloperCLocal Warehouse]
    end
    
    subgraph Remote
        D[Remote Repository<br/>GitHub/GitLab]
    end
    
    A <-->|push/pull| D
    B <-->|push/pull| D
    C <-->|push/pull| D
    
    style D fill:#d4edda

(2) أنواع المستودعات البعيدة

منصات استضافة المستودعات البعيدة الشائعة:

(3) اسم المستودع البعيد

بشكل افتراضي، يستخدم Git اسمين:



2. عرض مستودع بعيد

(1) عرض اسم المستودع البعيد

▶ مثال: سرد المستودعات البعيدة

BASH
# View the name of the remote repository
git remote

# Output:
# origin

# View All Remote Repositories(IncludingURL)
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

(2) عرض تفاصيل المستودع البعيد

▶ مثال: عرض التفاصيل

BASH
# VieworiginDetails
git remote show origin

# Output:
# * 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
#     feature  new (next fetch will store in remotes/origin)
#   Local branches configured for 'git pull':
#     main     merges with remote main
#     develop  merges with remote develop
#   Local refs configured for 'git push':
#     main     pushes to main     (up to date)
#     develop  pushes to develop  (local out of date)

(3) عرض الفروع البعيدة

▶ مثال: سرد الفروع البعيدة

BASH
# View Remote Branches
git branch -r

# Output:
#   origin/HEAD -> origin/main
#   origin/main
#   origin/develop
#   origin/feature

# View All Branches(Local+Remote)
git branch -a

# Output:
# * main
#   develop
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature


3. إضافة مستودع بعيد

(1) إضافة مستودع بعيد جديد

▶ مثال: إضافة مستودع بعيد

BASH
# Add a Remote Repository
git remote add origin https://github.com/user/repo.git

# Add a second remote repository
git remote add upstream https://github.com/original/repo.git

# Add a named remote repository
git remote add backup git@backup-server.com:repo.git

# Verification: Added successfully
git remote -v

# Output:
# origin    https://github.com/user/repo.git (fetch)
# origin    https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)
# backup    git@backup-server.com:repo.git (fetch)
# backup    git@backup-server.com:repo.git (push)

(2) استخدام عناوين URL مختلفة لعمليات الاسترداد والإرسال

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

BASH
# Add a Remote Repository: different URLs for fetch and push
git remote add origin https://github.com/user/repo.git
git remote set-url --push origin git@github.com:user/repo.git

# View Configuration
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  git@github.com:user/repo.git (push)

# Common config: fetch via HTTPS, push via SSH

(3) إضافة "origin" تلقائيًا عند الاستنساخ

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

BASH
# Automatically add when cloning a repositoryorigin
git clone https://github.com/user/repo.git

# View Remote Repositories
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# Specify a different name when cloning
git clone -o myremote https://github.com/user/repo.git

git remote -v

# Output:
# myremote  https://github.com/user/repo.git (fetch)
# myremote  https://github.com/user/repo.git (push)


4. تعديل المستودع البعيد

(1) إعادة تسمية مستودع بعيد

▶ مثال: إعادة التسمية

BASH
# Rename a Remote Repository
git remote rename origin github

# Verification
git remote -v

# Output:
# github  https://github.com/user/repo.git (fetch)
# github  https://github.com/user/repo.git (push)

(2) تعديل عنوان URL للمستودع البعيد

▶ مثال: تعديل عنوان URL

BASH
# Modify a Remote RepositoryURL
git remote set-url origin https://github.com/newuser/newrepo.git

# Editfetch URL
git remote set-url --delete origin https://github.com/user/repo.git
git remote set-url --add origin https://github.com/newuser/newrepo.git

# Editpush URL
git remote set-url --push origin git@github.com:user/repo.git

# Verify Changes
git remote -v

(3) إضافة عناوين URL وحذفها

▶ مثال: إدارة عناوين URL متعددة

BASH
# Addfetch URL
git remote set-url --add origin https://backup.com/repo.git

# Addpush URL
git remote set-url --add --push origin https://backup.com/repo.git

# DeleteURL
git remote set-url --delete origin https://backup.com/repo.git

# View AllURL
git remote -v


5. حذف مستودع بعيد

(1) إزالة مرجع مستودع بعيد

▶ مثال: حذف مستودع بعيد

BASH
# Delete a remote repository reference(Does not affect the remote repository itself)
git remote remove upstream

# Or userm
git remote rm backup

# Confirm Deletion
git remote

# Output:
# origin

(2) إزالة الإشارات إلى الفروع البعيدة التي انتهت صلاحيتها

▶ مثال: تنظيف فرع بعيد

BASH
# Delete a remote branch(Branches on a remote repository)
git push origin --delete feature

# Clean up references to remote branches that have been deleted locally
git fetch -p

# Or
git remote prune origin

# View the branches that need to be cleaned up
git remote prune origin --dry-run

# Output:
# * origin/feature would be pruned


6. العمليات المتقدمة على مستودعات البيانات البعيدة

(1) تكوين سير عمل الفروع

▶ مثال: تكوين سير عمل «فورك»

BASH
# 1. ForkFrom the original repository to your account

# 2. Clone YourselfFork
git clone https://github.com/yourname/repo.git

# 3. Add the original repository asupstream
git remote add upstream https://github.com/original/repo.git

# 4. View Remote Repositories
git remote -v

# Output:
# origin    https://github.com/yourname/repo.git (fetch)
# origin    https://github.com/yourname/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

# 5. Synchronize with upstream updates
git fetch upstream
git merge upstream/main

# 6. Push to your ownFork
git push origin main

(2) سير العمل مع مستودعات متعددة عن بُعد

100%
graph TB
    A[Local Warehouse] -->|push| B[GitHub<br/>origin]
    A -->|push| C[GitLab<br/>gitlab]
    A -->|push| D[Backup Server<br/>backup]
    
    B -->|pull| A
    C -->|pull| A
    
    style A fill:#fff3cd
    style B fill:#d4edda
    style C fill:#c3e6cb
    style D fill:#cce5ff

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

BASH
# Add Multiple Remote Repositories
git remote add origin https://github.com/user/repo.git
git remote add gitlab https://gitlab.com/user/repo.git
git remote add backup git@backup-server.com:repo.git

# Push to all remote repositories
git push origin main
git push gitlab main
git push backup main

# Or addremote.pushDefaultLayout
git config remote.pushDefault origin

# AddpushurlAutomatically Push to Multiple Repositories
git remote set-url --add --push origin https://github.com/user/repo.git
git remote set-url --add --push origin https://gitlab.com/user/repo.git

# Nowpush originIt will be pushed to both repositories at the same time
git push origin main

(3) عرض التحديثات على المستودع البعيد

▶ مثال: عرض التحديثات عن بُعد

BASH
# Get Remote Updates(Do not merge)
git fetch origin

# View the latest status of a remote branch
git branch -r

# View RemotemainBranch Commits
git log origin/main

# Compare the differences between local and remote
git diff main origin/main

# View details of a remote branch
git show origin/feature


7. تكوين المستودعات البعيدة

(1) تهيئة إعدادات المستودع البعيد

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

BASH
# View Remote Repository Configuration
git config --local --list | grep remote

# Output:
# remote.origin.url=https://github.com/user/repo.git
# remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

# LayoutfetchStrategy
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

# Layoutpush URL
git config remote.origin.pushurl git@github.com:user/repo.git

# Configure the default push branch
git config branch.main.remote origin
git config branch.main.merge refs/heads/main

(2) SSH مقابل HTTPS

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

BASH
# UsageHTTPS
git remote set-url origin https://github.com/user/repo.git

# UsageSSH(Recommendations)
git remote set-url origin git@github.com:user/repo.git

# UsageGitAgreement
git remote set-url origin git://github.com/user/repo.git

# HTTPSYou must enter your password each time,Or use credential storage
git config --global credential.helper store

# SSHA key needs to be configured
ssh-keygen -t ed25519 -C "your_email@example.com"
# Add the public key toGitHub/GitLab


❓ أسئلة شائعة

س ما الفرق بين مصطلحي «المصدر» و«المنبع»؟
ج «origin» هو المستودع الذي قمت بإنشاء نسخة منه، و«upstream» هو المستودع الأصلي. في سير عمل «fork»، تقوم بسحب التحديثات من «upstream»، ثم إرسالها إلى «origin»، ثم إنشاء طلب سحب.
س كيف يمكنني إرسال التغييرات إلى عدة مستودعات بعيدة في آن واحد؟
ج استخدم git remote set-url --add --push لإضافة عدة عناوين URL للدفع، بحيث يتم إرسال دفعة واحدة إلى جميع عناوين URL التي تم تكوينها. أو يمكنك الدفع يدويًّا إلى كل مستودع بعيد.
س هل يؤثر حذف مرجع مستودع بعيد على المستودع البعيد؟
ج لا. git remote remove هذا الإجراء يحذف التكوين المرجعي المحلي فقط؛ ولا يؤثر على المستودع البعيد نفسه. فالمستودع البعيد لا يزال موجودًا؛ ولكنه لم يعد مُتتبعًا محليًّا.
س أيهما أفضل، HTTPS أم SSH؟
ج يُعد SSH أكثر ملاءمة؛ فبمجرد إعداد مفتاح، لن تحتاج إلى إدخال كلمة مرور في كل مرة. أما HTTPS فهو أبسط — فهو لا يتطلب إعداد مفتاح — لكنك تحتاج إلى إدخال كلمة مرور أو استخدام مخزن بيانات الاعتماد. نوصي باستخدام SSH.
س كيف يمكنني الاطلاع على تفاصيل مستودع بعيد؟
ج استخدم git remote show <name> لعرض المعلومات التفصيلية، بما في ذلك عنوان URL، والفروع التي يتم تتبعها، وقواعد السحب/الدفع المُعدَّة، وغير ذلك.

📖 ملخص


📝 تمارين

  1. تمرين أساسي: إنشاء مستودع محلي، وإضافة مستودع بعيد على GitHub، وعرض معلومات المستودع البعيد، وتعديل عنوان URL الخاص بالمستودع البعيد، وممارسة العمليات الأساسية لإدارة المستودعات البعيدة.

  2. تمرين متقدم: قم بمحاكاة سير عمل «التفرع» (fork): أضف مستودعين عن بُعد، هما origin وupstream؛ واسحب التحديثات من upstream؛ وانقلها إلى origin؛ وافهم نموذج التعاون في سير عمل «التفرع».

  3. التحدي: تهيئة عمليات الإرسال التلقائي إلى عدة مستودعات بعيدة: إعداد عدة عناوين URL للإرسال لمستودع بعيد واحد بحيث يتم إرسال عملية إرسال واحدة في وقت واحد إلى GitHub وGitLab وخادم النسخ الاحتياطي.

Web-Tutorial.com

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

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

100%