TechnicalEasy Git

How do you resolve a merge conflict in Git?

Asked for Frontend Developer, Backend Developer, Full-Stack Developer, Mobile Developer

Why they ask this: Conflicts happen weekly in team projects; interviewers want to see you stay calm and methodical.

Answer

A conflict happens when two branches change the same lines of a file (or one deletes a file the other edits), so Git cannot combine them automatically.

  1. Run git status to see the conflicted files.
  2. Open each file and find the markers:
text
<<<<<<< HEAD
const API_URL = '/api/v1';
=======
const API_URL = '/api/v2';
>>>>>>> feature/new-api
  1. Edit the code to the correct final version and delete the markers. Talk to the other developer if you are unsure which change is right.
  2. Run the app and the tests.
  3. git add <file>, then git commit (for a merge) or git rebase --continue (for a rebase).

To give up and start again, use git merge --abort or git rebase --abort. VS Code's merge editor makes the "accept current / incoming / both" choice easier.

Can you explain this in your own words?

Back to question bank