Welcome back to CodeYourCraft! Today, we're going to dive into a crucial aspect of Git: fixing merge conflicts. 🎯
Merge conflicts arise when you and your team members are working on the same file and try to merge your changes. Git can't automatically decide which changes to keep, and it needs your help to resolve the issue. 💡
Merge conflicts occur due to differences in lines added, deleted, or modified by different team members in the same file. Let's take an example to understand this better.
Suppose you and your team member are working on a file index.html. If you both modify the same lines of code, Git will not be able to merge the changes automatically, and a merge conflict will occur.
When you try to merge a branch with the current one, Git will inform you if there are any merge conflicts.
$ git checkout my-branch
$ git merge master
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.To resolve the conflict, you need to manually edit the file and decide which changes you want to keep. You can do this by opening the conflicted file using the command:
$ git open index.htmlYou'll see something like this:
<<<<<<< HEAD
<!-- Original content from the current branch -->
<h1>Original Heading</h1>
======
<!-- Content from the branch you're merging in -->
<h1>New Heading</h1>
>>>>>>> my-branchChoose the content you want to keep and remove the conflicting sections (<<<<<<<, ======, >>>>>>>). Save the file and exit.
After resolving the conflicts, you need to stage and commit the changes:
$ git add index.html
$ git commit -m "Resolved merge conflict in index.html"Now, the merge conflict is resolved, and the branches are successfully merged.
What happens when you try to merge two branches with conflicting changes?
That's all for today! Stay tuned for more Git tutorials on CodeYourCraft. Happy coding! 📝