Hands-On: A Real Merge Conflict
Hands-On: Simulate a Real Merge Conflict
Two branches edit src/ping.py on Linux. You merge, resolve markers, and delete the feature branch.
Setup
Start from a clean ~/desk/desk-api (chapter 3 hands-on).
cd ~/desk/desk-api
git switch main
git statusBranch A — JSON ping
git switch -c feat/ping-json
cat > src/ping.py <<'EOF'
def ping():
return {"status": "ok"}
EOF
git add src/ping.py
git commit -m "feat(ping): return a dict"
git switch mainBranch B — string change on main
cat > src/ping.py <<'EOF'
def ping():
return "OK"
EOF
git add src/ping.py
git commit -m "feat(ping): shout the status"main and feat/ping-json have diverged.
A ← B ← C ← shout main ping.py returns "OK"
↖
dict feat/ping-json ping.py returns {"status": "ok"}
Merge and conflict
git merge feat/ping-json
git status
cat src/ping.pyYou should see markers. Open the file in nvim (or your editor) and make it:
def ping():
return {"status": "OK"}No marker lines left.
git add src/ping.py
git commit -m "merge feat/ping-json"
git log --oneline --graph --decorate -8
git branch -d feat/ping-jsonDone when
git statusis clean onmainsrc/ping.pyreturns a dict with"OK"feat/ping-jsonis deletedgit log --graphshows a merge commit (two parents)
If you want to retry: git reset --hard to the commit before the merge (only on this lab clone), or copy the repo and run the page again. Chapter 9 rebases this same divergence into a straight line instead.