Resolve a Git Merge Conflict Without Losing Changes
Read the conflict
In the clean conflict lab, the base notes.txt contains Tea. main changed it to Tea and milk; feature changed it to Tea and rice. Both changed the same line differently. A merge pauses when Git cannot choose a safe automatic result.
The merge returns nonzero. Short status includes UU notes.txt. In this fixture, markers show <<<<<<< HEAD, ======= and >>>>>>> feature around the competing lines. The current commit has not advanced.
Decide what the final file should say
For this shopping note, both additions are desired. Edit the file to contain both items. In a real project ask what behavior is intended; removing markers alone is not a resolution. Some conflicts involve deletion or renaming and may have no text markers.
printf "Tea and milk and rice\n" > notes.txtgit diff --checkgit add -- notes.txtgit status --shortgit diff --cachedThe resolved line is staged. git diff --check detects some whitespace errors and conflict markers in diffs, but does not prove semantic correctness. Review the staged change and test behavior.
Finish the merge
Once all paths are resolved, record the merge. git merge --continue is another way to finish after staging all resolutions and may open an editor.
git commit -m "Merge shopping notes"git show HEAD:notes.txtgit rev-list --parents -n 1 HEADgit status --shortThe committed line includes milk and rice, the commit has two parents, and status is clean. The original branch commits remain in history.
Practice
Try resolving the conflict by keeping only milk. Would Git allow the commit? Why would the result still fail the stated shopping-note requirement?
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Resolve a Git Merge Conflict Without Losing Changes — knowledge check
Reason about merge history, conflict state and safe integration.
What does UU notes.txt indicate in the fixture?
What should determine the final conflict resolution?
What does git add notes.txt do after resolving the conflict?
What does a clean git diff --check prove?
Which final line meets the stated shopping-note requirement?