🌿 MERGING BRANCHES — EXPLAINED SLOWLY
What is a Merge?
Merge means combining two branches into one.
Imagine you have:
- The
mainbranch (the stable version) - A
new-featurebranch (where you’re experimenting)
When you merge, Git takes the changes from new-feature and adds them to main.
How to Merge
git checkout main
git merge new-feature
- First, switch to the branch you want to merge INTO (
main) - Then, merge the other branch (
new-feature)
💥 CONFLICTS — WHAT ARE THEY?
A conflict happens when Git CAN’T automatically merge changes.
When does this happen?
When two people modified the same line of the same file in different ways.
Example Conflict
Imagine you and a teammate both edit index.html:
You:
<h1>Hello World</h1>
Your teammate:
<h1>Hello GitHub</h1>
Git doesn’t know which one to keep. Conflict!
🚨 HOW TO RESOLVE A CONFLICT
Step 1: Identify the conflict
When you try to merge:
git merge new-feature
Git tells you:
CONFLICT in index.html
Step 2: Open the file
You’ll see something like this:
<<<<<<< HEAD
<h1>Hello World</h1>
=======
<h1>Hello GitHub</h1>
>>>>>>> new-feature
<<<<<<< HEAD= your version (main)======== separator>>>>>>> new-feature= the other version
Step 3: Decide what to keep
You have 3 options:
- Keep your version
- Keep their version
- Keep both (create a new version)
For example, keep both:
<h1>Hello World - Hello GitHub</h1>
👉 Remove the <<<<<<<, =======, and >>>>>>> markers.
Step 4: Mark as resolved
git add index.html
git commit -m "Resolve merge conflict"
✅ Tips to Avoid Conflicts
- ✅ Pull before you start working
git pull
-
✅ Communicate with your team
- Who is working on what
- Avoid editing the same files
-
✅ Make small, frequent commits
-
✅ Use branches for each feature
📌 Summary
- Merge = combine branches
- Conflict = Git can’t decide
- Resolve = edit the file manually
- Always
git pullbefore starting
🧪 Practice
- Create two branches from
main - Modify the same line in both
- Try to merge → conflict!
- Resolve it manually
- Commit the resolution