Sub topic 9
Checking your resolution
This chapter assumes you can already open a conflicted file, edit it, and run git add and git commit. It covers how to confirm that the result is correct before you commit and after you commit. A merge that finishes without errors is not necessarily a correct merge. Git only checks that the conflict markers are gone from the index. It does not check that the code works.
Why verification matters
When you resolve a conflict, you are making a decision that Git could not make for you. Git cannot tell whether your combined text is valid, whether it compiles, or whether it keeps the intent of both branches. Three kinds of mistake are common:
- Leftover conflict markers, such as
<<<<<<<or>>>>>>>, committed as ordinary text. - A file that is syntactically wrong after combining two edits, for example a duplicated line or a missing bracket.
- A file that is valid but loses a change from one side, because you kept only one version of a hunk.
A short checklist catches all three. Run it before git commit, and run the last steps again after the commit.
Before you commit
Search for leftover markers
Git does not stop you from staging a file that still contains marker lines. Search for them yourself. The example below uses the config.txt conflict from earlier chapters.
git grep -n -E '^(<<<<<<<|=======|>>>>>>>)'git grep searches tracked files in the working tree. The -n option prints line numbers. The pattern anchors each marker to the start of a line, which avoids false matches on text such as ======= inside a longer line. No output means no markers were found. Be aware that a Markdown file with a heading underline made of = characters will match the second alternative, so read the hits instead of treating any output as a failure.
Git also has a built-in check. git diff --check reports conflict markers and whitespace errors:
git diff --checkIf it prints lines such as config.txt:1: leftover conflict marker, fix them before you stage. An empty result with exit status 0 means nothing was found. This check is useful in scripts and in a pre-commit hook, because it returns a non-zero exit status when it finds a problem.
Review the combined diff
During a conflicted merge, git diff with no arguments shows a combined diff. It compares the working tree against both parents at once. After you edit the file, the output changes to show only what differs from each side.
git diffA combined diff uses two columns of markers at the start of each line. The first column compares to the current branch (HEAD), and the second compares to the branch being merged. A - in a column means the line is in that parent but not in your result. A + means the line is in your result but not in that parent.
For the timeout example, suppose you decided on timeout = 60. The diff looks like this:
diff --cc config.txt
index 5d2c1a3,8f4b9e0..0000000
--- a/config.txt
+++ b/config.txt
@@@ -1,1 -1,1 +1,1 @@@
- timeout = 30
-timeout = 60
++timeout = 60Read it from the left. The first line shows - in the first column only: the result drops timeout = 30 from the current branch. The second line shows - in the second column only, and so on. The ++ line is the line your result contains that neither parent had in that exact position. The exact index hashes will differ on your machine. What matters is that the result contains the lines you intended and nothing else.
If the combined diff is empty for a file, your result is identical to one of the parents for every hunk, which usually means you took one side entirely. That is fine if you meant to, and a warning sign if you meant to combine both.
You can also compare against a single side:
git diff --ours
git diff --theirs
git diff --baseThese compare the working tree to the stage 2 version, the stage 3 version, and the stage 1 version of the file. Use --ours to see everything you changed relative to your branch, and --theirs to see what you kept or dropped from the incoming branch.
Build and run the tests
Git cannot validate meaning, so use the project's own tools. Compile the code, run the linter, and run the test suite. If tests are slow, at least run the ones that cover the files you touched. A conflict in a function that two people changed in different ways is exactly where tests catch a logically wrong merge.
Do this before committing when you can. Failing tests are easier to fix while the merge is still open, because git merge --abort remains available and the history stays clean.
Check that nothing unmerged remains
Before you commit, confirm that no paths are still listed as unmerged:
git status
git ls-files -ugit status should show no entries under "Unmerged paths". git ls-files -u should print nothing. If a file still appears, you have not staged its resolution. git commit will refuse to run while unmerged entries exist.
After you commit
Inspect the merge commit
Once you commit, look at what Git recorded:
git show --cc
git log --oneline --graph -5git show on a merge commit prints a combined diff by default. It shows only the hunks where the result differs from all parents, which means it highlights exactly the parts where you had to make a decision. If the combined diff is empty, the merge introduced nothing beyond what the parents already contained. A merge with no manual edits looks like this. A merge where you chose and combined text will show your lines.
The graph from git log --graph should show two parents joining at the new commit. If it shows a straight line, the history was fast-forwarded or rebased instead of merged, which may not be what you intended.
To compare the result with each parent separately, use the parent selectors:
git diff HEAD^1 HEAD
git diff HEAD^2 HEADHEAD^1 is the branch you were on. HEAD^2 is the branch you merged in. The first command shows everything the merge brought into your branch. The second shows everything your branch contributed relative to the incoming branch. Both should match your understanding of the two lines of work.
Rerun the build
Run the full test suite again on the committed result. Passing tests before commit is a good sign, but you may have changed files after your last run. Running again on the exact commit confirms that what you recorded is what you tested.
Fixing a mistake you notice later
If you find an error after committing and have not pushed, you can amend the merge commit:
# fix the file, then:
git add config.txt
git commit --amendAmending a merge commit keeps both parents and replaces the recorded result. If you have already pushed, do not rewrite shared history. Make a new commit that corrects the file instead. If the whole resolution was wrong and you have not pushed, git reset --hard ORIG_HEAD returns the branch to where it was before the merge. Use it carefully, because it discards uncommitted work in the working tree.
Common gaps in verification
| Mistake | How it slips through | Check that catches it |
|---|---|---|
| Markers left in a file | Git lets you stage any text | `git diff --check` or `git grep` |
| Duplicate lines from both sides | File looks plausible | Combined diff, tests |
| One side's change dropped | No error from Git | `git diff HEAD^1 HEAD`, `git diff HEAD^2 HEAD` |
| Code no longer compiles | Merge succeeded textually | Build and tests |
| Unrelated file auto-merged badly | Git merged cleanly without a conflict | Tests, review of `git show --cc` |
The last row matters. A clean automatic merge can still be semantically wrong. If one branch renames a function and the other adds a call to the old name in a different file, Git sees no conflict, yet the program breaks. Tests are the only reliable defense against this class of error.
Exercise
You resolved a conflict in config.txt and staged it. Name one command that checks for leftover conflict markers across your changes, and one command that shows which paths are still unmerged.
Show answer
git diff --check (or git grep -n -E '^(<<<<<<<|=======|>>>>>>>)') detects leftover markers. git ls-files -u lists unmerged paths, and git status shows them under "Unmerged paths".
Exercise
After committing a merge, you want to see only the lines where you made a decision that differed from both parents. Which command shows this, and why is it limited to those lines?
Show answer
git show (or git show --cc) on the merge commit. It prints a combined diff, which hides hunks that match at least one parent and shows only hunks that differ from all parents.