π Day 24 β Advanced Git: Merge, Rebase, Stash & Cherry Pick
Until now, I learned:
How to create branches
How to switch branches
How to commit
How to push to GitHub
But today I learned something very important:
π How branches come back together
π How to safely switch work
π How to move commits
π How to copy specific commits
These are real-world Git skills.
Letβs understand everything step by step in very easy language.
πΏ Task 1 β Git Merge (How Branches Join Together)
π§ What is Merge?
When we finish working on a feature branch,
we need to bring that feature into main.
That process is called:
MERGE
β Step 1: Create feature-login branch
git switch main
git switch -c feature-login
Explanation:
git switch mainβ Go to main branch-c feature-loginβ Create a new branch from current position
Now add some commits:
echo "Login Page" > login.txt
git add login.txt
git commit -m "Added login page"
echo "Login validation" >> login.txt
git add login.txt
git commit -m "Added login validation"
Now feature-login has 2 commits.
β Step 2: Merge into main
git switch main
git merge feature-login
π§ What happened?
If no new commit was added to main after creating the branch,
Git performs:
β‘ Fast-Forward Merge
What is Fast-Forward Merge?
It means:
Main simply moves forward to match feature branch.
Before:
A --- B (main)
\
C --- D (feature-login)
After merge:
A --- B --- C --- D (main)
No extra merge commit created.
Git just moved the pointer forward.
π Now Letβs See Merge Commit
β Create feature-signup
git switch -c feature-signup
Add commit:
echo "Signup Page" > signup.txt
git add signup.txt
git commit -m "Added signup page"
β Add commit to main before merging
git switch main
echo "Hotfix" > hotfix.txt
git add hotfix.txt
git commit -m "Hotfix commit"
Now main has a new commit.
β Now merge feature-signup
git merge feature-signup
π§ What happened?
Now Git creates:
π Merge Commit
Because:
Main moved forward
Feature branch also has commits
History is different
Git creates a new commit that joins both histories.
β What is a Merge Conflict?
A merge conflict happens when:
Both branches change the same line in the same file.
Example:
On main:
Hello World
On feature:
Hello DevOps
When merging, Git gets confused.
You will see:
<<<<<<< HEAD
Hello World
=======
Hello DevOps
>>>>>>> feature-branch
You must manually edit and decide which version to keep.
After fixing:
git add file.txt
git commit
Conflict resolved.
π Task 2 β Git Rebase (Clean History Method)
π§ What is Rebase?
Rebase means:
Move your branch on top of another branch.
Instead of combining histories like merge,
rebase rewrites history to make it straight.
β Create feature-dashboard
git switch main
git switch -c feature-dashboard
Add commits:
echo "Dashboard UI" > dashboard.txt
git add dashboard.txt
git commit -m "Added dashboard UI"
echo "Dashboard API" >> dashboard.txt
git add dashboard.txt
git commit -m "Added dashboard API"
β Move main forward
git switch main
echo "Security patch" > security.txt
git add security.txt
git commit -m "Security patch"
Now main moved ahead.
β Rebase feature-dashboard onto main
git switch feature-dashboard
git rebase main
π§ What does rebase do?
It takes your commits and:
Removes them temporarily
Moves branch to latest main
Reapplies your commits on top
Before:
A --- B --- E (main)
\
C --- D (feature-dashboard)
After rebase:
A --- B --- E --- C' --- D'
It looks clean and straight.
β Why should you NEVER rebase pushed commits?
Because:
Rebase rewrites history.
If someone already downloaded those commits,
you will break their repository.
Golden rule:
Never rebase shared commits.
π Rebase vs Merge
Use Merge:
In team projects
For shared branches
Use Rebase:
For personal branches
To clean history before pushing
π§Ή Task 3 β Squash Commit vs Regular Merge
β Create feature-profile
git switch -c feature-profile
Make small commits:
git commit -m "Added profile page"
git commit -m "Fixed typo"
git commit -m "Formatting fix"
git commit -m "Minor improvement"
β Squash merge
git switch main
git merge --squash feature-profile
git commit -m "Added profile feature"
π§ What happened?
Instead of 4 commits,
main gets only 1 commit.
That is squash merge.
π What is Squash Merge?
It combines all commits into one single commit.
π Regular Merge
git merge feature-settings
Regular merge keeps all commits separate.
π― When to use squash?
Use squash when:
Many small messy commits
Want clean main branch
Trade-off:
- You lose detailed commit history
π¦ Task 4 β Git Stash (Temporary Save)
π§ What is Stash?
Stash means:
Temporarily save unfinished work.
Imagine:
You are coding.
Suddenly urgent bug appears.
You must switch branch.
But you haven't committed yet.
Use stash.
β Make uncommitted changes
echo "Work in progress" >> login.txt
Try switching:
git switch main
Git may stop you.
β Save using stash
git stash
Now your changes are saved safely.
β Bring changes back
git stash pop
π Difference: pop vs apply
pop | apply |
|---|---|
Applies and deletes stash | Applies but keeps stash |
π When to use stash?
Urgent bug fix
Switching tasks temporarily
Not ready to commit
π Task 5 β Cherry Pick
π§ What is Cherry Pick?
Cherry pick means:
Copy one specific commit from another branch.
β Create feature-hotfix
git switch -c feature-hotfix
Make 3 commits.
Check commit hashes:
git log --oneline
Example:
a1b2c3 Fix typo
d4e5f6 Security fix
g7h8i9 UI update
β Cherry pick only second commit
git switch main
git cherry-pick d4e5f6
Now only that commit is added to main.
π When to use cherry-pick?
Urgent production fix
Apply one bug fix to another branch
Hotfix scenario
β What can go wrong?
Duplicate commits
Merge conflicts
Messy history