Git: دليل تفصيلي حول عمليات «الخروج» و«تبديل الفروع» في Git
Checkout هو أمر متعدد الاستخدامات وهام في Git يتيح لك التبديل بين الفروع، واستعادة الملفات، وسحب التزامات محددة. وقد أدخلت نسخة Git 2.23 الأمرين git switch وgit restore الأكثر وضوحًا لتقاسم مهام الأمر checkout.
1. نظرة عامة على الأمر checkout
(1) الوظائف المتعددة لـ «checkout»
- تبديل الفروع: التبديل إلى فرع آخر
- إنشاء الفروع والتبديل بينها: إنشاء فرع جديد والتبديل إليه على الفور
- استرداد الملفات: استعادة الملفات من المستودع
- الكشف عن الالتزام: الانتقال إلى التزام معين (تقسيم مؤشر الرأس)
graph TB
A[git checkout] --> B[Switch Branches]
A --> C[Create a Branch]
A --> D[Restore Files]
A --> E[Checkout and Submit]
style A fill:#fff3cd
style B fill:#d4edda
style C fill:#c3e6cb
style D fill:#cce5ff
style E fill:#f8d7da
(2) إدخال أوامر جديدة
نظرًا لوجود عدد كبير جدًّا من وظائف سحب التغييرات، أدخلت نسخة Git 2.23 أمرين جديدين هما:
git switch: تُستخدم خصيصًا للتبديل بين الفروعgit restore: مصمم خصيصًا لاستعادة الملفات
(3) مقارنة الأوامر
| العملية | الخروج | التبديل/الاستعادة |
|---|---|---|
| تغيير الفرع | git checkout <branch> |
git switch <branch> |
| إنشاء والتبديل | git checkout -b <branch> |
git switch -c <branch> |
| استعادة الملف | git checkout -- <file> |
git restore <file> |
| استعادة المخزون | git checkout HEAD -- <file> |
git restore --staged <file> |
2. التبديل بين الفروع
(1) أساسيات التبديل
▶ مثال: الانتقال إلى فرع موجود
# Traditional Method
git checkout feature
# A New Approach(Recommendations)
git switch feature
# Output:
# Switched to branch 'feature'
# Switch tomainBranch
git switch main
# Output:
# Switched to branch 'main'
# Your branch is up to date with 'origin/main'.
(2) شرح مفصل لعملية التبديل
عند التبديل بين الفروع، يقوم Git بتنفيذ الإجراءات التالية:
sequenceDiagram
participant User
participant Git
participant HEAD
participant Workspace
User->>Git: git switch feature
Git->>Git: Check unsaved changes
Git->>HEAD: UpdateHEADPointer
HEAD->>Git: OrientationfeatureBranch
Git->>Workspace: Update File
Workspace->>User: Switchover complete
(3) خيارات التبديل
▶ مثال: استخدام خيار التبديل
# Switch to the previous branch
git switch -
# Output:
# Switched to branch 'main'
# Switch Branches and Create a Branch(If it does not exist)
git switch -c new-feature
# Force Switch(Discard Local Changes)
git switch -f feature
# Switch workspaces without changing them(Update OnlyHEAD)
git switch --detach feature
3. إنشاء الفروع والتبديل بينها
(1) إنشاء فرع جديد
▶ مثال: الإنشاء والتبديل فورًا
# Traditional Method
git checkout -b feature
# A New Approach(Recommendations)
git switch -c feature
# Output:
# Switched to a new branch 'feature'
# Created based on a specific commit
git switch -c feature a1b2c3d
# Created from a remote branch
git switch -c feature origin/feature
# Output:
# Branch 'feature' set up to track remote branch 'feature' from 'origin'.
# Switched to a new branch 'feature'
(2) خيارات إنشاء فرع
▶ مثال: خيارات إنشاء فرع
# Create a branch but do not switch to it
git branch feature
# Create and Switch,Set Up an Upstream Branch
git switch -c feature --track origin/feature
# Create a branch based on a specific tag
git switch -c v1.1-branch v1.0.0
# Create from a commit on another branch
git switch -c feature main~5
4. الملفات المكتشفة
(1) استعادة الملفات من المستودع
▶ مثال: استعادة ملفات مساحة العمل
# Traditional Method
git checkout -- README.md
# A New Approach(Recommendations)
git restore README.md
# Restore from a Specific Commit
git restore --source=a1b2c3d README.md
# Restore from HEAD (Latest Commit)
git restore --source=HEAD README.md
# Restore Multiple Files
git restore file1.js file2.js
# Restore the entire directory
git restore src/
(2) الاستعادة من منطقة التجهيز
▶ مثال: إلغاء الإعداد
# Traditional Method
git reset HEAD README.md
# A New Approach(Recommendations)
git restore --staged README.md
# Cancel the temporary save and restore the workspace at the same time
git restore --staged --worktree README.md
# Or
git checkout HEAD -- README.md
(3) استعادة الملفات المحذوفة
▶ مثال: استعادة الملفات المحذوفة عن طريق الخطأ
# Accidentally Deleted Files
rm important-file.js
# Restore from the repository
git restore important-file.js
# Or
git checkout HEAD -- important-file.js
# View Deleted Files
git status
# Output:
# deleted: important-file.js
# Recover All Deleted Files
git restore .
5. سحب التغييرات وتسجيلها (مؤشر الرأس المنفصل)
(1) ما هو مؤشر الفصل؟
عندما يتم سحب التزام — بدلاً من فرع — فإن HEAD يشير مباشرةً إلى ذلك الالتزام بدلاً من الإشارة إلى فرع. وتُسمى هذه الحالة «HEAD منفصل».
graph TB
subgraph Normal State
HEAD1[HEAD] --> main1[main]
main1 --> C1[SubmitC3]
end
subgraph Separator Arrow
HEAD2[HEAD] --> C2[Submita1b2c3d]
main2[main] --> C2
end
style HEAD1 fill:#d4edda
style HEAD2 fill:#f8d7da
(2) الدخول إلى حالة مؤشر رأس الفصل
▶ مثال: استرجاع سجل التغيير المحدد
# Detect Specific Commits
git checkout a1b2c3d
# Output:
# Note: switching to 'a1b2c3d'.
#
# You are in 'detached HEAD' state. You can look around, make experimental
# changes and commit them, and you can discard any commits you make in this
# state without impacting any branches by switching back to a branch.
#
# HEAD is now at a1b2c3d feat: Add a login feature
# Detection Label
git checkout v1.0.0
# Detect Remote Branches
git checkout origin/feature
(3) التشغيل في حالة مؤشر رأس الفاصل
▶ مثال: العمليات التي تتم تحت مؤشر الفاصل
# Currently in the "separate head" pointer state
git status
# Output:
# HEAD detached at a1b2c3d
# nothing to commit, working tree clean
# You can view the code
git log --oneline
# You can edit the file
echo "test" > test.js
# Can be submitted
git add test.js
git commit -m "test commit"
# Output:
# [detached HEAD d4e5f6g] test commit
# 1 file changed, 1 insertion(+)
# create mode 100644 test.js
# ⚠️ Note:This commit is not on any branch.!
(4) حفظ التغييرات على مؤشر رأس التقسيم
▶ مثال: حفظ التغييرات في فرع جديد
# A commit was created while the pointer was in the split head state.
# Method1:Create a new branch and save
git switch -c new-branch
# Output:
# Switched to a new branch 'new-branch'
# Method2:Merge into the existing branch
git branch temp-branch
git switch main
git merge temp-branch
git branch -d temp-branch
# If you switch branches without saving
git switch main
# You can do this byreflogRetrieve
git reflog
# d4e5f6g HEAD@{0}: checkout: moving from a1b2c3d to main
# a1b2c3d HEAD@{1}: commit: test commit
# Create a branch pointing to that commit
git branch saved-work d4e5f6g
6. حل التضاربات عند التبديل بين الفروع
(1) التعامل مع التغييرات غير المحفوظة
عند التبديل بين الفروع، إذا كانت هناك تغييرات لم يتم تثبيتها في دليل العمل، فقد يحدث ما يلي:
▶ مثال: التعامل مع التغييرات غير المحفوظة
# View Current Status
git status
# Output:
# Changes not staged for commit:
# modified: README.md
# Situation1:Modifications do not conflict with the new branch
git switch feature
# Switch Successful,The change has been saved
# Situation2:Conflicts Between Changes and a New Branch
git switch feature
# Output Error:
# error: Your local changes to the following files would be overwritten by checkout:
# README.md
# Please commit your changes or stash them before you switch branches.
(2) طرق حل تعارضات التبديل
▶ مثال: حل تعارضات التبديل
# Methods1:Submit Changes
git add .
git commit -m "WIP: Save as Draft"
git switch feature
# Methods2:Save Changes Temporarily(stash)
git stash
git switch feature
# After completing other tasks
git switch main
git stash pop
# Methods3:Cancel Edits
git restore .
git switch feature
# Methods4:Force Switch(Discard Changes)
git switch -f feature
# Methods5:Carry, Modify, Toggle(If possible)
git switch -m feature
# GitI'll try to merge the changes.
7. checkout مقابل switch مقابل restore
(1) ملخص مقارنات الأوامر
graph TB
A[GitCommand Selection] --> B{Operation Type}
B -->|Switch Branches| C[git switch]
B -->|Restore Files| D[git restore]
B -->|Complex Operations| E[git checkout]
style C fill:#d4edda
style D fill:#c3e6cb
style E fill:#fff3cd
(2) الاستخدام الموصى به
▶ مثال: استخدام أوامر Git الحديثة
# Switch Branches - Usageswitch
git switch feature
git switch -c new-feature
# Restore Files - Usagerestore
git restore README.md
git restore --staged README.md
# Detect Specific Commits - Usageswitch --detach
git switch --detach a1b2c3d
# checkoutReserved for special cases
git checkout HEAD~5 -- file.js # Restore a Specific File from a Previous Version
❓ أسئلة شائعة
checkout أم switch أم restore؟git switch للتبديل بين الفروع وgit restore لاستعادة الملفات. أما الأمر git checkout فهو معقد للغاية وقد يسبب الارتباك. ومع ذلك، لا يزال الأمر checkout مفيدًا في بعض السيناريوهات المتقدمة.HEAD مباشرةً إلى «كوميت» بدلاً من فرع. ويكمن الخطر في أن «الكوميتات» في هذه الحالة لا تنتمي إلى أي فرع، وقد تُفقد عند التبديل بين الفروع. يُنصح بإنشاء فرع جديد لحفظ عملك.git stash لتجهيز التغييرات؛ 3) استخدام git restore . لإلغاء التغييرات. اختر الطريقة المناسبة بناءً على أهمية التغييرات.git restore --source=<commit> <file> أو git checkout <commit> -- <file> لاستعادة الملفات من عملية التثبيت المحددة. على سبيل المثال: git restore --source=HEAD~5 README.md.git switch -c <new-branch> لإنشاء فرع جديد وحفظ عملك الحالي. إذا كنت قد قمت بالفعل بالتبديل بين الفروع، فيمكنك استخدام git reflog لاسترداد الالتزام، ثم استخدام git branch <name> <commit> لإنشاء الفرع.📖 ملخص
- الأمر
checkoutمتعدد الاستخدامات؛ يُنصح باستخدامswitchوrestoreبدلاً منه. - التبديل بين الفروع:
git switch <branch>تحديث HEAD ودليل العمل - إنشاء والتبديل:
git switch -c <branch>— يتم ذلك في خطوة واحدة - استعادة الملف:
git restore <file>الاستعادة من المستودع - فصل مؤشر HEAD: يشير HEAD مباشرةً إلى عملية الالتزام؛ لذا عليك إنشاء فرع لحفظ عملك
- التعامل مع التغييرات غير المحفوظة: قم بتثبيتها أو وضعها في قائمة التغييرات المحددة أو تجاهلها قبل التبديل بين الفروع
📝 تمارين
-
تمرين أساسي: أنشئ عدة فروع، واستخدم
git switchللتبديل بينها، ولاحظ كيف تتغير مساحة العمل عند التبديل بين الفروع، وافهم دور مؤشر HEAD. -
تمرين متقدم: قم بمحاكاة سيناريو «توجيه الرأس»: اطلع على «كوميت» تاريخي محدد، وأنشئ «كوميت» في تلك الحالة، ثم استخدم
git switch -cلإنشاء فرع جديد لحفظ عملك. -
التحدي: قم بمحاكاة سيناريو تطوير واقعي: أثناء تطوير ميزة ما على فرع مخصص للميزات، تحتاج فجأة إلى التبديل إلى الفرع الرئيسي لإصلاح خطأ عاجل. استخدم
stashلحفظ عملك الحالي، وبعد إصلاح الخطأ، قم باستعادة عملك ومتابعة التطوير.