Almost every beginner is afraid of the first merge conflict. It is not a disaster. It simply means: two people (or two branches) changed the same lines in different ways, and Git cannot know which version you want. It stops and asks you to decide. Once you have done it two or three times, it becomes a routine job. This lesson walks through one calmly.
The situation
A price file says Tea: 20. On the branch update-tea, someone changed it to 25. Meanwhile on main, someone changed it to 22. Both changes are on the same line, so Git cannot combine them automatically. Here is the real session.
$ git merge update-tea
Auto-merging price.txt
CONFLICT (content): Merge conflict in price.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status -s
UU price.txt
$ cat price.txt
<<<<<<< HEAD
Tea: 22
=======
Tea: 25
>>>>>>> update-tea
Coffee: 30
$ git add price.txt
$ git commit --no-edit
[main 1012b0b] Merge branch 'update-tea'
$ git log --oneline --graph
* 1012b0b Merge branch 'update-tea'
|\
| * 586290f Tea costs 25
* | 0c40438 Tea costs 22
|/
* c156a1b Add prices
Reading what Git tells you
CONFLICT (content): Merge conflict in price.txtnames the file with the problem. Other files merged fine.git status -sshowsUUfor the file: unmerged on both sides.- Git changed the file itself and added markers: the lines between
<<<<<<< HEADand=======are your current branch's version (main: 22). The lines between=======and>>>>>>> update-teaare the other branch's version (25). - The line
Coffee: 30was not marked, because both sides agree about it.
How to resolve it
- Open the file in your editor and find the markers.
- Decide what the final text should be. You can keep one side, the other, or write something new. We agreed on a price of 24.
- Delete all three marker lines (
<<<<<<<,=======,>>>>>>>) and leave only the final text. The file must look like normal text again. - Save, then
git add price.txtto tell Git the file is resolved. - Finish with
git commit --no-edit, which uses the default merge message. In the log graph, you can see the two lines joining at the merge commit.
Changed your mind? Abort
If you start a merge and feel lost, run git merge --abort. Git puts everything back as it was before the merge. There is no shame in a fresh start, and it is much better than guessing.
How to have fewer conflicts
- Pull the latest changes often, so your branch does not drift far from
main. - Keep branches short-lived. A branch that lives for weeks collects conflicts.
- Make small commits and small pull requests.
- Talk to your team: two people changing the same file at the same moment is a signal to coordinate.
- Do not reformat whole files, for example by changing indentation everywhere, because the diff touches every line and creates conflicts for others.
Conflicts are about people, not only code
The tool shows the clash, but the answer is often a decision. Which price is right? Which wording should stay? If you are not sure, ask the person who made the other change. Do not simply pick one side to make the conflict go away, because you could lose someone's work.
Your task
- Create a project with
price.txtcontainingTea: 20. Make two branches that change that line differently and commit on each. - Merge one into the other and see the conflict. Run
git statusand open the file. - Resolve it by writing a final value, remove the markers, add and commit.
- Repeat, but this time run
git merge --abortand check that everything returned to its state before the merge.
How to check it worked
- The final file has no
<<<<<<<,=======or>>>>>>>lines. git statussays the working tree is clean after the merge commit.- After
git merge --abort, the file contents are what they were before the merge.