🔀 Branching & merging for real

You will make a side line of work, then bring it home, and see exactly what git does in each case.

1Make a bookmark, wander off, come back

You already know the big secret: a branch is not a copy of your project. It is a bookmark in your photo album. Making one costs nothing and changes nothing.

🔖 Two bookmarks can sit in the same album. One marks "the official story so far" (main). The other marks "the page I am experimenting on". You move one bookmark forward as you add photos. The other one just stays where you left it.

Here is the whole dance, start to finish:

git switch -c my-idea
Switched to a new branch 'my-idea'
# ...edit files, take a photo, edit more, take another photo...
git switch main
Switched to branch 'main'
git merge my-idea
🗣 Git's command for "make a new bookmark and move me onto it" is git switch -c my-idea. The -c means create. Old-timers say git checkout -b my-idea; it is the exact same move with an older word. To hop between existing bookmarks, it is just git switch main.

While you sit on my-idea and commit, that blue bookmark slides forward with each new photo. The green main bookmark does not move. It is waiting for you, right where you left it.

One surprise the first time you see it: when you run git switch main, the files on your disk change to match that bookmark. Your experiment seems to vanish. It has not. Every photo you took on my-idea is safe in the album. Switch back and the files reappear, exactly as you left them. Committed work is almost impossible to lose.

Then git merge my-idea brings the side work into main. There are only two things that can happen next. Let us watch both, using real save points from your own project.

2Case 1: the bookmark slide (fast-forward)

Picture this: you went off on a branch and took three photos. Meanwhile, main did nothing. Nobody added anything to it. Main is simply three save points behind you, on the very same straight string of beads.

So what does "merging" even need to do? Press play and find out. These are real save points from your own project.

d4cd26a 836f2de db039fb f30f6b7 email review tool finished content review fixes todo update your branch main before: main is three save points behind

Look at what did NOT happen. No new photo was taken. Your files on disk did update, just like when you switch bookmarks, but the album gained nothing new. The beads are the same beads. Git just slid a label along the string until it caught up. That is the gentlest merge there is.

🗣 Git's word for the bookmark slide is a "fast-forward". Like pressing ⏩ on a paused video: nothing new is created, you just catch up to where the story already got to. It is only possible when history stayed one straight line.

3Case 2: two loose ends (a real merge)

Now the more interesting situation: while you were working on your branch, main ALSO grew. Maybe you fixed something quick on main, or a teammate did. Either way, the string of beads forked. Now there are two loose ends.

🪢 Two loose ends of string get tied with one knot. You cannot slide a bookmark across a fork; the bookmark would not know which end to follow. So git ties the two ends together with one special new bead that holds both strands.

This is the real fork from your own project. Both lines grew out of 836f2de. Press play to tie them back together.

836f2de finished content 0fdf109 fb9fecb launch plan launch tracking db039fb f30f6b7 review fixes todo update your branch main M remembers BOTH parents

This time git really does take ONE new photo. That photo contains both sets of changes: the launch work from one strand and the fixes from the other. And look at its sticky note: it lists TWO previous photos as its parents, not one. The fork is tied back into a single line, and nothing from either side is lost.

🗣 Git's word for that knot bead is a "merge commit". It is a normal save point in every way except one: its sticky note points back at two parents instead of one. That is how the album remembers the fork ever happened.

One question is probably forming in your head: what if both sides edited the same line of the same file? Good question. Git will pause and ask YOU which version to keep. That is a conflict, and it is a question, not an error. Next lesson is all about answering it calmly.

4How do you know which case you will get?

Here is the nice part: you do not have to know. You type git merge either way, and git looks at the string of beads and picks for itself.

What the history looks likeWhat git does
One straight line, main just fell behindSlides the bookmark forward (fast-forward)
Two loose ends, both sides grewTakes one new tying photo (merge commit)

Either way, the command is the same and your work from both sides ends up in the album. There is no wrong button to press here.

5Do it with your own hands

⌨️ Try it yourself

Back to the playground from lesson 5. (If you deleted it, just redo the quick setup from lesson 5 first.) You will make a branch, commit on it, and merge it back.

cd /tmp/git-playground
git switch -c add-greeting
echo "hi there" >> notes.txt
git add -A
git commit -m "Add greeting"
git switch main
git merge add-greeting
# watch the output closely...
git log --oneline

This one is the bookmark-slide case: main never moved while you were away. Git even tells you so. Look for the word Fast-forward in the merge output. No new photo, just a label catching up.

6Check yourself

Q. When is the bookmark-slide (fast-forward) merge impossible?

A bookmark can only slide along one straight string. If the branch you are merging into grew its own beads, the history forked, so git has to tie the two ends together with a merge commit instead. The number of commits and the size of the files do not matter at all.

Think about it: why can the bookmark-slide merge never have a conflict?

A conflict needs two competing edits to the same thing. In a fast-forward there is only one straight line of work, so there is nothing for your changes to compete with. Git just slides the label and goes back to sleep.
🧠 If you remember one thing: merging is either sliding a bookmark or taking one photo with two parents, and git chooses the right one for you.