Rebase – Keeping History Clean in Git
This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.
Table of Contents
10 sections
- 1. Interactive Git Rebase Explorer
- 2. How Git Rebase Works Under the Hood
- 3. Rebase in Action
- 4. Resolving Conflicts During a Rebase
- Useful Rebase Control Commands
- 5. Pushing Rebased Branches: Safe Force-Pushing
- Never Use Blind --force
- Always Use --force-with-lease
- 6. Decision Matrix: Merge vs Rebase vs Squash
- 7. The Golden Rule of Rebasing
Rebasing is the key to a clean and linear commit history.
Instead of merging (which introduces an extra merge commit and diamond-shaped history), rebasing applies your commits one by one on top of the new base.
When we have a feature branch and merge it with master, history can quickly become complicated with extra merge commits:

What we really want our commit timeline to look like is a single clean line:

That’s where Git rebase comes in!
1. Interactive Git Rebase Explorer
Step through the interactive timeline below to see how Git lifts feature commits, replays them on top of master’s tip, and fast-forwards master:
Git Rebase Lifecycle
Step 1 of 3: Divergent Feature and Master Branches
$ git log --graph --oneline Feature branched from C1 with commits F1 and F2. Meanwhile, master progressed to C2.
2. How Git Rebase Works Under the Hood
When you execute git rebase master while on feature1:
- Find Common Ancestor: Git determines the common base commit (merge-base) where
feature1originally diverged frommaster. - Generate Patches: Git saves each unique commit on
feature1as a temporary patch file in.git/rebase-apply/(or.git/rebase-merge/). - Reset Branch: Git resets the current branch (
feature1) to point directly to the tip ofmaster. - Replay Commits: Git applies each saved patch one by one onto the new base. Each replayed commit receives a new SHA hash because its parent commit has changed.
3. Rebase in Action
Suppose you have a feature1 branch based on an older master commit. Before merging, you want to update it with the latest master changes:
git checkout feature1
git rebase master # Rebase feature1 onto the latest master
After rebasing, this is how your codebase appears:

Now the commits form a single straight line, and you can fast-forward merge feature1 into master:
git checkout master
git merge feature1
git log --all --graph --oneline
Note: In your terminal output, you will see
Fast-forward. Because there are no diverging commits on master, the master branch pointer simply advances straight forward to HEAD.
4. Resolving Conflicts During a Rebase
If a replayed commit modifies code that was also changed in upstream master, Git temporarily pauses the rebase and informs you of the conflict:
# 1. Inspect the conflicted files
git status
# 2. Open the file, resolve conflict markers (<<<<<<<, =======, >>>>>>>), and save
# 3. Stage the resolved files (do NOT run git commit!)
git add <resolved-file>
# 4. Continue replaying remaining commits
git rebase --continue
Useful Rebase Control Commands
git rebase --continue: Resumes the rebase after resolving conflicts and staging files.git rebase --skip: Skips the current patch entirely (useful if the upstream branch already incorporated the exact change).git rebase --abort: Aborts the entire rebase process and restores your branch to its exact state before the rebase started.
5. Pushing Rebased Branches: Safe Force-Pushing
Because rebasing rewrites commit hashes, attempting a normal git push to your remote feature branch will be rejected with a [rejected - non-fast-forward] error.
Never Use Blind --force
# DANGEROUS: Overwrites remote branch blindly, destroying teammates' work!
git push --force origin feature1
Always Use --force-with-lease
# SAFE: Checks that no one else pushed new commits to the remote branch before overwriting
git push --force-with-lease origin feature1
--force-with-lease verifies that the remote ref matches what you last fetched. If a colleague pushed a new commit to your feature branch in the meantime, Git will refuse to overwrite it, protecting your team against accidental data loss.
6. Decision Matrix: Merge vs Rebase vs Squash
| Strategy | Commit History Topology | SHA Hashes | Best Used When… |
|---|---|---|---|
git merge | Preserves branching topology; adds an extra merge commit with two parents. | Preserves all original commit hashes untouched. | Merging major release branches or preserving the exact chronological sequence of a collaborative feature. |
git rebase | Linear, single-line commit history with zero clutter. | Rewrites commit hashes for all replayed commits. | Updating a local feature branch with the latest changes from main before opening a pull request. |
git squash | Compresses multiple small WIP commits into one single atomic commit. | Creates a brand new unified commit hash. | Preparing a clean, self-contained feature or bugfix commit for production review. |
7. The Golden Rule of Rebasing
Never rebase commits that have been pushed to a public, shared branch!
Rebasing creates brand new commits with new SHA hashes. If teammates have already branched off or pulled those original commits, rebasing will cause duplicate commits and painful merge conflicts across your team. Only rebase local, private branches before merging them upstream.