Branches and merging
Merging and conflicts
Goal: Merge branches and resolve conflicts · 10 min
To bring a branch's work into main: switch to main, then git merge feature.
- Fast-forward: if main has no new commits since the branch started, Git just moves the main pointer forward.
- Merge commit: if both branches have new commits, Git creates a commit with two parents.
- Conflict: if both changed the same lines, Git stops and marks the file:
<<<<<<< HEAD
<h1>Welcome</h1>
=======
<h1>Hello!</h1>
>>>>>>> feature
The part above ======= is yours (HEAD), below is the incoming branch. Edit the file to the correct final content, delete the markers, then git add the file and git commit. git merge --abort cancels the merge.
Worked example: Resolving a conflict
- The merge stops with a conflict in index.html.
- Edit the file: keep the right heading, remove all markers.
- Stage the resolved file and complete the merge.
git switch main
git merge feature
# CONFLICT (content): Merge conflict in index.html
nano index.html
git add index.html
git commit