Merge branches and understand fast-forward merges
Merge branches and understand fast-forward merges
When a feature is ready, Git can integrate its history into another branch. The exact result depends on whether the target branch has moved since the feature branch started.
What you’ll learn
- Merge a branch.
- Distinguish fast-forward and merge commits.
- Read the post-merge graph.
Two merge shapes
Fast-forward
Target is an ancestor of source.
move pointer forwardDiverged
Both lines have new commits.
combine historiesVerify
Inspect and test the result.
merged code is not automatically correct
Merge combines reachable histories
If main has not advanced, Git may fast-forward main to the feature tip. If both branches have new commits, a normal merge can create a merge commit with two parents.
The merge commit records that two lines of development were combined. Git does not automatically decide whether the combined behavior is correct; tests and review still matter.
Merge a feature into main
Run this in a disposable practice repository unless the lesson explicitly says otherwise.
git switch main
git merge feature/search
git log --oneline --decorate --graph --allIf main is still an ancestor of feature/search, the merge can be a fast-forward. If main diverged, Git may create a merge commit after combining file changes.
Practice: Merge branches
Your task: Create one branch that can fast-forward and another scenario where both main and feature receive commits. Compare the graph after each merge.
Common pitfall to avoid
Do not use merge as a substitute for testing. A conflict-free merge only means Git could combine the text automatically, not that the program behaves correctly.
Check your understanding
Merge branches: what to remember
Merge branches and understand fast-forward merges is complete when you can explain the state being changed before you run the command, not merely repeat the syntax.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.