Merging Basics
The vegan pancakes were a success. Now that recipe needs to come home to main, where everyone reads it. Merging is how the work from one branch joins another, and knowing which kind of merge Git will do removes most of the mystery.
The rule
Stand on the branch that receives, then merge in the branch that gives.
Merging always brings another branch into the branch you are on:
git switch main
git merge vegan-pancakesGit then does one of two things.
Fast-forward. If main has not moved since the branch was created, main is simply behind. Git slides the main label forward to the branch's tip. No new commit is made.
Updating 9b2d7e4..c41a0f2
Fast-forward
pancakes.md | 6 +++---Merge commit. If both branches gained new commits, their histories have split. Git combines the two tips, using the commit where they split as a reference, and records a new merge commit with two parents. Your editor may open with a ready-made message such as Merge branch 'vegan-pancakes'; save and close it to finish.
Conflicts. When both branches changed the same lines of the same file, Git cannot choose for you. It stops and marks the file:
<<<<<<< HEAD
2 cups milk
=======
2 cups oat milk
>>>>>>> vegan-pancakesThe part above ======= is your current branch; the part below is the incoming one. Edit the file to the version you want, delete the three marker lines, then git add pancakes.md and git commit to finish the merge. If you would rather back out, git merge --abort returns everything to how it was before the merge. Resolving tricky conflicts and choosing between merge strategies are topics for more advanced Git work.
After a successful merge, the branch has done its job: git branch -d vegan-pancakes.
The shortcut
Before merging, ask "Has the receiving branch moved since we split?" Check with git log --oneline --graph --all. One straight line means a fast-forward; a fork means a merge commit, and possibly conflicts where the same lines changed.
Receive on main
git switch main. The receiving branch is the one you stand on.
The trap is merging the wrong way round. git merge main while standing on vegan-pancakes brings main's changes into the experiment, and main itself does not change at all.
Drills
Answer fast — trust the rule.
Asha wants the vegan-pancakes work inside main. What should she run?
main has no new commits since vegan-pancakes was created. What kind of merge happens?
How many parents does a merge commit have?
During a conflict, what does the text between <<<<<<< HEAD and ======= show?
Mid-conflict, Asha decides she is not ready to merge. Which command puts things back?
Remember
- Stand on the branch that receives, then merge in the branch that gives.
- A straight line means fast-forward; a fork means a merge commit.
- Conflicts: edit to the version you want, remove the markers,
git add,git commit, orgit merge --abort.
