Sub topic 9
Checking your resolution
This chapter assumes you can run git merge, read conflict markers, and edit a conflicted file by hand. Here you learn how to confirm a resolution is correct before and after you commit it.
Why verify
Git marks a conflict as resolved when you run git add on the file. It does not check that the content is correct. You can stage a file that still contains marker lines, or one that merges cleanly but no longer works. A short check before the commit catches both problems.
Before committing
Search for leftover markers
Look for any marker line that remains in the working tree.
git diff --checkgit diff --check reports conflict markers and whitespace errors in the changes. It exits with a non-zero status when it finds a problem, so scripts and hooks can use it.
You can also search directly:
grep -rnE '^(<<<<<<<|=======|>>>>>>>)' .A legitimate line of ======= can appear in some files, such as reStructuredText or Markdown headings. Treat each hit as something to look at, not as proof of a problem.
Confirm nothing is still unmerged
git status
git ls-files -ugit status should no longer list "Unmerged paths". git ls-files -u should print nothing. If a path still appears, it still has stages 1, 2 and 3 in the index, and you have not staged a resolution for it.
Review what the merge will contain
During a merge, git diff with no arguments shows only conflicts you have not staged. After staging, compare the result with each parent:
git diff --cached main
git diff --cached featureThe first command shows what the merge changes relative to main. The second shows what it changes relative to feature. A good resolution looks like a sensible mix of both. If a file is identical to one side, check that you meant to discard the other side's change.
For a combined view of a merge, git diff --cc works on a conflicted file. It shows only the lines that differ from both parents.
Run the project's checks
Git understands text, not meaning. Two edits that do not overlap can still break each other, for example when one branch renames a function and the other adds a call to the old name. Build the project and run its tests before you commit.
After committing
Inspect the merge commit itself:
git show --cc HEAD
git log --oneline --graph -5git show --cc displays the combined diff for the merge commit. It lists the lines where the result differs from all parents, which is where your manual edits are. If the output is empty, the result matches one parent or merges cleanly everywhere.
If you find a mistake
The right fix depends on whether you have shared the merge.
| Situation | Action |
|---|---|
| Not yet committed | Edit the file, run `git add` again, then check again. Use `git merge --abort` to start over. |
| Committed, not pushed | Fix the file and run `git commit --amend`, or reset the branch with `git reset --hard ORIG_HEAD` and merge again. |
| Already pushed | Add a new commit that fixes the problem. Do not rewrite shared history. |
ORIG_HEAD points to where your branch was before the merge started. Git sets it when a merge, rebase, or reset moves the branch, so it works for undoing a merge you just made. Later operations overwrite it.
Exercise
You resolved a conflict in config.txt, ran git add, and committed. Later you notice the file still contains a line starting with <<<<<<<. Which earlier check would have caught it, and what do you do if the commit is already pushed?
Show answer
Running git diff --check, or searching the files for marker lines, before the commit would have found it. Git does not check content when you stage a file. Because the commit is pushed, do not rewrite history. Remove the marker lines, stage the file, and make a new commit that fixes it.
Exercise
git ls-files -u prints nothing, and the tests pass. Why might you still want to run git diff --cached main before committing?
Show answer
Those checks show that no files are unmerged and that the code works, but not that you kept the right changes. The diff against main shows exactly what the merge adds to main, so you can confirm that the other branch's changes are present and that you did not drop either side's work by accident.