git

🏠 Homepage 🏠 Syllabus of Day 2 ⬅️ Previous: 🔀 Merging the PR into Main

9. Resolving Merge Conflicts

What are Merge Conflicts?

A merge conflict occurs when Git cannot automatically combine changes from different branches because the same lines in a file have been changed in both branches. This usually happens during merging or rebasing.

When Do Merge Conflicts Happen?

⚠️ Merge conflicts can occur whether you merge changes using a GitHub Pull Request, git pull, or git fetch + git merge. The steps to resolve conflicts are the same in all cases.

How to Resolve Merge Conflicts

  1. Try to merge branches:
    git merge branch-name
    

    If there are conflicts, Git will pause the merge and mark the conflicted files.

  2. Identify conflicted files:
    git status
    

    Conflicted files will be listed as “both modified”.

  3. Open the conflicted files in your editor.
    • Git marks conflicts like this:
      <<<<<<< HEAD
      Your changes
      =======
      Incoming changes
      >>>>>>> branch-name
      
    • Edit the file to keep the correct changes and remove the conflict markers.
  4. Stage the resolved files:
    git add filename.txt
    
  5. Complete the merge:
    git commit
    

    (If you were in the middle of a merge, this will finish it.)

Example Workflow

git checkout main
git pull origin main
git merge feature-branch
# Resolve conflicts in your editor
git add conflicted-file.txt
git commit

Tips for Avoiding Conflicts

Tools for Resolving Conflicts in VS Code


💡 Other Tips


Summary Table

Step Command/Action
Start merge git merge branch-name
Check conflicts git status
Edit files Resolve conflict markers in editor
Stage resolved files git add filename.txt
Complete merge git commit

Tip:
Take your time resolving conflicts and test your code after merging to ensure everything works

➡️Up Next: 📋Summary & Cheatsheet