Skip to main content

Command Palette

Search for a command to run...

πŸš€ Day 24 – Advanced Git: Merge, Rebase, Stash & Cherry Pick

Updated
β€’7 min read

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

2 views