Dealing with Merge Conflicts
Imagine you and a friend are writing a story together. You both decide to edit the same sentence, but you each write a different ending. When you try to put your changes together, you realize both endings cannot fit at the same time. This is what happens in Git when two branches change the same part of a fileβit's called a merge conflict.
# Here is what a merge conflict might look like in a file:
Once upon a time, there was a brave knight.
<<<<<<< HEAD
He fought dragons and saved the kingdom every day.
=======
She explored the enchanted forest and discovered hidden magic.
>>>>>>> feature/new-ending
The villagers cheered for their hero.
When you try to merge branches and Git finds that both have changed the same lines, it cannot decide which version to keep. Git marks the conflict in the file with special lines: <<<<<<<, =======, and >>>>>>>. The text between <<<<<<< HEAD and ======= is what is currently in your branch. The text between ======= and >>>>>>> feature/new-ending is what came from the branch you are merging in.
To fix a merge conflict, follow these steps:
- Open the file and look for the conflict markers:
<<<<<<<,=======, and>>>>>>>; - Decide which version you want to keep, or combine the changes in a way that makes sense;
- Delete the conflict markers and any unwanted text;
- Save the file;
- Stage the file with
git add; - Commit the resolution with
git commit.
Once everything looks good, you tell Git the conflict is resolved by staging and committing the file.
# After resolving the conflict, your file might look like this:
Once upon a time, there was a brave knight.
She explored the enchanted forest and discovered hidden magic.
The villagers cheered for their hero.
# Then you would run:
git add story.txt
git commit -m "Resolved merge conflict in story.txt"
1. What is a merge conflict?
2. How do you fix a merge conflict?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain what each conflict marker means in more detail?
What should I do if I want to keep parts from both versions?
Are there any tools that can help me resolve merge conflicts more easily?
Awesome!
Completion rate improved to 8.33
Dealing with Merge Conflicts
Swipe to show menu
Imagine you and a friend are writing a story together. You both decide to edit the same sentence, but you each write a different ending. When you try to put your changes together, you realize both endings cannot fit at the same time. This is what happens in Git when two branches change the same part of a fileβit's called a merge conflict.
# Here is what a merge conflict might look like in a file:
Once upon a time, there was a brave knight.
<<<<<<< HEAD
He fought dragons and saved the kingdom every day.
=======
She explored the enchanted forest and discovered hidden magic.
>>>>>>> feature/new-ending
The villagers cheered for their hero.
When you try to merge branches and Git finds that both have changed the same lines, it cannot decide which version to keep. Git marks the conflict in the file with special lines: <<<<<<<, =======, and >>>>>>>. The text between <<<<<<< HEAD and ======= is what is currently in your branch. The text between ======= and >>>>>>> feature/new-ending is what came from the branch you are merging in.
To fix a merge conflict, follow these steps:
- Open the file and look for the conflict markers:
<<<<<<<,=======, and>>>>>>>; - Decide which version you want to keep, or combine the changes in a way that makes sense;
- Delete the conflict markers and any unwanted text;
- Save the file;
- Stage the file with
git add; - Commit the resolution with
git commit.
Once everything looks good, you tell Git the conflict is resolved by staging and committing the file.
# After resolving the conflict, your file might look like this:
Once upon a time, there was a brave knight.
She explored the enchanted forest and discovered hidden magic.
The villagers cheered for their hero.
# Then you would run:
git add story.txt
git commit -m "Resolved merge conflict in story.txt"
1. What is a merge conflict?
2. How do you fix a merge conflict?
Thanks for your feedback!