Git Foundations / Branch and Merge

Resolve merge conflicts

Lesson 16 of 33. Open to read. No account needed.

Why Conflicts Happen

A merge combines what each branch changed since their merge base. When the two branches changed different parts of a file, or different files, Git combines them on its own. When both changed the same lines in different ways, Git cannot know which result you want. It stops, marks the disputed lines in the file and asks you to decide. That is a merge conflict.

A conflict is not an error or damage. Nothing is lost: both versions are in the file, and the merge simply waits for your decision.

What Git Shows You

Git reports the conflict as it happens:

Auto-merging uploads.txt
CONFLICT (content): Merge conflict in uploads.txt
Automatic merge failed; fix conflicts and then commit the result.

git status then says You have unmerged paths. and lists each conflicted file under "Unmerged paths" as both modified. While the merge is unfinished, Git will not let you switch branches.

Inside the file, the conflict looks like this:

The file uploads.txt during git merge add-gif. Lines above and below the conflict merged cleanly. A line of less-than signs followed by HEAD starts the conflict; below it is main's version of the allowed_types line. A line of equals signs divides it from add-gif's version. A line of greater-than signs followed by add-gif ends the conflict.

  • <<<<<<< HEAD starts the conflict. The lines after it are your current branch's version, here main's.
  • ======= divides the two versions.
  • The lines after it are the other branch's version, ending at >>>>>>> add-gif, which names the branch you are merging.
  • Everything outside the markers merged without trouble.

Resolving a Conflict

Resolving means editing the file so it says what the project should say, then telling Git you are done:

  1. Decide the content. Read both versions and work out the intent of each. The answer might be one side, the other, or a combination. Often the right result keeps both intentions.

  2. Edit the file. Write the result and delete all three marker lines.

  3. Check for leftover markers before you stage the file:

    git diff --check
    

    It prints leftover conflict marker with the line number for any marker still in the file, and nothing when the file is clean.

  4. Mark it resolved by staging it: git add <file>. When every conflicted file is staged, git status says All conflicts fixed but you are still merging. To check what you staged, add --staged:

    git diff --staged --check
    
  5. Finish the merge:

    git merge --continue
    

    Git opens your editor with the prepared message, Merge branch '<name>'. Save and close it, and the merge commit is made.

Git does not check that your result makes sense. It accepts whatever you stage, even a file that still contains markers, which is why the checks in steps 3 and 4 matter.

Backing Out

If you would rather not merge yet, for example because you need to ask the author of the other branch, abort:

git merge --abort

Your branch and files go back to exactly how they were before the merge. Start merges from a clean git status so that abort has nothing else to restore.

Exercise: Resolve a Conflicted Merge

Two branches changed the same settings line: add-gif allows GIF uploads, and main meanwhile allowed WebP. Both changes are wanted.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup:

    mkdir -p ~/git-practice/conflict-practice
    cd ~/git-practice/conflict-practice
    git init -b main
    cat > uploads.txt <<'EOF'
    # Upload settings
    
    max_size_mb = 10
    allowed_types = jpg, png
    keep_days = 30
    EOF
    git add uploads.txt
    git commit -m "Add upload settings"
    git switch -c add-gif
    cat > uploads.txt <<'EOF'
    # Upload settings
    
    max_size_mb = 10
    allowed_types = jpg, png, gif
    keep_days = 30
    EOF
    git commit -am "Allow GIF uploads"
    git switch main
    cat > uploads.txt <<'EOF'
    # Upload settings
    
    max_size_mb = 10
    allowed_types = jpg, png, webp
    keep_days = 30
    EOF
    git commit -am "Allow WebP uploads"
    
  2. Merge add-gif into main:

    git merge add-gif
    

    Git reports CONFLICT (content): Merge conflict in uploads.txt.

  3. Look at the state and the file:

    git status
    cat uploads.txt
    

    uploads.txt is listed as both modified, and the file contains the markers shown in the diagram.

  4. Practise backing out, then start the merge again:

    git merge --abort
    git status
    git merge add-gif
    

    After the abort, git status reports a clean working tree and the file is back to main's version. The second merge stops at the same conflict.

  5. Resolve it. Both changes are wanted, so the line should allow both new types. Write the combined file, without markers, then check:

    cat > uploads.txt <<'EOF'
    # Upload settings
    
    max_size_mb = 10
    allowed_types = jpg, png, webp, gif
    keep_days = 30
    EOF
    git diff --check
    

    git diff --check prints nothing. In your own projects you would make this edit in your editor; the command here writes the same result.

  6. Mark the file resolved and check what you staged:

    git add uploads.txt
    git status
    git diff --staged --check
    

    git status says All conflicts fixed but you are still merging., and the check prints nothing.

  7. Finish the merge. Your editor opens with Merge branch 'add-gif'; save and close it.

    git merge --continue
    git log --oneline --graph --all
    cat uploads.txt
    

    The graph shows the merge commit joining both branches, and the file allows jpg, png, webp, gif.

