Guides · git
How to fix a merge conflict in git
By Lalla Asmaa Alaoui, founder of Mel · Updated
Open each file git marks as conflicted, find the lines between <<<<<<< and >>>>>>>, keep the version you want, and delete the markers. Then run git add on the file and git commit to finish the merge.
A conflict happens when two branches change the same lines, or one branch edits a file the other deleted. Git cannot guess which change is right, so it stops and asks you. Nothing is lost while you decide. The steps below are the same on Mac, Linux and Windows.
Steps
- See which files are in conflict.
git statusConflicted files are listed under "Unmerged paths" as "both modified". For just the names:
git diff --name-only --diff-filter=U- Open a conflicted file and find the markers. Each conflict looks like this:
<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature-branchThe part above ======= is your current branch (HEAD). The part below is the branch you are merging in.
- Decide what the code should be. Keep one side, keep both, or write a new line that combines them. Then delete all three marker lines. The result might simply be:
const timeout = 60;Read the code around the conflict, not only the conflict itself. The right answer often depends on what each branch was trying to do.
- Search for leftover markers. One missed marker breaks the build.
git diff --checkThis warns about leftover conflict markers and whitespace errors. You can also search with grep -n "<<<<<<<" path/to/file on Mac and Linux, or findstr /n "<<<<<<<" path\to\file in Windows Command Prompt.
- Mark the file as resolved.
git add path/to/fileRepeat steps 2 to 5 for every conflicted file.
- Build and test. Run your project's tests before you commit. A merge can be free of conflict markers and still break, for example when one branch renamed a function the other branch calls.
- Finish the merge.
git commitGit fills in a merge message for you. Save and close the editor. On newer versions of git, git merge --continue does the same.
Shortcuts and escape hatches
- Take one side for a whole file. During a merge,
git checkout --ours path/to/filekeeps your branch's version andgit checkout --theirs path/to/filekeeps the incoming one. Thengit addthe file. During a rebase the meaning of ours and theirs is swapped, so check the result before you add it. - Start over.
git merge --abortputs everything back to how it was before the merge began. - Conflicts during a rebase. Resolve the same way, then run
git addandgit rebase --continueinstead ofgit commit. Usegit rebase --abortto give up. - Clearer conflict markers.
git config --global merge.conflictStyle zdiff3adds a third section showing the original lines, which makes many conflicts easier to read. It needs git 2.35 or newer; older versions can usediff3.
Avoid the next one
- Pull or rebase from
mainoften, so your branch never drifts far. - Keep branches small and short-lived.
- Agree on formatting tools, so whitespace changes do not collide.
Doing it with Mel
Every command above works at Mel's prompt. git status and git diff become blocks you can scroll and filter, and file paths in the output become links you can ctrl+click (cmd+click on macOS) to open.
A merge conflict is also a good job for an agent, because the fix needs reading, editing and testing. Press ctrl+enter (cmd+enter on macOS) and write something like: "Resolve the merge conflicts in this repo. For each one, explain what each side changed and which you kept, then run the tests." The agent can read both sides, propose edits and run the test command. With Mel's approvals, each edit and command shows on an approval card before it runs, and every applied edit shows as a diff card.
When the agent is done, open the code review panel with ctrl+shift+= (cmd+shift+= on macOS) and read the final result. Run git commit yourself when you agree with it.
FAQ
What do the <<<<<<<, ======= and >>>>>>> markers mean? They mark one conflict. Lines between <<<<<<< and ======= are your current branch. Lines between ======= and >>>>>>> are the incoming branch. Delete all three marker lines when you resolve it.
How do I cancel a merge with conflicts? Run git merge --abort. Your branch goes back to the state it was in before you started the merge.
How do I keep my version of a file in a conflict? During a merge, run git checkout --ours path/to/file, then git add path/to/file.
Why do I still see conflict errors after editing the file? Git waits until you git add each resolved file. Check git status for files still listed under "Unmerged paths".
Can an AI agent fix merge conflicts? It can propose a resolution and run your tests. You should still read the diff, because only you know which change the team intended.
- How to see what changed before you commit
- How to create and switch branches in git
- How to review an AI agent's code changes
- What is an agentic terminal?
Facts checked on Oct 25, 2026.
Git is a trademark of its owner. Mel is not affiliated with or endorsed by Git.
Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.
Follow on LinkedIn