Merging & Conflict Resolution
Integrate branches with merge and rebase, and resolve conflicts when they arise
Knowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Merging integrates the changes from one branch into another.
git switch main
git merge feature-login # bring feature-login changes into mainFast-forward merge — if main has no new commits since the branch split off, Git just moves the main pointer forward. No merge commit, linear history.
3-way merge — if both branches have diverged, Git creates a merge commit that ties them together.
Merge conflicts happen when the same lines were changed differently in both branches. Git marks them:
<<<<<<< HEAD
color: blue; ← your current branch
=======
color: red; ← incoming branch
>>>>>>> feature-uiYou edit the file to keep what you want, remove the markers, then:
git add conflicted-file.css
git commit # completes the mergeRebase — alternative to merge. Replays your commits on top of the target branch, producing a linear history:
git switch feature-login
git rebase main # replay feature-login commits on top of mainExamples
Merge a feature branch into main
Always switch to the target branch first, then merge the source into it.
git switch main
git merge feature-login
# If fast-forward: no merge commit, history stays linear
# If diverged: Git opens editor for a merge commit messageResolve a merge conflict
Conflicts are normal in team work. The key steps: edit the file, stage it, commit.
git merge feature-ui
# CONFLICT (content): Merge conflict in styles.css
# Open styles.css, find <<<< markers, edit to desired state
# Remove all <<<, ===, >>> lines
git add styles.css
git commit # Git pre-fills the merge commit messageHow well did you understand this?
Next in Git & Version Control
Remotes, GitHub & Push/Pull