Episode 9 of 12
Merging Branches and Conflicts
Learn how to merge branches together and resolve merge conflicts.
Merging combines the changes from one branch into another. Sometimes changes conflict and need manual resolution.
Basic Merge
# Switch to the branch you want to merge INTO
git switch main
# Merge the feature branch
git merge feature-navbar
Fast-Forward Merge
If no new commits were made on main since branching, Git does a fast-forward merge — it simply moves the pointer forward.
Merge Commit
If both branches have new commits, Git creates a merge commit that combines both histories.
Merge Conflicts
Conflicts occur when both branches changed the same lines in the same file. Git can't decide which version to keep, so you must resolve it manually.
# After a conflicted merge, Git marks the file:
<<<<<<< HEAD
<h1>Welcome to our website</h1>
=======
<h1>Welcome to my awesome site</h1>
>>>>>>> feature-branch
Resolving Conflicts
- Open the conflicted file
- Choose which version to keep (or combine them)
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Stage and commit the resolved file
# After manually fixing the conflict:
git add resolved-file.html
git commit -m "Resolve merge conflict in header"
Abort a Merge
# If things go wrong, cancel the merge
git merge --abort
Tips for Avoiding Conflicts
- Pull from
mainfrequently to stay up to date - Keep branches small and focused
- Communicate with your team about which files you're editing
- Merge feature branches quickly — don't let them get too old