πŸ’©

programierds

Back to course
Lessons 3 / 3

Merging branches and conflicts

🌿 MERGING BRANCHES β€” EXPLAINED SLOWLY

🧠 Key idea (concept first, commands later)

Imagine this:

  • main branch β†’ stable version of the project
  • login branch β†’ you are building a new feature

πŸ‘‰ Merging means:

Bringing the changes from one branch into another

Usually:

  • You work on a secondary branch
  • You merge that branch into main

πŸ“Š Visual example (mental model)

main:   A --- B --- C
                 \
login:             D --- E

After the merge:

main:   A --- B --- C --- F
                 \     /
login:             D --- E

πŸ§ͺ REAL STEP BY STEP EXAMPLE (no conflicts)

Step 1️⃣ Create a project

git init

Create a file:

message.txt
Hello world
git add .
git commit -m "Initial message"

Step 2️⃣ Create a new branch

git checkout -b login

πŸ“Œ Now you are on the login branch.


Step 3️⃣ Change something in the new branch

Edit message.txt:

Hello world
Adding login screen

Save:

git add .
git commit -m "Add login"

Step 4️⃣ Go back to main

git checkout main

πŸ“Œ Heads up: the file returns to its original version.


Step 5️⃣ Merge the login branch

git merge login

πŸŽ‰ Merge successful, no conflicts


⚠️ NOW: CONFLICTS (the important part)

🧠 When do conflicts happen?

A conflict happens when:

  • Two branches
  • Change the same line
  • In different ways

Git doesn’t know which one to pick 🀯


πŸ’₯ REAL CONFLICT EXAMPLE

Step 1️⃣ Initial state

message.txt file on main:

Hello world

Step 2️⃣ The login branch changes the file

git checkout -b login
Hello world from login
git add .
git commit -m "Change text in login"

Step 3️⃣ Go back to main and change the same thing

git checkout main
Hello world from main
git add .
git commit -m "Change text in main"

Step 4️⃣ Try to merge (BOOM πŸ’₯)

git merge login

Git answers:

CONFLICT (content): Merge conflict in message.txt

πŸ” WHAT A CONFLICT LOOKS LIKE (ON THE INSIDE)

Open message.txt and you see:

<<<<<<< HEAD
Hello world from main
=======
Hello world from login
>>>>>>> login

What does this mean?

  • <<<<<<< HEAD β†’ what is in your current branch (main)
  • ======= β†’ separator
  • >>>>>>> login β†’ what comes from the other branch

πŸ› οΈ HOW TO RESOLVE THE CONFLICT (STEP BY STEP)

Step 1️⃣ Decide what stays

Option A: keep main:

Hello world from main

Option B: keep login:

Hello world from login

Option C: combine:

Hello world from main and login

πŸ‘‰ You decide.


Step 2️⃣ Delete the markers

⚠️ VERY IMPORTANT You must delete:

<<<<<<<
=======
>>>>>>>

Step 3️⃣ Save the file

The file should look clean:

Hello world from main and login

Step 4️⃣ Mark it as resolved

git add message.txt

Step 5️⃣ Create the resolution commit

git commit -m "Resolve conflict between main and login"

πŸŽ‰ Conflict resolved correctly.


πŸ”„ FULL merge-with-conflict flow

git merge login
# conflict
# edit file
git add .
git commit -m "Resolve conflict"

🧠 Professional tips

βœ”οΈ Run git pull before working βœ”οΈ Make small commits βœ”οΈ Use branches for EVERYTHING βœ”οΈ Read the conflict calmly βœ”οΈ Git NEVER deletes your work


❌ Common mistakes

❌ Deleting the whole file ❌ Committing without resolving ❌ Panicking and closing everything πŸ˜…


🧠 FINAL RECAP

Merge without conflict

git checkout main
git merge branch

Merge with conflict

  1. Git alerts you
  2. Edit the file
  3. Delete the markers
  4. git add
  5. git commit