Success: git diff --check reported nothing, the merge commit exists with two parents, and uploads.txt contains both new file types and no markers.

If something differs:

What you seeLikely causeNext step
leftover conflict markerA marker line is still in the fileDelete it, then run the check again
fatal: cannot switch branch while mergingThe merge is unfinishedFinish it with git merge --continue, or git merge --abort
git status still lists both modifiedThe file was not staged after editinggit add <file>
An editor opened during git merge --continueGit is asking for the merge commit's messageSave and close to accept it
There is no merge in progressThe merge already finished or was abortedCheck git log --oneline --graph
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf conflict-practice
    ls
    

Independent task: before cleaning up, run this setup. It adds a README and an app name on main, then renames the app on one branch while main marks it as a beta, so the merge conflicts in two files:

cat > README.md <<'EOF'
# Photo Share

Share photos with your family.
EOF
cat >> uploads.txt <<'EOF'
app_name = Photo Share
EOF
git add README.md uploads.txt
git commit -m "Add README and app name"
git switch -c rename
cat > README.md <<'EOF'
# Snapshots

Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings

max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Snapshots
EOF
git commit -am "Rename the app to Snapshots"
git switch main
cat > README.md <<'EOF'
# Photo Share Beta

Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings

max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Photo Share Beta
EOF
git commit -am "Mark the app as beta"
git merge rename

Resolve both files so that both intentions survive, check for markers and finish the merge.

Check Your Understanding

  1. In a conflict, someone deletes both versions and the markers, leaving no allowed_types line at all, then stages and commits. Git accepts it. Why is that not a resolution?
  2. What does git merge --abort give you back, and when would you use it?

Answer and Reasoning

Independent task

git status lists both README.md and uploads.txt as both modified. One branch renamed the app and the other marked it as a beta, so the combined result is "Snapshots Beta" in both files:

cat > README.md <<'EOF'
# Snapshots Beta

Share photos with your family.
EOF
cat > uploads.txt <<'EOF'
# Upload settings

max_size_mb = 10
allowed_types = jpg, png, webp, gif
keep_days = 30
app_name = Snapshots Beta
EOF
git diff --check
git add README.md uploads.txt
git diff --staged --check
git merge --continue

Both checks print nothing, and the merge commit is Merge branch 'rename'. Keeping only one side in either file would also complete the merge, but it would silently drop the other branch's intention.

Understanding check

  1. Git only checks that the conflicted files were staged; it does not check what they say. Deleting both versions throws away both branches' changes, and here it removes a setting the application needs. A resolution must contain the content the project should have, which usually means keeping both intentions.
  2. It returns the branch and files to exactly their state before the merge started, with no conflict and no partial result. Use it when you cannot resolve the conflict yet, for example because you need to ask the other branch's author what they intended.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy