Sub topic 7
Resolving a conflict step by step
This chapter assumes you can commit, branch and switch branches, and read git status and git log output. You should also know what a merge base is and what conflict markers look like. The earlier chapters cover both. Here you will follow one conflict from the first error message to the finished merge commit, with the commands and the checks that keep a resolution safe.
The setup: a conflict you can reproduce
The script below builds a repository where two branches change the same line after their common commit.
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge featureThe last command stops with output like this:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.Git has not failed. It has paused halfway through a merge. Every change it could combine safely is already staged. Every change it could not combine is left in your working tree for you to decide.
The workflow at a glance
The cycle has the same shape every time, however large the conflict is.
Step 1: Find out what is unresolved
Start with the list of files that need you.
git status
git diff --name-only --diff-filter=Ugit status shows them under "Unmerged paths" with the label both modified. The git diff form prints only the file names, which is useful in a large merge where git status is long and you want to paste the list into a script or a checklist.
Changes that merged cleanly appear under "Changes to be committed". You do not need to touch them, but they are part of the final commit, so they are worth reviewing later.
Step 2: Edit the file to its final content
Open config.txt. Git has written both versions into it:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> featureThe part above ======= is your current branch (main, shown as HEAD). The part below is the branch you are merging in (feature).
Your job is to produce the text that should exist after the merge. That text can be:
- one side as it is,
- the other side as it is,
- a combination of both,
- something new that neither side wrote.
Suppose the team agrees on 45 seconds. Replace the whole block, including all three marker lines, with the final line:
timeout = 45Resolving a conflict is not the same as picking a side. If one branch renamed a variable and the other added a new use of the old name, the right answer changes both. Read what each commit was trying to do. git log --merge -p -- config.txt shows only the commits from either side that touch the conflicted file, which saves a lot of searching.
Files with several conflict blocks
A single file can hold many blocks. Fix every one of them. Editors can search for <<<<<<< to jump from block to block. Do not stop after the first block and assume the rest is fine.
Step 3: Tell Git the file is resolved
Git does not detect that you finished editing. You mark a file as resolved by staging it.
# edit config.txt to the final content, removing all marker lines
git add config.txt
git statusAfter git add, the file moves from "Unmerged paths" to "Changes to be committed". Repeat steps 2 and 3 for each unmerged file.
Git does not check what you stage. If you leave marker lines in the file and run git add, Git accepts it, and the markers are committed. The verification step below exists to catch that.
Step 4: Verify before you commit
Run these checks while the merge is still uncommitted, because fixing a mistake is cheapest now.
git diff --check
git grep -n -E '^(<<<<<<<|=======|>>>>>>>)'
git diff --cachedgit diff --checkreports leftover conflict markers and whitespace errors in the changes it compares.git grepwith the pattern above finds marker lines anywhere in the tracked files. The patterns are anchored to the start of a line, so ordinary text such as a comment divider of equals signs in the middle of a line does not match.git diff --cachedshows what you are about to commit. For a merge in progress,git diff --cachedcompares against the first parent, so you see everything the merge brings into your branch.
Then run the project's build and tests. Git only understands text. Two changes can merge without any conflict and still break the program, for example when one branch deletes a function and the other adds a call to it. That is called a semantic conflict, and only a build or a test run will reveal it.
Step 5: Commit
git commitGit opens your editor with a prepared message such as Merge branch 'feature'. The file list that Git adds as comments includes a "Conflicts:" section. You can keep it, since it tells future readers which files needed attention. If you want to explain a non-obvious decision, add a line or two to the message. Something like "Use 45 s timeout, a compromise agreed in review" will help whoever runs git blame later. To accept the default message, use git commit --no-edit.
Now check the result:
git log --oneline --graph -n 5
git show --ccThe graph shows a merge commit with two parents. git show --cc displays a combined diff that lists only the places where the result differs from every parent. That is the part of the merge you wrote yourself.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `error: Committing is not possible because you have unmerged files` | At least one file has not been staged | Run `git status`, edit and `git add` the remaining files |
| Marker lines appear in the committed file | The file was staged without removing the markers | Edit the file, then `git commit --amend` if the merge commit is not yet shared |
| The result compiles but behaves wrongly | You kept one side and dropped a needed change from the other | Compare with `git diff HEAD~1 HEAD` style checks and `git show --cc`, then fix in a follow-up commit |
| You edited the wrong file or made a mess | The resolution went in the wrong direction | `git checkout -m -- config.txt` restores the conflicted version with markers for that file |
The last fix is worth remembering. git checkout -m -- <file> recreates the original conflict in the working tree, so you can start that single file again without abandoning the whole merge.
If the merge is going badly
If you have lost track of what you changed, you can stop and restore the pre-merge state:
git merge --abortThis is safe at any point before you run git commit. Do it when the conflict turns out to be much larger than expected. A short call to the author of the other branch is often cheaper than guessing.
Exercise
Exercise
You run git merge feature and Git reports a conflict in config.txt and app.py. You fix config.txt, run git add config.txt, and then run git commit. What happens, and what should you have done before committing?
Show answer
The commit is refused with "Committing is not possible because you have unmerged files", because app.py is still unmerged. Before committing, you should have edited app.py to its final content and staged it with git add app.py. You can then verify with git diff --check and your tests, and run git commit.
Exercise
After staging all files, which command lists leftover conflict marker lines in tracked files, and why can a successful git add not be taken as proof that none remain?
Show answer
git diff --check (or git grep -n -E '^(<<<<<<<|=======|>>>>>>>)') finds them. git add only records the file's current content and marks the path as resolved. Git does not inspect the content for markers, so marker lines that you forgot to delete are staged like any other text.