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
The direction is the part people get wrong. You stand on the branch you want to change and name the branch you want to take from. 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.

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.
Git chooses automatically. If nothing happened on 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.

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.

After the merge

CommandAnswers
git log --oneline --graphWhat the history looks like now
git branch --mergedWhich branches are safely merged and can go
git branch --no-mergedWhich still hold work not in this branch
git branch -d add-searchDelete 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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. To bring add-search into main, where do you stand?

2. What is a fast-forward merge?

3. A merge goes wrong halfway through. What returns you to where you were?

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 مدموجة محذوفة بأمان
How do you want to submit? / كيف تريد التسليم؟