Bringing Work Together
Branches let work grow separately. Merging brings it back together: git merge <branch> takes the commits on another branch that your current branch does
not have yet and combines them into the current branch.
You always merge into the branch you are on. To bring events into
main, switch to main first, then run git merge events. The other branch
does not change.
Fast-Forward or Merge Commit
What Git does depends on the shape of the history.

Fast-forward. In the top half of the diagram, main has no commits of its
own since events started from it. Everything on main is already part of
events, so there is nothing to combine: Git moves main forward to E.
No new commit is made, and Git reports Fast-forward.
Three-way merge. In the bottom half, both branches have new commits since
they split at N. Git compares three snapshots: the two branch tips, E and
R, and the commit where they split, N, called the merge base. It works
out what each side changed since the merge base, combines both sets of changes
and records the result in a new merge commit, M. A merge commit has two
parents: the previous tip of your branch and the tip of the branch you merged.
Git reports Merge made by the 'ort' strategy., naming the method it used.
For a merge commit, Git needs a message. It prepares one,
Merge branch 'recipes', and on a terminal opens your editor with it: save and
close to accept. --no-edit accepts the prepared message without the editor,
and -m "<message>" supplies your own.
When the two sides changed the same lines differently, Git cannot combine them on its own and stops with a conflict. The next lesson covers that.
Choosing to Always Record a Merge
Some teams want every merged branch to appear as a merge commit, even when a
fast-forward was possible, so the history shows where each piece of work came
in. --no-ff asks for that:
git merge --no-ff <branch>
Before You Merge
- Be on the branch that should receive the work.
git branchshows it. - Start from a clean
git status. Git refuses to merge when uncommitted edits would be overwritten, and the merge documentation warns that merging with other uncommitted changes can leave you in a state that is hard to back out of. - Afterwards, read
git log --oneline --graph --allto see what happened.
Exercise: A Fast-Forward and a Three-Way Merge
A newsletter has two branches ready: events and recipes. You will merge
both into main and compare the results.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It creates
mainwith one commit and two branches, each with one commit of its own:mkdir -p ~/git-practice/merge-practice cd ~/git-practice/merge-practice git init -b main cat > newsletter.md <<'EOF' # Newsletter Hello, readers. EOF git add newsletter.md git commit -m "Add newsletter draft" git switch -c events cat > events.md <<'EOF' # Events Picnic on Saturday. EOF git add events.md git commit -m "Add events" git switch main git switch -c recipes cat > recipe.md <<'EOF' # Recipe of the month Lemon rice. EOF git add recipe.md git commit -m "Add recipe" git switch main -
Look at the starting shape and predict what each merge will do:
git log --oneline --graph --all* 7898148 (events) Add events | * 7dfa55b (recipes) Add recipe |/ * 8653a21 (HEAD -> main) Add newsletter draftmainhas no commits of its own, so merging either branch first can fast-forward. -
Merge
events:git merge events git log --oneline --graph --allGit prints
Updating, the two short IDs, andFast-forward. The graph shows(HEAD -> main, events)on "Add events":mainsimply moved. -
Merge
recipes. Now both sides have commits the other lacks, so Git makes a merge commit. Your editor opens withMerge branch 'recipes'; save and close it.git merge recipes git log --oneline --graph --all git show --stat lsGit prints
Merge made by the 'ort' strategy.Illustrative graph:* eb69636 (HEAD -> main) Merge branch 'recipes' |\ | * 7dfa55b (recipes) Add recipe * | 7898148 (events) Add events |/ * 8653a21 Add newsletter draftgit show --statincludes aMerge:line with two short IDs: the merge commit's two parents.lslists all three files. -
Merge
recipesagain:git merge recipesGit says
Already up to date.:mainalready contains every commit onrecipes.
Success: the graph shows the events merge as a straight line with no
merge commit, and the recipes merge as a merge commit with two parents.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
An editor opened during git merge | Git is asking for the merge commit's message | Save and close to accept it |
merge: recipe - not something we can merge | A misspelled branch name | Check the name with git branch |
Your local changes to the following files would be overwritten by merge | Uncommitted edits to a file the merge changes | Commit or discard them, then merge |
| The merge brought work into the wrong branch | You were on a different branch | git reset --hard ORIG_HEAD undoes an unshared merge; check git status first |
CONFLICT | Both branches changed the same lines | Run git merge --abort for now; the next lesson resolves conflicts |
-
Clean up:
cd ~/git-practice ls rm -rf merge-practice ls
Independent task: before cleaning up, create a branch named credits from
main, commit a file credits.md on it, switch back to main and merge it so
that a merge commit is recorded even though a fast-forward is possible. Show
from the graph that it worked.
Check Your Understanding
Predict whether each merge will fast-forward.
- You created
featurefrommain, made two commits onfeature, and nobody has committed tomainsince. Onmain, you rungit merge feature. - After you created
feature, a teammate's fix was committed tomain. Onmain, you rungit merge feature. - On
main, you rungit merge featurea second time with no new commits on either branch.
Answer and Reasoning
Independent task
git switch -c credits
cat > credits.md <<'EOF'
Thanks to everyone who wrote for this issue.
EOF
git add credits.md
git commit -m "Add credits"
git switch main
git merge --no-ff credits
git log --oneline --graph --all
Without --no-ff, this merge would fast-forward, because main has no
commits that credits lacks. With it, the graph shows a new
Merge branch 'credits' commit on main and a side line through
"Add credits": a merge commit with two parents.
Understanding check
- Fast-forward.
mainhas no commits of its own sincefeaturestarted, so Git just movesmaintofeature's tip. - Merge commit. Both branches now have commits the other lacks, so Git combines them from their merge base and records a commit with two parents.
- Neither: Git reports
Already up to date., becausemainalready contains everything onfeature.