How to resolve Git merge conflicts

· Category: Git

Short answer

When Git cannot automatically merge changes, it marks conflicts in the files. Edit the files to resolve them, then stage and commit.

Steps

  1. Identify conflicted files:
git status
  1. Open the file and look for conflict markers:
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch-name
  1. Edit to keep the desired code, removing markers.
  2. Stage and complete the merge:
git add .
git commit

Tips

  • Use a merge tool like git mergetool or VS Code's built-in resolver.
  • Communicate with teammates when resolving complex conflicts.
  • git diff during a merge shows only conflicted regions.

Common issues

  • Accidentally committing conflict markers will break your code.
  • Binary file conflicts require choosing one version manually.