Merge conflicts
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
A merge conflict is Git asking you a question: two branches changed the same line, and only a person can decide what the final line should be. Git stops the merge, writes both versions into the file and waits for you.
Why a conflict happens
A merge conflict happens when the two branches of a merge changed the same lines of a file in different ways. Git cannot know which version is right, so it stops the merge and asks you to decide.
Priya raised the pen price to Rs 12 on pen-price. Meanwhile Rahul renamed pen to gel pen on main. Both changed line 2 of prices.txt, and now Priya merges pen-price into main.
Example 1 · The merge stops with a conflict
Runs in: Your terminal
PS C:\Users\Priya\price-list> git merge pen-pricePS C:\Users\Priya\price-list> git statusOutputAuto-merging prices.txt CONFLICT (content): Merge conflict in prices.txt Automatic merge failed; fix conflicts and then commit the result. 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: prices.txt no changes added to commit (use "git add" and/or "git commit -a")
What this code does
CONFLICT (content)names the file. No merge commit was made yet: you are now in the middle of a merge.git statuslistsprices.txtunderUnmerged pathsasboth modified, and shows both ways out: fix and commit, orgit merge --abort.
A conflict is not an error in Git or in your code. It is a normal part of teamwork, and nothing is lost: both versions are in the file.
Conflict markers
Conflict markers are the three lines Git writes into a conflicted file: <<<<<<< HEAD starts your branch's version, ======= separates the two versions, and >>>>>>> <branch> ends the version from the branch being merged in.
Open prices.txt in your editor, or print it in the terminal. Only the conflicting line is wrapped in markers.
Example 2 · The conflict inside prices.txt
Runs in: Your terminal
PS C:\Users\Priya\price-list> cat prices.txtNotebook: Rs 15 <<<<<<< HEAD Gel pen: Rs 10 ======= Pen: Rs 12 >>>>>>> pen-price
What this code does
- Between
<<<<<<< HEADand=======ismain's line, the branch you are on. VS Code calls it the Current Change. - Between
=======and>>>>>>> pen-priceis the line frompen-price, the Incoming Change. - In PowerShell,
catprints a file (it is a short name forGet-Content). The notebook line and the rest of the file merged without trouble.
The marker lines are not valid code. If you run or compile a file with one left in, it fails at the <<<<<<< line: Java reports error: illegal start of expression and Python reports SyntaxError: invalid syntax.
Resolve the conflict
Resolving means editing the block until it holds the final code, deleting all three marker lines and saving. Priya and Rahul agree on one line: Gel pen: Rs 12. Then git add tells Git the file is resolved, and a commit finishes the merge.
On Windows
Open the file from PowerShell in Notepad, or in VS Code with code prices.txt. In VS Code, the links Accept Current Change, Accept Incoming Change and Accept Both Changes appear above the block; pick one, then fix the line by hand if needed.
PS C:\Users\Priya\price-list> notepad prices.txt- Replace the whole block, markers included, with
Gel pen: Rs 12. - Save with Ctrl + S and close Notepad.
- Back in PowerShell, check, add and commit as in the example below.
Example 3 · Check, add and commit
Runs in: Your terminal
PS C:\Users\Priya\price-list> git diff --checkPS C:\Users\Priya\price-list> cat prices.txtPS C:\Users\Priya\price-list> git add prices.txtPS C:\Users\Priya\price-list> git commit --no-editPS C:\Users\Priya\price-list> git log --oneline --graphOutputNotebook: Rs 15 Gel pen: Rs 12 [main 5b4c1d9] Merge branch 'pen-price' * 5b4c1d9 (HEAD -> main) Merge branch 'pen-price' |\ | * 5c19044 (pen-price) Raise pen price to Rs 12 * | f2022a4 Rename pen to gel pen |/ * d504b31 Add README * 9d23448 Add price list
What this code does
git diff --checkprinted nothing, so no marker is left. Before the edit it printedprices.txt:2: leftover conflict markerand the same for lines 4 and 6.- The file shows the agreed line.
git addmarks the conflict as resolved. git commit --no-editfinishes the merge with the usual message. In the graph, a resolved conflict looks like any other merge commit.
Resolve in your IDE
Most editors and IDEs have a merge tool. It shows the two versions side by side and writes your choice into the file, so you do not edit markers by hand. Then commit as usual.
- VS Code: click Resolve in Merge Editor, choose Accept Current, Accept Incoming or both, check the Result pane, then Complete Merge and commit in Source Control.
- IntelliJ IDEA and PyCharm: Git → Resolve Conflicts…, then Accept Yours, Accept Theirs or Merge… to edit the result in the middle pane, then Apply and commit.
- Eclipse: a conflicting file has a red mark. Right-click → Team → Merge Tool, edit the left pane and save, then Team → Add to Index and commit in the Git Staging view.
HEAD all mean the branch you are on; "Theirs" and "Incoming" mean the branch being merged in.Cancel with git merge --abort
git merge --abort cancels the merge and puts every file back exactly as it was before git merge. Use it when the conflict is bigger than expected, or you want to ask a teammate first. You can merge again later.
On Windows
Back at the conflict, before any edit, in PowerShell:
PS C:\Users\Priya\price-list> git merge --abortPS C:\Users\Priya\price-list> git statusOutputOn branch main nothing to commit, working tree clean
prices.txthas no markers again, andmainis still onf2022a4, as if the merge never started.
main into your branch often, and do not reformat whole files on a feature branch.Next step
Check your understanding
10 quick questions