wasAsked.

How do you resolve merge conflicts in Git?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Git

Community answers

Posted by wasAsked editorial collection

A merge conflict happens when Git cannot automatically combine changes — typically when two branches changed the same lines of a file, or one branch modified a file another deleted. Conflicts can occur during merge, rebase, cherry-pick or stash pop. Step 1 — Identify the conflict git merge feature/payments # CONFLICT (content): Merge conflict in src/OrderService.cs git status # lists files under "Unmerged paths" Step 2 — Understand the conflict markers <<<<<<< HEAD var tax = amount * 0.18m; ======= var tax = TaxCalculator.Calculate(amount, region); >>>>>>> feature/payments - The section between <<<<<<< HEAD and ======= is your current branch's version. - The section between ======= and >>>>>>> is the incoming branch's version. (During a rebase the meaning flips: HEAD is the branch you are rebasing onto.) Step 3 — Resolve - Decide the correct final code: keep one side, combine both, or write something new. Understand why each change was made — talk to the other developer if necessary. - Remove all conflict markers. - Tools help: VS Code, Visual Studio and JetBrains IDEs offer three-way merge editors ("Accept Current", "Accept Incoming", "Accept Both"); git mergetool opens a configured tool. - For whole-file decisions: git checkout --ours path or git checkout --theirs path (then review carefully). - Setting merge.conflictStyle to zdiff3 shows the common ancestor too, which makes the intent of both changes clearer. Step 4 — Verify - Build the project and run tests; a conflict-free merge can still produce broken code if the combination is logically wrong. Step 5 — Mark as resolved and complete git add src/OrderService.cs git commit # for merges # or: git rebase --continue / git cherry-pick --continue To give up and return to the previous state: git merge --abort or git rebase --abort. Handling special cases - Modify/delete conflicts: decide whether the file should exist; git rm or git add accordingly. - Binary files (images, documents): choose one version; Git cannot merge their contents. - Lock files (package-lock.json, packages.lock.json): usually regenerate them rather than merging by hand. - Large or repeated conflicts during rebase: git rerere ("reuse recorded resolution") can remember how you resolved a conflict. How to reduce conflicts - Integrate often: pull or rebase from main frequently and keep pull requests small and short-lived. - Communicate when several people change the same area. - Consistent formatting (shared formatter settings, .editorconfig) to avoid whitespace-only conflicts. - Modular code with clear ownership reduces overlapping edits. Interview summary: "I check git status, open the conflicted files, understand both changes, combine them correctly and remove the markers, build and test, then git add and continue the merge or rebase. To prevent conflicts I keep branches short-lived and sync with main often."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics