Merge conflict
Мерж-конфликт
A merge conflict happens when Git can’t automatically combine changes. If two branches replace the same line with different text, for example, you need to tell Git what the final version should be.
What a merge conflict means
A conflict is a decision Git leaves to you. Git can usually combine edits that don’t overlap, even when they’re in the same file. But when two changes compete, it stops and asks you to resolve them. Despite the name, a conflict can also interrupt a rebase or a cherry-pick, when Git is applying changes from individual commits.
How Git decides what it can combine
When merging diverged branches, Git looks at three snapshots: the common base and the tip of each branch. The base tells Git what each side changed. If one branch edits an introduction and the other adds a paragraph at the end, Git can usually keep both changes. If one branch deletes the document while the other edits it, there’s a conflict even though there aren’t two competing versions of a line. Git needs to know whether the file should exist at all.
Reading and resolving a conflict
Suppose you’re on main and run git merge feature. Both branches changed a setting in the same place. With the default conflict marker style, the file might contain this block:
<<<<<<< HEAD
retries = 3
=======
retries = 5
>>>>>>> feature
In this merge, the top section comes from your current branch, main, and the bottom comes from feature. The markers show the alternatives, not which is correct. If the requirements call for five retries, replace the entire block with retries = 5. In other cases, the right result may combine parts from both sides or look different from either one.
- Run
git statusto see which paths still need attention. Not all conflicts produce markers in a text file. - Edit each file to reflect the intended behavior. Stage files you want to keep with
git add <file>or record a deletion withgit rm <file>. - Review the staged changes with
git diff --cachedand run tests that cover the behavior you changed. - Once all paths are resolved, run
git merge --continue. If the operation was a rebase, usegit rebase --continueinstead. A rebase may stop again when Git applies a later commit.
What a clean merge doesn’t tell you
Git checks whether it can combine the edits; it doesn’t know whether the result works. One branch could rename a configuration key while another adds a reference to the old key in a different file. The merge may be clean, but the application may no longer read the setting as intended. Similarly, accepting an entire side in a merge editor can discard unrelated edits. Review the result rather than treating the disappearance of conflict markers as proof that the work is done.
In an interview
You may be given a conflict block and asked what you would keep. Explain what each edit was trying to accomplish and what you’d check if the desired behavior isn’t obvious. A good answer covers both how you’d finish the Git operation and how you’d verify the resulting code.