0%

Merging and Conflicts

Read a Merge Conflict

A is not a damaged . It is a merge paused at a decision Git cannot make honestly. The prepared repository is paused now: main added a solo tier and chat support, while simplify-tiers raised two prices on the same lines.

Ask Git where the merge stopped

You already use git status to find the current and outstanding file changes. During a merge, the same plain command also says that a merge is in progress and lists paths that are unmerged.

Run it now:

git status

Git names pricing.md under Unmerged paths and says that both branches modified it. The message also tells you the next two safe directions: fix the file and stage it, or abort the merge.

Read the marked alternatives

Open the conflicted file with the editor you learned in Chapter 2:

nano -w pricing.md

nano opens the file, and -w prevents the editor from inserting hard line breaks merely because the terminal is narrow. Do not save a change yet. Read the three marker lines Git added:

<<<<<<< HEAD
Solo: 12 per month
Standard: 20 per month
Team: 60 per month
Support: email, and chat on Team
=======
Standard: 25 per month
Team: 75 per month
Support: email only
>>>>>>> simplify-tiers

The line beginning with <<<<<<< starts the current branch’s version. HEAD means the branch you were on when the merge began, which is main here. The line containing ======= separates the alternatives. The line beginning with >>>>>>> ends the other branch’s version and names simplify-tiers.

The markers are not decorations. They are literal file contents, so the file is not resolved while any marker remains. Leave nano without saving: press Ctrl-X, then N if it asks whether to save.

The safe way back

Sometimes you start a merge and realize you need more information before deciding. git merge --abort cancels the merge attempt and tries to restore the pre-merge state. This prepared merge began with a clean working tree, so aborting restores that clean state. Starting a merge with uncommitted edits can make restoring those edits difficult, especially if you edit them further during the merge. That is why you check and settle your work before merging. The option --abort means stop this in-progress merge rather than create or combine anything.

Practice that recovery now:

git merge --abort
git status

The first command removes the in-progress merge state and conflict markers. The second confirms that main is clean again. Neither branch’s were deleted, and simplify-tiers is still sitting there waiting whenever you want to try again.

Leave the repository in this settled state. You have now seen one of the two exits from a paused merge, and you can reach the other one whenever you choose, because starting the merge again is the same git merge simplify-tiers you read in lesson 1.

In a conflicted file, what does the text between <<<<<<< HEAD and ======= represent?

What does git merge --abort do during a conflict?

You have now met a conflict without having to resolve one, which is the right order: the state is much less alarming once you know it has a door at both ends. Before you walk through the far door, the next lesson gives you the experience this chapter has so far only described, a merge that simply works.