How rebasing actually works, when to use it, and why it’s not the villain in your Git history

So you’ve mastered the holy trinity of git add
, git commit
, and git push
. You even know the dark magic of git reset --hard
, which you learned the hard way after erasing six hours of work because, “eh, staging looked weird.”
Congrats, you’re officially Git dangerous.
But let’s talk about the Git command that most devs either pretend to understand or actively avoid like it’s a merge conflict waiting to happen: git rebase
.
Even seasoned devs get twitchy around rebase. Maybe it’s the word “rewrite history.” Maybe it’s the fact that force-pushing after a rebase has the potential to destroy your whole team’s week. Or maybe it’s because nobody ever explained it in plain English without abstract diagrams and corporate slide decks.
Rebase is one of Git’s most powerful and misunderstood tools. It’s not a replacement for git merge
it’s a scalpel, not a hammer. When used correctly, it makes your commit history clean, elegant, and easy to follow. When used recklessly… well, let’s just say it’s a great way to get publicly roasted in a code review.
In this guide, we’re going to demystify rebase. We’ll walk through what it actually does (not just the theory), how it compares to merge, when it’s a good idea, and when it’ll absolutely blow things up. You’ll learn how to use it like a pro, with visual examples, jokes, one meme, and some hard-earned tips.
Let’s fix that Git imposter syndrome once and for all.
Table of contents
- Git merge vs rebase: What’s the actual difference?
- What does rebase actually do under the hood?
- When rebase is awesome (and safe)
- When rebase is dangerous (and chaotic)
- Conflict resolution during rebase
- Bonus: Interactive rebase tricks
- Two life-saving Git commands every dev should know
- Best practices for pull requests
- TLDR + conclusion + helpful resources
1. Git merge vs rebase: What’s the actual difference?
Let’s be real most devs first learn Git like this:
- You make a branch
- You do your thing
- You
git merge
it back - And now your
git log
looks like spaghetti.
Merge is the “safe” option. It takes two branches and smashes them together with a special merge commit in between, tying the histories together like a weird Git marriage ceremony:
“Do you, Feature Branch, take this Main Branch, to have and to hold, till conflicts do you part?”
Here’s the deal though git merge
preserves the exact history. Every commit remains intact. The downside? It gets noisy, especially if everyone’s merging every 3 minutes. Your Git history turns into an archaeological dig site.
1.1. Enter rebase
Rebase doesn’t merge. It replays. It takes your commits and sticks them on top of another branch as if you had pulled the latest changes before you started.
Instead of:
A---B---C (main)
<br> D---E---F (feature)
After git rebase main
:
mathematica
A---B---C---D'---E'---F' (feature)
Same code, different story. Those little apostrophes ('
) mean the commits have been rewritten. New hashes, new history. Your branch now pretends like it always started from the current main.
Think of it like this:
- Merge is additive keeps branches parallel, adds a connector.
- Rebase is reconstructive pretends your branch happened later, like some Git time travel.
Each has its place. Merge is safer on shared branches. Rebase gives you cleaner history great for prepping PRs.
2. What does rebase actually do under the hood?
Alright, buckle in. We’re going full Matrix for a second but I’ll keep it real.
When you run git rebase
, you’re not just “moving commits.” You’re rewriting history like a sneaky time traveler trying to pretend their feature branch was always up-to-date.
Let’s break it down into three steps that Git actually performs:
Step 1: Find the common ancestor
Git figures out where your feature branch forked from the branch you’re rebasing onto (e.g., main
). This point is called the merge base.
Let’s say:
main: A---B---G
feature: <br> D---E---F
The merge base is commit B the last shared commit before the two branches diverged.
Step 2: Detach and save your commits
Git takes your branch’s commits (D, E, F), and temporarily stashes them like patch files into memory.
It’s like grabbing your changes, taking a step back, and saying: “Hold up, let me re-introduce myself.”
Step 3: Reapply them on top of the new base
Now Git moves the branch pointer to the latest commit on main
in this case, Gand replays your commits on top of it, one at a time.
The result:
main: A---B---G
<br>feature: D'--E'--F'
Even though the code is the same, those '
versions are entirely new commits. Why? Because each now has a new parent commit (G), and Git generates a new hash based on that parent + content.
This is why rebase
is considered destructive you lose the original commit IDs.

