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.
- Run
git statusto see the conflicted files. - Open each file and find the markers:
text
<<<<<<< HEAD
const API_URL = '/api/v1';
=======
const API_URL = '/api/v2';
>>>>>>> feature/new-api- 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.
- Run the app and the tests.
git add <file>, thengit commit(for a merge) orgit 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?