Git: شرح مفصل لعمليات «push» في Git والمزامنة عن بُعد
يُقصد بـ«الدفع» عملية تحميل التزام محلي إلى مستودع بعيد. ومن خلال عملية الدفع، يمكن لأعضاء الفريق مشاركة الكود ومزامنة التغييرات والتعاون في عملية التطوير. ويُعد فهم الخيارات والاعتبارات المختلفة المتعلقة بعملية الدفع أمرًا ضروريًا لتعاون الفريق.
1. المفاهيم الأساسية للإشعارات الفورية
(1) ما المقصود بالإشعار الفوري؟
يُقصد بـ«الدفع» عملية تحميل عمليات التسجيل من مستودع محلي إلى مستودع بعيد:
- تحميل التغييرات: إرسال التزام محلي جديد إلى المستودع البعيد
- تحديث الفرع: تحديث مؤشر الفرع البعيد
- رمز المزامنة: اسمح لأعضاء فريقك بمشاهدة التغييرات التي أجريتها
- نسخ الكود احتياطيًا: قم بنسخ الكود احتياطيًا إلى مستودع بعيد
graph LR
A[Local Warehouse<br/>Local Repository] -->|git push| B[Remote Repository<br/>Remote Repository]
B --> C[Team Members<br/>Team Members]
C -->|git pull| D[Other Local Repositories]
style A fill:#fff3cd
style B fill:#d4edda
style C fill:#c3e6cb
(2) الشروط المسبقة لتفعيل الإشعارات الفورية
قبل إرسال إشعار فوري، تأكد من:
- توجد عمليات تسجيل جديدة محليًّا (غير موجودة على الخادم البعيد)
- الفرع المحلي مستمد من الفرع البعيد (لا يوجد انقسام)
- حق الوصول للكتابة إلى مستودع بعيد
- اتصال الشبكة يعمل بشكل سليم
(3) اتجاه الدفع
يُعد «الدفع» عملية أحادية الاتجاه: من الجهاز المحلي → إلى الجهاز البعيد
- لا يؤثر الدفع على المستودع المحلي
- سيؤدي النقر إلى تحديث الفرع البعيد
- بعد عملية «الدفع»، يمكن لأعضاء الفريق «سحب» التحديثات
2. عمليات الدفع الأساسية
(1) النشر إلى فرع بعيد
▶ مثال: إشعار فوري بسيط
# Push the current branch tooriginthe corresponding branch
git push
# Output:
# fatal: The current branch feature has no upstream branch.
# To push the current branch and set the remote as upstream, use
# git push --set-upstream origin feature
# Push and configure the upstream branch
git push -u origin feature
# Or
git push --set-upstream origin feature
# Output:
# Enumerating objects: 5, done.
# Counting objects: 100% (5/5), done.
# Writing objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done.
# Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
# remote: Resolving deltas: 100% (1/1), completed with 1 local object.
# remote:
# remote: Create a pull request for 'feature' on GitHub by visiting:
# remote: https://github.com/user/repo/pull/new/feature
# To https://github.com/user/repo.git
# * [new branch] feature -> feature
# Branch 'feature' set up to track remote branch 'feature' from 'origin'.
(2) النشر إلى فرع محدد
▶ مثال: النشر إلى فرع معين
# PushmainBranch toorigin
git push origin main
# Push a local branch to a remote branch with a different name
git push origin local-branch:remote-branch
# Push the current branch to the remote repositorymainBranch
git push origin HEAD:main
# Push all local branches
git push --all origin
# Output:
# To https://github.com/user/repo.git
# * [new branch] develop -> develop
# * [new branch] feature -> feature
# * [new branch] main -> main
(3) التحقق بعد الإرسال
▶ مثال: التحقق من نتائج الإرسال الفوري
# View Remote Branches
git branch -r
# Output:
# origin/HEAD -> origin/main
# origin/main
# origin/feature
# View the upstream settings for a local branch
git branch -vv
# Output:
# * main a1b2c3d [origin/main] feat: Add Feature
# feature d4e5f6g [origin/feature] WIP: New Features
# Check the status of the remote repository
git remote show origin
3. علامات الدفع
(1) إرسال العلامات إلى المستودع البعيد
▶ مثال: علامات الدفع
# Create a tag
git tag v1.0.0
# Push a Single Tag
git push origin v1.0.0
# Output:
# To https://github.com/user/repo.git
# * [new tag] v1.0.0 -> v1.0.0
# Push All Tags
git push --tags
# Or
git push origin --tags
# Send tags along with the push notification
git push --follow-tags
# Push only lightweight tags
git push --follow-tags origin main
(2) حذف علامة عن بُعد
▶ مثال: حذف علامة
# Delete Local Tags
git tag -d v1.0.0
# Delete Remote Tag
git push origin --delete v1.0.0
# Or userefspecGrammar
git push origin :refs/tags/v1.0.0
# Output:
# To https://github.com/user/repo.git
# - [deleted] v1.0.0
(3) استراتيجية «تاج بوش»
graph TB
A[Create a tag] --> B{Tag Type}
B -->|Lightweight Tags| C[No automatic push notifications]
B -->|Footnote Label| D[Recommended Posts]
C --> E[Manual Push]
D --> F[git push --tags]
style D fill:#d4edda
style F fill:#c3e6cb
4. إشعارات الدفع الإجبارية
(1) متى يكون «الدفع القسري» ضروريًا؟
تُستخدم ميزة «Force push» لتعديل سجل عمليات الالتزام الخاصة بعملية التزام تم إرسالها مسبقًا:
- تم تعديل آخر عملية تسجيل (--amend)
- أعاد كتابة التاريخ باستخدام
rebase - عمليات التسجيل المدمجة
- تم إصلاح عملية تسجيل غير صحيحة
(2) استخدام الخيار --force
▶ مثال: الإرسال القسري
# Modify the last commit
git commit --amend -m "Revised commit message"
# Regular push notifications will fail
git push origin main
# Output:
# ! [rejected] main -> main (non-fast-forward)
# error: failed to push some refs to 'https://github.com/user/repo.git'
# Mandatory Push Notifications
git push --force
# Or
git push -f
# Output:
# + a1b2c3d...d4e5f6g main -> main (forced update)
(3) استخدام الخيار --force-with-lease (موصى به)
▶ مثال: الإرسال القسري لأغراض أمنية
# --force-with-lease Safer
# If there are new commits pushed by others on the remote repository,Will reject the push notification
git push --force-with-lease
# Output:
# + a1b2c3d...d4e5f6g main -> main (forced update)
# If there are new commits on the remote
git push --force-with-lease
# Output:
# ! [rejected] main -> main (stale info)
# error: failed to push some refs to 'https://github.com/user/repo.git'
(4) مخاطر الإشعارات الفورية الإلزامية
graph TB
A[Mandatory Push Notifications] --> B{There is a new commit on the remote repository?}
B -->|Yes| C[Overwrite Another User's Submission<br/>Data Loss]
B -->|No| D[Security Updates]
C --> E[Teamwork Issues]
D --> F[Normal operation]
style C fill:#f8d7da
style D fill:#d4edda
⚠️ تحذير هام:
- قد تؤدي عمليات الدفع القسرية إلى استبدال عمليات التسجيل الخاصة بأعضاء الفريق
- قبل تنفيذ عملية «دفع» القوة، قم بعملية «سحب» للتحقق من حالة جهاز التحكم عن بُعد
- استخدم
--force-with-leaseأولاً - توخَّ الحذر عند استخدام الإشعارات الإجبارية أثناء العمل الجماعي
5. شرح مفصل لخيارات «الدفع»
(1) خيارات الإرسال الشائعة
| الخيار | الوصف | حالة الاستخدام |
|---|---|---|
-u |
إنشاء فرع Upstream | إرسال فرع للمرة الأولى |
-f |
«Force Push» | «Push» بعد تعديل السجل |
--force-with-lease |
إشعار أمني إلزامي | الطرق الموصى بها للإرسال الإلزامي |
--all |
إرسال جميع الفروع | إرسال دفعي |
--tags |
نشر جميع العلامات | إصدار |
--dry-run |
محاكاة النشر | معاينة محتوى النشر |
--verbose |
المخرجات التفصيلية | إرسال لتصحيح الأخطاء |
▶ مثال: استخدام خيار «الدفع»
# Simulated Push Notification(Do not push)
git push --dry-run
# Output:
# To https://github.com/user/repo.git
# * [new branch] feature -> feature
# Detailed Output
git push --verbose
# Push and display progress
git push --progress
# Skip the hook during a push
git push --no-verify
(2) تكوين الدفع
▶ مثال: تكوين سلوك الإرسال الفوري
# Set the Default Push Policy
git config --global push.default simple
# push.default options:
# nothing - Do not push,Must be explicitly specified
# current - Push the current branch to the remote branch with the same name
# upstream - Push the current branch to its upstream branch
# simple - Similarupstream,But the branch names must be the same(Default)
# matching - Push all matching branches
# Configure Automatic Upstream Setup
git config --global push.autoSetupRemote true
# Right now, directlygit pushIt will automatically configure the upstream connection.
git push
(3) خطافات الدفع
▶ مثال: خطاف الدفع
# pre-pushHooks are executed before a push
# .git/hooks/pre-push
#!/bin/sh
# Run tests before deployment
npm test
if [ $? -ne 0 ]; then
echo "Tests failed, aborting push"
exit 1
fi
# Skip Hook Push
git push --no-verify
6. سيناريوهات الإشعارات الفورية وأفضل الممارسات
(1) التعامل مع إشعارات الدفع المرفوضة
▶ مثال: التعامل مع حالات رفض الإشعارات الفورية
# Push Rejected
git push origin main
# Output:
# ! [rejected] main -> main (fetch first)
# error: failed to push some refs to 'https://github.com/user/repo.git'
# hint: Updates were rejected because the remote contains work that you do
# hint: not have locally. This is usually caused by another repository pushing
# hint: to the same ref. You may want to first integrate the remote changes
# hint: (e.g., 'git pull ...') before pushing again.
# Solution1:Pull First, Then Push
git pull --rebase origin main
git push origin main
# Solution2:Push after merging
git pull origin main
git push origin main
# Solution3:Mandatory Push Notifications(Use with caution)
git push --force-with-lease origin main
(2) سير عمل «الدفع»
sequenceDiagram
participant Developer
participant Local Warehouse
participant Remote Repository
Developer->>Local Warehouse: git add & commit
Developer->>Remote Repository: git fetch
Developer->>Local Warehouse: git merge/rebase
Developer->>Remote Repository: git push
Remote Repository->>Developer: Push successful
(3) أفضل الممارسات المتعلقة بالإشعارات الفورية
▶ مثال: أفضل الممارسات الخاصة بالإشعارات الفورية
# 1. Fetch the latest code before pushing.
git fetch origin
git rebase origin/main
# 2. Make sure your submission is complete
git status
git log origin/main..HEAD
# 3. Push to a feature branch
git push -u origin feature/user-auth
# 4. Create Pull Request (on GitHub)
# 5. Delete the remote branch after merging
git push origin --delete feature/user-auth
❓ أسئلة شائعة
git pull أو git fetch + git merge/rebase لمزامنة التحديثات البعيدة، ثم قم بحل أي تعارضات قبل الإرسال.--amend لتعديل عملية التزام، أو استخدام rebase لإعادة كتابة السجل، أو دمج عمليات الالتزام. ومع ذلك، استخدم هذه الطريقة بحذر؛ وافضل استخدام --force-with-lease بدلاً من ذلك.git push -u origin <branch-name> لإرسال الفرع الجديد وتعيين الفرع الرئيسي. بعد ذلك، يمكنك استخدام git push للإرسال مباشرةً.git push origin <branch> لإرسال الفرع، واستخدم git push origin <tag> أو git push --tags لإرسال جميع العلامات. لا يتم إرسال العلامات تلقائيًا مع الفرع.📖 ملخص
- عملية «الدفع» هي عملية تحميل التزام محلي إلى مستودع بعيد.
- عملية الدفع الأساسية:
git push origin <branch>الدفع إلى فرع بعيد - إعداد فرع المنبع: استخدم الخيار
-uلتعيين فرع المنبع؛ ويمكنك بعد ذلك إرسال التحديثات مباشرةً - إرسال العلامات:
--tagsإرسال جميع العلامات، أو إرسال علامة واحدة - «Force Push»:
--forceالكتابة القسرية،--force-with-leaseأكثر أمانًا - أفضل الممارسات في عملية الدفع: قم بالسحب أولاً، ثم الدفع؛ واستخدم عمليات الدفع القسري بحذر
📝 تمارين
-
تمرين أساسي: قم بإنشاء مستودع محلي وإضافة مستودع بعيد؛ ثم قم بإنشاء «كوميت» ودفعه إلى المستودع البعيد لتجربة عملية الدفع بالكامل، بما في ذلك إعداد فرع «أبستريم».
-
تمرين متقدم: قم بمحاكاة سيناريو يتم فيه رفض عملية الإرسال: أنشئ «كوميت» جديدًا في المستودع البعيد، وأنشئ «كوميت» جديدًا محليًّا، وحاول إرساله ليتم رفضه، ثم قم بحل المشكلة باستخدام
pullأوrebaseقبل إعادة الإرسال. -
التحدي: جرب عملية «الدفع القسري»: استخدم
--amendلتعديل «كوميت» تم دفعه بالفعل، ثم استخدم--force-with-leaseلإجراء «الدفع القسري»، وافهم مبادئ ومخاطر «الدفع القسري».