3. When rebase is awesome (and safe)
Rebase gets a bad rap because, yeah, it can absolutely wreck things. But in the right context, it’s straight-up magic. Here’s when rebasing isn’t just safe it’s actually the smart move.
3.1. When you’re working on a local feature branch
Nobody else is using it. You’re in your own sandbox. Rebase all you want. Clean up your messy commit history like nothing ever happened.
git checkout feature-login
git rebase main
Boom your feature now looks like it branched off main yesterday, not three sprints ago when you still believed in code comments.
3.2. Right before opening a pull request
Want to make your PR reviewer’s life 10x easier? Rebase your branch onto the latest main so it looks fresh and conflict-free. You’ll avoid merge commits and make the commit history clean and readable.
3.3. Using git rebase -i
to squash or reorder commits
Rebase interactively (-i
) lets you:
- Squash 12 WIP commits into one clean commit
- Reword bad commit messages
- Drop useless commits (like “fix typo again”)
It’s the Git equivalent of linting your commit history.
We’ll dive deeper into this in Section 7.
Rebase responsibly and locally, and you’ll look like a Git wizard without summoning merge demons.
4. When rebase is dangerous (and chaotic)
So far, rebase sounds pretty clean, right? Polished history, no merge clutter, perfect for PRs…
But hold up, Git samurai before you start rebasing everything in sight, know this:
rebasing shared history can nuke your teammates’ work.
Why? Because rebase changes commit hashes. And Git sees different hashes as totally new commits even if the code is the same.
So if you’ve already pushed your branch, and someone else based their work on it, rebasing and force-pushing it will break their Git world.
They’ll try to pull, and Git will throw a tantrum like:
“Bro, these commits don’t exist anymore. What even is this history?!”
The golden rule of rebase:
❌ Do not rebase commits that have already been pushed to a shared/public branch.
✅ Only rebase your local, personal branches.
Unless:
- Everyone knows and agrees
- You really need to clean things up
- You force push with care (like
--force-with-lease
)
Wait, what’s --force-with-lease
?
It’s the slightly safer cousin of --force
.
It only pushes your rebase if nobody else has pushed anything new to the same branch.
Still risky. Still can ruin CI pipelines. But a bit less like dynamite.
git push --force-with-lease
5. Conflict resolution during rebase
Ah yes rebasing’s spicy side effect: merge conflicts.
Rebasing is basically Git saying:
“Hey, I’m going to replay your work on top of this new base. But… something changed. You fix it.”
Why do conflicts happen?
When Git replays your commits, if the file has been changed both in your branch and in the target branch (like main
), Git doesn't know which version is correct.
So it stops.
It throws conflict markers like:
<<<<<<< HEAD
main branch version
=======
your branch version
>>>>>>> feature-branch
Your job? Pick a winner. Or manually combine the changes.
The basic flow
git rebase main
# uh-oh, conflict
# fix the file(s)
git add <file>
git rebase --continue
Still messy? You can always nuke the rebase:
git rebase --abort
That takes you back to where you started, no harm done.
Pro tip: Use AI to help
If you’re using tools like GitKraken or GitHub Copilot, many offer AI conflict resolution suggestions.
Example: GitKraken will scan the diff and propose fixes with confidence scores.
You still review and decide but it saves brainpower during those “Friday 5pm deploy” moments.
Press enter or click to view image in full size
Bonus: Interactive rebase tricks
If git rebase
is the lightsaber, then git rebase -i
is when you unlock Jedi training mode.
This is where Git goes from “just works” to fine-tuned timeline editing.
6. What is interactive rebase?
Run this:
git rebase -i HEAD~4
You’ll get a retro-looking text editor showing your last 4 commits, like this:
pick a1b2c3d add login page
pick b2c3d4e fix typo
pick c3d4e5f wip - styling
pick d4e5f6g final changes
From here, you can:
-
pick
keep commit as is -
squash
merge it into the one above -
reword
change the commit message -
drop
remove the commit entirely -
edit
stop at this commit to make changes
Example:
reword a1b2c3d add login page
squash b2c3d4e fix typo
squash c3d4e5f wip - styling
pick d4e5f6g final changes
This turns three noisy WIP commits into a clean, single commit — like they always existed that way.
GUI options?
Yes please.
Tools like GitKraken let you interactively rebase with drag-and-drop, commit previews, and even auto-generated messages.
It’s like cleaning your room but with fewer existential questions.
7. Two life-saving Git commands every dev should know
We’ve all been there.
You’re halfway through a feature, knee-deep in broken code, and suddenly someone slacks you:
“Hotfix needed on
main
. Now.”
You’re not ready to commit. You don’t want to lose your work. You need a time machine.
7.1. git stash
: Pause button for your work
git stash
Boom. Your changes vanish from the working directory — but Git’s got them saved.
Need them back?
git stash pop
Want to keep the stash around even after applying?
git stash apply
Bonus:
git stash -u
Also stashes untracked files. Super useful when switching contexts.
Some Git GUIs (like GitKraken) even auto-generate stash descriptions using AI based on your diffs. So instead of “what was that stash again?”, you get:
“fix navbar responsiveness + remove debug logs”
7.2. git commit --amend
: Edit history, one commit at a time
Made a typo in your commit message? Forgot to add a file? No need to make a new commit.
git add missing.js
git commit --amend
Now your last commit is replaced as if nothing ever happened.
Just don’t amend commits you’ve already pushed unless you like chaos.
8. Best practices for pull requests
Once your branch is rebased, cleaned up, and pushed don’t just YOLO the PR.
A well-structured pull request = faster reviews, fewer back-and-forths, and less team suffering.
Here’s what seasoned devs actually look for:
8.1. Keep your PR focused
One PR = one logical change.
Bug fix, new feature, or refactor pick one.
Don’t mix a login page redesign with a database index change “just because they were in the same file.”
8.2. Use clear commit messages
A good commit message completes the sentence:
“If applied, this commit will…”
Examples:
- ✅
fix login redirect bug when session expires
- ✅
refactor cart service to batch DB calls
- ❌
stuff
- ❌
update things
Use conventional commits if your team supports it it’s helpful for changelogs and automation.
Tools like GitKraken now offer AI commit message generation based on your diff. It’s not perfect, but it gets you 80% of the way there and you can tweak the rest.
Quick PR checklist:
- Descriptive title + summary
- Tests included (if needed)
- One purpose, one goal
- Clean commit history (squashed, rebased)
TLDR + conclusion + helpful resources
You made it. You now know more about git rebase
than 90% of developers—and you didn’t even have to watch a two-hour YouTube video recorded in 480p by someone whispering.
TLDR: When to rebase vs merge
Press enter or click to view image in full size
Final thoughts
Rebasing isn’t scary it just suffers from bad tutorials, abstract metaphors, and traumatic memories of git push --force
.
Use it wisely, and you’ll write cleaner history, improve code reviews, and look like the dev who actually understands Git beyond the meme level.
Now go forth and squash your typos, reorder your commits, and push your branches responsibly.
Helpful resources
- Pro Git book (Chapter 3.6 Rebasing)
- Atlassian’s rebase guide
- GitKraken Git client
- Conventional commits
- Learn Git branching (interactive playground)

Top comments (0)