Merging Branches
What is Merging?
Merging combines the changes from one branch into another. It's how you bring your completed feature back into the main code.
The Merge Workflow
# 1. Switch to the branch you want to merge INTO
git switch main2. Merge the feature branch
git merge feature-login
Before Merge
main: A ─── B ─── C
\
feature: D ─── E
After Merge
main: A ─── B ─── C ─────── F (merge commit)
\ /
feature: D ─── E
Git creates a merge commit (F) that combines both histories.
Fast-Forward Merge
If main hasn't changed since you branched, Git does a simpler fast-forward merge:
Before:
main: A ─── B
\
feature: C ─── DAfter (fast-forward):
main: A ─── B ─── C ─── D
No merge commit needed — Git just moves the pointer forward.
Merge Conflicts
Sometimes Git can't automatically merge because both branches changed the same lines. This is called a merge conflict.
<<<<<<< HEAD
This is the line from main
=======
This is the line from feature
>>>>>>> feature-login
To resolve:
- Open the file with the conflict
- Choose which version to keep (or combine them)
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Stage and commit:
git add . && git commit -m "Resolve merge conflict"
Summary
| Command | Action |
|---|---|
| <code>git merge branch-name</code> | Merge branch into current |
| <code>git log --oneline --graph</code> | Visualize merge history |
💡 Tip: Always switch to main first, then merge the feature branch into it.