Lesson 12 / الدرس 12
Creating and switching branches / إنشاء الـbranches والتبديل بينها
Two commands, and one surprise: switching branches changes the files in your folder. Your editor will show different content, and nothing is lost.
أمران، ومفاجأة واحدة: التبديل بين branches يغيّر الملفات في مجلدك. فسيعرض محررك محتوى مختلفًا، ولا شيء يضيع.
git branch list branches; * marks the one you are on
git branch new-header create one, and stay where you are
git switch new-header move onto it
git switch -c new-header create AND move onto it — the usual one
git switch - back to the branch you were on before
git branch -d new-header delete it (refuses if unmerged)
git branch -D new-header delete it anyway — see lesson 11's warning
git checkout -b for the same thing; switch was added later precisely because checkout did too many unrelated jobs. Both still work. git checkout -b للشيء نفسه؛ وأُضيف switch لاحقًا لأن checkout كان يقوم بأعمال كثيرة غير مترابطة. وكلاهما ما يزال يعمل.Switching changes your files
$ git switch new-header
Switched to branch 'new-header'
Your editor now shows the files as they are on THIS branch.
The other branch's version of index.html has not been deleted —
it is in the history, and switching back brings it straight back.
If you are half way through something and genuinely cannot commit it, git stash puts your uncommitted changes aside and gives you a clean working directory; git stash pop brings them back afterwards. It is useful and it is also easy to forget you have done it, so treat it as a short-term tool rather than a place to keep things.
git stash يضع تغييراتك غير المودَعة جانبًا ويعطيك مجلدًا عاملًا نظيفًا؛ وgit stash pop يعيدها بعدها. وهو نافع، ومن السهل أيضًا أن تنسى أنك فعلته، فعامله أداةً قصيرة الأجل لا موضعًا تحفظ فيه الأشياء.Naming a branch
| Name | Verdict |
|---|---|
| fix-contact-form | Good — says what it is for |
| add-search | Good |
| feature/dark-mode | Good — the slash groups related work |
| issue-42-slow-search | Good — links to a ticket and says what it is |
| test | Poor — a test of what? |
| ahmed | Poor — a branch is a piece of work, not a person |
| new | Poor — everything was new once |
A branch usually lives for a day or two and then disappears, so the name only has to make sense to you for that long — but you will have several open at once, and git branch listing four names you cannot tell apart is its own small misery.
git branch يسرد أربعة أسماء لا تميّز بينها بؤس صغير بذاته.main — أو إلى branch الأسبوع الماضي — أشيع خطأ في الـbranches على الإطلاق. وgit status يقولها في السطر الأول، ويمكن جعل مِحَثّك يعرضها، ويضعها VS Code في الزاوية. وهو قابل للإصلاح، لكن الإصلاح متعب والفحص يستغرق ثانية.
If it does happen, the fix is short: git switch -c the-right-branch takes your uncommitted work with you onto a new branch. If you already committed to the wrong one, create the branch where you are, then use git reset from lesson 10 to move the wrong branch back.
git switch -c the-right-branch يأخذ عملك غير المودَع معك إلى branch جديدة. وإن كنت أودعت في الخطأ بالفعل، فأنشئ الـbranch حيث أنت، ثم استخدم git reset من الدرس 10 لإرجاع الـbranch الخطأ.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does git switch -c fix-form do?
git switch -c fix-form؟It is the two-in-one you will type most. The older spelling for the same thing is git checkout -b.
git checkout -b.2. You switch branches. What happens to the files in your folder?
Your editor genuinely shows different content. Nothing is lost — the other version is in the history and switching back restores it.
3. Git refuses to switch, saying your changes would be overwritten. What is it doing?
Uncommitted work has no copy anywhere, so git will not silently destroy it. Commit first, or stash.
Score / النتيجة: 0 / 3
Your task / مهمتك
Create two branches in your repository and make a different change on each — something visible, such as a heading with different text. Then switch between them repeatedly, showing git branch and the contents of the changed file each time, so the file visibly changes under you. Finish by deleting one branch and explaining what that did and did not remove.
أنشئ branchين في مستودعك وأجرِ تغييرًا مختلفًا على كل واحدة — شيئًا مرئيًا كعنوان بنص مختلف. ثم بدّل بينهما مرارًا، مُظهرًا git branch ومحتوى الملف المتغيّر في كل مرة، ليتغير الملف تحت عينيك. واختم بحذف branch واشرح ما الذي حذفه وما لم يحذفه.
- Two branches with a genuinely different file on each branchان بملف مختلف فعلًا على كل واحدة
- The file shown changing as you switch الملف معروض وهو يتغير مع التبديل
- git branch output, with the * marking where you are مخرَج git branch، و* تعلّم أين أنت
- What deleting a branch did and did not remove ما الذي حذفه حذف الـbranch وما لم يحذفه