Programming Intermediate 9 min

How to Resolve a Git Merge Conflict Without Losing Work

Merge conflicts happen when two branches change the same lines of the same file. Git cannot decide which version to keep, so it stops and asks you to choose. The markers it leaves in your files look alarming the first time, but the resolution process is mechanical once you understand the pattern.

Step-by-step

  1. 1

    Trigger the merge and read the error

    When Git detects a conflict during git merge or git pull, it stops mid-merge and prints the conflicting files. Nothing has been committed yet.

    bash
    git merge feature/user-auth
    # CONFLICT (content): Merge conflict in src/auth.js
    # Automatic merge failed; fix conflicts and then commit the result.
  2. 2

    Identify every conflicted file

    git status lists all conflicted files under Unmerged paths. These are the only files you need to edit — everything else has already been merged cleanly.

    bash
    git status
    # both modified:   src/auth.js
    # both modified:   config/app.js
  3. 3

    Read the conflict markers

    Open each conflicted file. Git wraps the two competing versions in markers: everything between <<<<<<< HEAD and ======= is what your current branch has; everything between ======= and >>>>>>> feature/user-auth is what the incoming branch has.

    bash
    <<<<<<< HEAD
    const timeout = 3000;
    =======
    const timeout = 5000;
    >>>>>>> feature/user-auth
  4. 4

    Edit the file to the correct final state

    Delete the conflict markers and all three delimiter lines. Write exactly what the file should contain — which may be one side, the other, or a combination of both. Save the file.

    bash
    // Resolved: keep the longer timeout from the feature branch
    const timeout = 5000;
  5. 5

    Mark each file resolved and commit

    After saving, stage the resolved file with git add. Once all conflicted files are staged, finalise the merge commit. Modern Git uses git merge --continue; older versions just use git commit.

    bash
    git add src/auth.js
    git add config/app.js
    
    # Modern Git (2.12+)
    git merge --continue
    
    # Or simply:
    git commit
  6. 6

    Use VS Code as a visual merge tool

    Visual tools eliminate the risk of accidentally leaving a marker in the file. Configure VS Code as your default mergetool, then let it open each conflict with Accept Current / Accept Incoming / Accept Both buttons.

    bash
    git config --global merge.tool vscode
    git config --global mergetool.vscode.cmd 'code --wait $MERGED'
    
    # Then during a conflict:
    git mergetool
  7. 7

    Abort the merge entirely if needed

    If the conflict is too complex to resolve right now, bail out completely. This resets both files and the index back to the state they were in before you ran git merge.

    bash
    git merge --abort
    # The repo is back to the pre-merge state
  8. 8

    Enable rerere to remember resolutions

    rerere (reuse recorded resolution) records how you resolved a conflict so Git can replay the same resolution automatically if the same conflict appears again — common on long-lived feature branches that are rebased repeatedly.

    bash
    git config --global rerere.enabled true
    # From now on, resolved conflicts are recorded in .git/rr-cache/
    # and replayed automatically on future identical conflicts.

Tips & gotchas

  • Pull from the base branch frequently (<code>git pull origin main</code>) to keep your feature branch close to main — smaller diffs mean fewer conflicts.
  • Keep branches short-lived. A branch open for two weeks is a conflict waiting to happen.
  • If a conflict spans hundreds of lines, <code>git merge --abort</code> and talk to the author of the conflicting change before proceeding.
  • Search for leftover markers before committing: <code>grep -r "&lt;&lt;&lt;&lt;&lt;&lt;&lt;" .</code>

Wrapping up

Merge conflicts are a normal part of collaborative development — not an emergency. The workflow is always the same: read git status, fix the markers, git add, git merge --continue. The best long-term strategy is prevention: short branches, frequent pulls, and rerere enabled so repeated conflicts resolve themselves.

#Git #Merge
Back to all guides

Need Help With Your Project?

Book a free 30-minute consultation to discuss your technical challenges and explore solutions together.