iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git merge

git merge combines two branches. A fast-forward moves the pointer; a real merge creates a merge commit with two parents. Conflicts happen when both sides touched the same lines.

Fast-forward, no-ff, squash, conflicts

EXAMPLE
# 1) The setup
git switch main
git pull
git switch -c feature/add-cart
# … work, commit …

# 2) Update feature with latest main (avoid surprise on the merge)
git fetch origin
git rebase origin/main        # cleaner history, rewrites your commits
# OR
git merge  origin/main        # merge commit appears in feature

# 3) Merge feature into main
git switch main
git merge feature/add-cart           # fast-forward if main hasn't moved
git merge --no-ff feature/add-cart   # ALWAYS make a merge commit (preserves feature history)
git merge --squash feature/add-cart  # collapse to one commit, then `git commit`

# 4) Choose a merge strategy
git merge -X theirs branch     # on conflict, prefer their side
git merge -X ours   branch     # prefer ours
git merge -s ours   branch     # accept the merge but DROP all changes from branch
git merge --no-commit branch   # stage but don't commit — inspect first

# 5) Conflicts — see what to do
git merge feature/foo
# CONFLICT (content): Merge conflict in src/app.ts
git status                              # list conflicted files
git diff                                # see the conflict markers

# Inside the conflicted file:
# <<<<<<< HEAD
# (your side)
# =======
# (their side)
# >>>>>>> feature/foo

# Resolve manually OR pick a side:
git checkout --ours   src/app.ts
git checkout --theirs src/app.ts

# Use a 3-way merge tool
git mergetool                  # opens vimdiff/meld/vscode

# Mark resolved
git add src/app.ts
git commit

# Abort and start over
git merge --abort

# 6) Inspect a merge after the fact
git log --merges --oneline
git log --oneline --graph --decorate --all
git show <merge-sha>

# 7) Re-merge cleanly with rerere (Reuse Recorded Resolution)
git config --global rerere.enabled true
# Git remembers your conflict resolutions and applies them next time

# 8) Octopus merge (merge >2 branches at once)
git merge branch-a branch-b branch-c
# Fails on conflicts; rarely used outside CI manifest merges.

# 9) Common workflows
# Trunk-based:   rebase feature → fast-forward into main (linear history)
# Git Flow:      merge --no-ff to preserve feature branches
# Squash-merge:  GitHub default — one PR = one commit on main

Why it matters

Pick one merge style and stick with it across the team. Mixed (some squash, some merge-commit, some rebase) makes git log unreadable.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git switch main
git merge feature/login
# Use --no-ff to always create a merge commit.
Try it Yourself »

Exercise

Merge "feature" into the current branch.

git feature

Test yourself

Q1. Merge "feature" into current branch with…
Q2. For "always create a merge commit" pass…
Q3. After resolving conflicts in a merge, you…

Discussion

Loading…