Why do conflicts happen and how do you resolve them?
Show answer — try answering out loud first
A conflict happens when two branches modify the same lines of a file (or one deletes a file the other edits) and Git can't decide which version to keep. You resolve it by opening the file, choosing which code stays between the <<<<<<<, =======, and >>>>>>> markers, deleting those markers, then running git add on the file and finishing the merge with git commit.
Git merges automatically when the changes are in different areas. But if two branches changed the same line in different ways, Git won't guess which one you want: it asks you to decide. That's a conflict. It's not an error, it's a pending decision.
Git marks the conflicting area inside the file like this:
<<<<<<< HEAD
código de tu rama actual
=======
código de la rama que estás mergeando
>>>>>>> feature
Your job: leave the file the way it should end up (one version, the other, or a mix), delete the three markers, save, git add, and commit.
# You try to merge and a conflict appears
git merge feature
# CONFLICT (content): Merge conflict in index.html
# Automatic merge failed; fix conflicts and then commit the result.
# See which files are in conflict
git status
# both modified: index.html
# You open index.html and you'll see something like this:
# <<<<<<< HEAD
# <h1>Bienvenido</h1>
# =======
# <h1>Hola</h1>
# >>>>>>> feature
# You edit to keep the correct version and delete the markers.
# Then you mark the file as resolved:
git add index.html
# You finish the merge with a commit
git commit
# (Git already provides a default merge message)
# If you change your mind and want to cancel the merge entirely:
git merge --abort
Leaving the <<<<<<<, =======, or >>>>>>> markers without deleting them and committing like that, which breaks the file. Another mistake is panicking and deleting everything; instead, it's better to calmly look at what each version contributes. And if you get stuck, you can always cancel with git merge --abort and return to the state before the merge.
"A conflict happens when two branches change the same lines of a file and Git can't decide which one to keep. It's not an error, it's a decision that's up to me. I run
git statusto see the files in conflict, open them, look for the<<<<<<<,=======, and>>>>>>>markers, keep the correct code, delete the markers, and then rungit addon the resolved file andgit committo close the merge. If I get tangled up, I cancel everything withgit merge --abort."
You resolved the conflict in index.html by editing the content, but you forgot to delete the ======= line. You run git add and git commit. What happens?
See answer
The merge completes, but the file ends up broken: the commit includes the ======= line (and probably <<<<<<< and >>>>>>>) as literal text inside the code. Git doesn't validate that you deleted the markers; it trusts that you left the file correct. You'd have to make another commit fixing the file. That's why you should always review carefully before running git add.