Merging
Understand the idea
A fast-forward only moves a label. A real merge creates a commit with two parents.
The idea
A joins two lines. When the other is a direct ancestor, Git only moves your label. That is a fast-forward. When both sides have new commits, the merge commit stores two parents and a combined snapshot.
What changes
Fast-forward changes a label and checks out that snapshot. A true merge adds one commit whose parents are HEAD and the branch you named. The becomes the combined snapshot. If the same lines differ, the command stops with conflict markers instead of guessing. You will practice that choice in the conflict lesson.
| History | Result | Graph |
|---|---|---|
| The other branch is behind you | Fast-forward | Your label moves, no new commit |
| Both sides have commits | Merge commit | One node, two parents |
| The same lines disagree | Conflict | Markers in the file, merge unfinished |
The command
Make a commit on main, branch topic, commit a different file there, switch back to main, and run git merge topic. Because the edits do not overlap, the graph shows a commit with two parent hashes. If topic was simply ahead of main, the same command only slides the main label.
The mistake
Check your graph
After a diverged merge, the new commit lists both parents. main points at it. topic still points at its own tip, which is now one of those parents.
Your repository
Merge a branch that diverged. The graph shows either a moved label or a commit with two parent hashes.
Step 1
Start a line of work
signup begins at the current commit. If it already exists, run git switch signup.
Step 2
Add a file that main does not have
Create extra.txt in the editor and set the text to: signup form
Step 3
Stage that file
git add copies extra.txt from the editor into the index.
Step 4
Save the signup commit
signup now has one commit that main does not.
Step 5
Change main too
Switch back, then in the editor add this line to README.md: Shared project notes
Step 6
Stage the main edit
The README change is still only in the editor until you add it.
Step 7
Commit the main edit
Both branches now have a commit the other does not.
Step 8
Join the two lines
Merge brings signup into main. The graph shows a commit with two parents, or a moved label if one side had not diverged.
Step 9
Read the join
The new commit lists both parents when the histories had diverged.
The editor edits the working tree. Start a repository and the files appear here.
Commit graph
No commits yet. git init creates the first one.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.