Lesson 14 / الدرس 14
git merge: bringing the work back / git merge: إعادة العمل
Merging joins a branch back into another. It happens in one of two ways depending on whether the other branch moved while you were away — and knowing which you are getting explains the output.
الدمج يصل branch بأخرى. ويحدث بإحدى طريقتين بحسب هل تحركت الأخرى وأنت غائب — ومعرفة أيهما ستحصل عليها تفسّر المخرَج.
git switch main go to the branch that RECEIVES the work
git merge add-search bring add-search into it
git branch -d add-search the name has done its job
git merge main while on add-search does the opposite thing — which is sometimes what you want, as lesson 13 mentioned, but it is not how you finish a piece of work. git merge main وأنت على add-search يفعل العكس — وهو ما تريده أحيانًا كما ذكر الدرس 13، لكنه ليس كيف تنهي قطعة عمل.Two kinds of merge
FAST-FORWARD — main did not move while you worked
before: A ── B <- main
\
C ── D <- add-search
after: A ── B ── C ── D <- main, add-search
the name simply slid forward. No new commit.
MERGE COMMIT — main moved while you worked
before: A ── B ── E <- main
\
C ── D <- add-search
after: A ── B ── E ── M <- main
\ /
C ── D
M is a new commit with TWO parents.
main it just moves the name forward — that is a fast-forward, and it is why some merges produce no new commit at all. If both branches moved, it builds a merge commit joining them. Run the demonstration to read the output of each.
main فهو ينقل الاسم إلى الأمام فحسب — وذلك تقديم سريع (fast-forward)، ولهذا لا تنتج بعض الدمجات commit جديدة إطلاقًا. وإن تحركت الـbranchان، بنى commit دمج يصل بينهما. شغّل العرض لقراءة مخرَج كل واحد.
A merge commit is the only kind with two parents, and that is what makes git log --graph worth using: the lines that split and rejoin are branches that existed and were merged. It is a record of how the work actually happened, which is why many teams prefer it even when a fast-forward was possible.
git log --graph يستحق الاستخدام: فالخطوط التي تنقسم وتلتقي هي branches وُجدت ودُمجت. وهو سجل لكيفية حدوث العمل فعلًا، ولهذا تفضّله فرق كثيرة حتى حين كان التقديم السريع ممكنًا.After the merge
| Command | Answers |
|---|---|
| git log --oneline --graph | What the history looks like now |
| git branch --merged | Which branches are safely merged and can go |
| git branch --no-merged | Which still hold work not in this branch |
| git branch -d add-search | Delete a merged branch — refuses if it is not |
git branch --merged is the safe way to tidy up: everything it lists is already contained in your current branch, so deleting those names loses nothing at all.
git branch --merged هو الطريق الآمن للترتيب: فكل ما يسرده موجود أصلًا في branch الحالية، فحذف تلك الأسماء لا يفقد شيئًا إطلاقًا.git status، وأودع أو أزح كل ما هو معلّق، ثم ادمج. وإن ساء الدمج، فـgit merge --abort يعيدك إلى حيث كنت بالضبط.Sometimes git cannot decide on its own — both branches changed the same lines of the same file, and it stops and asks you. That is a conflict, it is normal, and it is the whole of the next lesson. Nothing is broken when it happens.
… في Source Control فيها Branch ← Merge Branch، وهي تسأل من أي branch تأخذ — سؤال الاتجاه نفسه في الأمر. ويُظهر الرسم بعدها بوضوح هل حصلت على تقديم سريع أم commit دمج، وهي أسرع طريقة لبناء حدس للفرق.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
To bring add-search into main, where do you stand?
add-search في main، أين تقف؟You stand on the branch that receives and name the branch you take from. The other direction is a real command that does the opposite thing.
2. What is a fast-forward merge?
It is why some merges produce no merge commit at all. The history ends up a straight line.
3. A merge goes wrong halfway through. What returns you to where you were?
--abort exists precisely for this and is the safe choice — unlike reset --hard, which would take uncommitted work with it.
--abort وُجد لهذا بالضبط وهو الخيار الآمن — بخلاف reset --hard الذي يأخذ معه العمل غير المودَع.Score / النتيجة: 0 / 3
Your task / مهمتك
Produce both kinds of merge deliberately and show the difference. First merge a branch into an untouched main and show the fast-forward output. Then make a branch, commit on it, also commit something on main, and merge — showing the merge commit. Include git log --oneline --graph after each so the two shapes are visible, and tidy up with git branch --merged.
أنتج نوعي الدمج عمدًا وأظهر الفرق. ادمج أولًا branch في main لم تُمسّ وأظهر مخرَج التقديم السريع. ثم اصنع branch وأودع عليها، وأودع شيئًا على main أيضًا، وادمج — مُظهرًا commit الدمج. وأدرج git log --oneline --graph بعد كل واحد ليظهر الشكلان، ورتّب بـgit branch --merged.
- A real fast-forward, with its output تقديم سريع حقيقي بمخرَجه
- A real merge commit, with its output commit دمج حقيقية بمخرَجها
- The graph after each, showing the two shapes الرسم بعد كل واحد، مُظهرًا الشكلين
- A merged branch deleted safely branch مدموجة محذوفة بأمان