Git Merge and Conflict Resolution
# CHAPTER 5
Git Merge and Conflict Resolution
1. Introduction
Branching allows you to work in isolation. However, isolation is only temporary. Once a feature on yourfeature-login branch is complete and fully tested, you must integrate that code back into the official main branch so it can be deployed to the users. This process is called Merging. Most of the time, merging is automated and flawless. But occasionally, Git will encounter a situation it cannot resolve mathematically—a Merge Conflict. In this chapter, we will learn how to execute a successful merge and develop the critical skills required to safely untangle and resolve merge conflicts without losing data.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Understand the mechanics of merging one branch into another (
git merge).
- Perform a "Fast-Forward" merge.
- Identify the cause of a Merge Conflict.
-
Read and understand conflict markers (
<<<<<<<,=======,>>>>>>>).
- Safely resolve a merge conflict using a text editor.
3. Beginner Explanation
Imagine two editors working on the same newspaper article.- The Merge: The boss takes Editor A's paragraph and Editor B's paragraph, realizes they wrote about different topics, and simply glues them together into one perfect article.
- The Conflict: Editor A changed the title of the article to "The Sun." Editor B changed the exact same title to "The Moon." The boss tries to glue them together, but a computer cannot logically decide which title is correct.
- The computer throws its hands up, highlights the title, and says to a human: "Hey! You both changed the exact same sentence. Tell me which one to keep!" This is a Merge Conflict.
4. Executing a Merge
Merging takes the commits from a feature branch and applies them to your current branch. The Golden Rule of Merging: You must firstcheckout the branch you want to receive the new code (usually main).
If nobody else touched the main branch while you were working on the feature, Git performs a "Fast-Forward" merge. It simply moves the main pointer forward. It is seamless.
5. The Dreaded Merge Conflict
A conflict occurs when two branches modify the exact same line of the exact same file, and then attempt to merge.When you run git merge, Git will stop and print in red:
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
If you open index.html in your text editor (like VS Code), Git has injected strange symbols into your code:
6. Mini Project: Resolve Merge Conflict Safely
Let's manually resolve the conflict above.The Anatomy of the Markers:
-
<<<<<<< HEAD: Everything between this line and the equal signs is the code currently on yourmainbranch.
-
=======: The dividing line.
-
>>>>>>> feature-store-name: Everything between the dividing line and this marker is the incoming code from your feature branch.
Step-by-Step Resolution:
- 1. Open the file in your text editor.
- 2. You are the human. You must decide which code stays. Let's say we want the "Store" code.
-
3.
Delete the
<<<<<<<marker.
- 4. Delete the "Company" line.
-
5.
Delete the
=======marker.
-
6.
Delete the
>>>>>>>marker.
-
7.
Your code should now just look like clean, normal HTML:
<h1>Welcome to our Store</h1>.
- 8. Save the file.
- 9. Tell Git the conflict is fixed by adding and committing:
bash
git add index.html
git commit -m "Resolve merge conflict in header"
`
7. Real-World Scenarios
A team of five developers is working on a React application. Developer A is assigned to change the background color of the navigation bar to Blue. Developer B is assigned to change the font size of the navigation bar. Because they are both editing Navbar.css on the same lines of code, a merge conflict is inevitable. They don't panic. Developer A merges their branch into main first. When Developer B attempts to merge, they get a conflict. Developer B opens VS Code, reviews the conflict markers, keeps Developer A's Blue color, applies their own font size changes, and completes the merge, perfectly synthesizing both developers' work.
8. Best Practices
-
Communicate: If you get a massive merge conflict spanning 500 lines of code, do not try to guess which code to keep. You might accidentally delete your coworker's entire feature. Call your coworker on Slack, jump on a screen share, and resolve the conflict together.
-
VS Code Merge Tools: Modern text editors like VS Code have built-in "Merge Conflict" highlighting. They provide simple clickable buttons that say "Accept Current Change," "Accept Incoming Change," or "Accept Both Changes," making the manual deletion of the
<<<<<<< markers unnecessary.
9. Common Mistakes
-
Committing the Markers: Beginners often panic, forget to delete the
<<<<<<< HEAD symbols, and just type git commit. Now, your actual website code contains raw Git markers, which will crash your application and display garbage text to the end user. Always double-check that every single marker is deleted before committing.
10. Exercises
-
1.
What specific scenario causes Git to trigger a Merge Conflict instead of an automatic merge?
-
2.
If you want to merge
feature-cart into main, what is the very first Git command you must type?
11. FAQs
Q: I initiated a merge, got a terrifying conflict, and I just want to cancel the whole thing and go back. Can I?
A: Yes! If you are in the middle of a conflict and feel overwhelmed, simply type git merge --abort. Git will instantly cancel the merge and revert your files exactly back to how they were before you started.
12. Summary
In Chapter 5, we completed the lifecycle of a feature branch by integrating it back into the primary codebase. We learned the precise sequence of commands required to execute a git merge. More importantly, we demystified the intimidating concept of Merge Conflicts. By understanding how to read Git's conflict markers (HEAD, =======`), we empowered ourselves to act as the logical human arbiter, safely combining divergent code paths without data loss.