Sub topic 6
Inspecting a Merge in Progress
This chapter assumes you can commit, branch and switch branches, and read git status and git log output. It also assumes you have seen the demo repository from the earlier chapters: two branches that each change timeout in config.txt. Here you will learn what Git records when a merge stops, and which commands read that record.
What "in progress" means
When git merge feature hits a conflict, Git does not create a merge commit. It stops halfway and leaves the repository in a special state:
- Files that merged cleanly are already staged.
- Conflicted files have markers written into the working tree.
- The index holds up to three versions of each conflicted file.
- Git records the commit being merged in
.git/MERGE_HEAD.
Re-create the conflict from the demo to follow along:
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 featureGit prints something like this:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.The repository now has three possible next steps: resolve and commit, abort, or keep inspecting. The diagram shows how these states connect.
Start with git status
git status is the first command to run. It lists every file in conflict and tells you how to proceed.
git statusOn branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: config.txt
no changes added to commit (use "git add" and/or "git commit -a")The label beside each path describes why it is unmerged:
| Label | Meaning |
|---|---|
| `both modified` | Both sides changed the file in ways that overlap |
| `both added` | Both sides created a file at the same path with different content |
| `deleted by us` | Your branch deleted the file, the other branch changed it |
| `deleted by them` | The other branch deleted the file, your branch changed it |
Files that merged cleanly appear under "Changes to be committed". Only the "Unmerged paths" section needs your attention.
To get only the names of the conflicted files, for example in a script, use:
git diff --name-only --diff-filter=URead the combined diff
Running git diff during a conflict does not show a normal two-column diff. It shows a combined diff that compares the working tree against both parents at once.
git diffdiff --cc config.txt
index 3b1c5d2,8a4f0e1..0000000
--- a/config.txt
+++ b/config.txt
@@@ -1 -1 +1,5 @@@
++<<<<<<< HEAD
+timeout = 30
++=======
+ timeout = 60
++>>>>>>> featureThe index hashes will differ on your machine. What matters is the two-character prefix on each line. The first column compares against the first parent (your branch), and the second compares against the second parent (the branch being merged).
++means the line is in neither parent. This applies to the marker lines Git inserted.+(space, plus) means the line is in the second parent but not the first.+(plus, space) means the line is in the first parent but not the second.
In the output above, timeout = 30 has the prefix +, so it appears in the working tree but not in the feature parent. Read the combined diff as a map of what each side contributed.
Once a file has no remaining conflicts, git diff stops listing it. A file that no longer appears in git diff is a rough sign that you resolved it, though you still must stage it.
To compare the working tree against one specific side, use these flags:
git diff --ours # working tree versus your branch
git diff --theirs # working tree versus the branch being merged
git diff --base # working tree versus the merge baseList the index stages with git ls-files -u
Git stores each side of a conflict in the index under a stage number. git ls-files -u lists these unmerged entries.
git ls-files -u100644 3b1c5d2e0f8a7c94a1d6e2b7f0c5a8d3e9b41f60 1 config.txt
100644 6f2a9c81b4d07e35c8a1f9d2e6b3047a5c8e1d92 2 config.txt
100644 8a4f0e17c3b95d2a6e0f48b1d7c9a3e5f2b60d84 3 config.txtEach line has the file mode, the blob hash, the stage number, and the path. The hashes are illustrative. The stages are:
| Stage | Contents | Name in commands |
|---|---|---|
| 1 | Version at the merge base (common ancestor) | `:1:` or `--base` |
| 2 | Version on your current branch | `:2:` or `--ours` |
| 3 | Version on the branch being merged | `:3:` or `--theirs` |
A normal, non-conflicted file has only stage 0. The number of stages tells you the kind of conflict. If stage 1 is missing, both sides added the file. If stage 2 or 3 is missing, one side deleted it.
Read each version directly
The index syntax :N:path lets you print any stage without touching the working tree.
git show :1:config.txt # base
git show :2:config.txt # ours
git show :3:config.txt # theirsFor the demo, these print timeout = 10, timeout = 30 and timeout = 60. Seeing all three values side by side often explains the conflict faster than the markers do. You can also extract a stage to a temporary file for a three-way comparison in your own tools:
git show :1:config.txt > /tmp/config.base
git show :3:config.txt > /tmp/config.theirsFind the commits behind the conflict
Knowing which commits touched the file helps you decide what the correct result should be. During a merge, MERGE_HEAD names the tip of the branch being merged, and HEAD is your tip.
git rev-parse HEAD MERGE_HEAD
git log --oneline --left-right HEAD...MERGE_HEAD
git log --merge --oneline -p- The
rev-parseline prints both commit hashes. - The
--left-rightlog marks commits that are only on your side with<and only on the other side with>. git log --mergeshows only commits from either side that touched conflicted files. Adding-pshows their patches, so you can read the intent behind each change.
Commit messages are often the quickest way to learn which value should win. A message such as "Raise timeout to 60 for slow CI" settles a conflict that markers alone cannot.
The files Git leaves in .git
A few files in .git show that a merge is active:
.git/MERGE_HEADholds the hash of the commit being merged. Its existence is howgit commitknows to create a merge commit with two parents..git/MERGE_MSGholds the prepared commit message, including a "Conflicts:" section listing the conflicted paths..git/MERGE_MODErecords options such as--no-fffor the final commit.
You rarely edit these by hand. Do not delete MERGE_HEAD to "cancel" a merge. Use git merge --abort instead, which restores the pre-merge state correctly.
A quick inspection routine
When a merge stops, run these in order:
git statusto see which files conflict and why.git diff --name-only --diff-filter=Uif you need a plain list.git ls-files -uto see how many stages each file has.git diffto see the combined view of the markers.git log --merge --onelineto read the commits behind the conflict.
After this you know what failed, which files are involved, and what each side intended. Only then edit the files.
Common mistakes
- Running
git diffand seeing nothing. After you stage a resolved file,git diffis empty for it. Usegit diff --cachedto see what will go into the merge commit. - Misreading
oursandtheirs. Duringgit merge, ours is your current branch. Duringgit rebasethe meanings swap, which a later chapter covers. - Assuming a clean
git statusmeans success. Files that merged cleanly can still be wrong. Inspect the staged result before committing. - Reading the combined diff as a normal diff. The two-column prefix is not the same as a single
+or-.
Exercise
In the demo repository, after git merge feature stops with a conflict, which command prints the version of config.txt from the branch you were merging in, without changing the working tree, and which stage number does it correspond to?
Show answer
git show :3:config.txt prints it. Stage 3 holds the version from the branch being merged (theirs). Stage 1 is the merge base and stage 2 is your current branch.
Exercise
git ls-files -u lists only stages 2 and 3 for a file, with no stage 1 entry. What does this tell you about the conflict?
Show answer
The file did not exist at the merge base, so both branches added it independently with different content. This is a "both added" conflict.