πΏ MERGING BRANCHES β EXPLAINED SLOWLY
π§ Key idea (concept first, commands later)
Imagine this:
mainbranch β stable version of the projectloginbranch β 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
- Git alerts you
- Edit the file
- Delete the markers
git addgit commit