Sub topic 6
Inspecting a Merge in Progress
This chapter assumes you can commit, switch branches, and read a basic git diff. It uses the config.txt conflict from the previous chapters. When git merge feature stops, Git has not finished. It has put the repository into a merge state, and you can examine that state with three commands before you change anything.
What a stopped merge leaves behind
When a conflict occurs, Git does three things:
- It merges every file it can and stages the result.
- It writes conflict markers into each file it could not merge.
- It records the unfinished merge, so the next
git commitcreates a merge commit.
Your repository is in a half-finished state, and these commands show you exactly where.
git status: which files need you
git statusLook for the section named Unmerged paths. Each file listed there still has a conflict. For the demo repository the output reads roughly:
On 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.txtThe label both modified means both branches changed the file. Other labels, such as deleted by us or added by them, point to delete and rename conflicts, which a later chapter covers. Files that merged cleanly appear under Changes to be committed.
git diff: what conflicts
git diffDuring a merge, git diff shows a combined diff. It lists only the regions that are still in conflict, so it is a quick way to see what is left to fix. The two leading columns show which side each line came from. A - in the first column means the line comes from the current branch, and a + in the second column means it comes from the merged branch.
Once you edit and stage a file, it drops out of this output. That makes git diff a progress check as well as a viewer.
git ls-files -u: the three versions
git ls-files -uThis lists the unmerged index entries. Normally the index holds one version of each file. For a conflicted file it holds three, numbered by stage:
| Stage | Contents |
|---|---|
| 1 | The base, the common ancestor version |
| 2 | Ours, the current branch |
| 3 | Theirs, the branch being merged |
Each line shows the file mode, the blob hash, the stage number, and the path. You can read any stage directly with git show:
git show :1:config.txt # base
git show :2:config.txt # ours
git show :3:config.txt # theirsThis is useful when the markers in the working file are hard to read, because you can compare all three clean versions side by side.
A typical inspection order
- Run
git statusto get the list of conflicted files. - Run
git diffto see the conflicting regions. - Run
git ls-files -uorgit show :N:filewhen you need the original versions.
Exercise
After a failed merge, git ls-files -u prints three lines for config.txt. What do the stage numbers 1, 2 and 3 mean, and which command prints the content of the branch you are merging in?
Show answer
Stage 1 is the common ancestor (base), stage 2 is ours (the current branch), and stage 3 is theirs (the branch being merged). git show :3:config.txt prints the merged-in branch's version.