Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git rebase explained: understand and use rebase

Rebase is one of the most powerful and most misunderstood Git commands. This page explains everything: how it works, the difference with merge, interactive rebase and best practices.

Key concept

What is git rebase?

git rebase takes the commits from your branch and replays them on top of another branch, as if you had started your work from the most recent version.

The result: a linear history, without the merge commits that clutter the log. It's like putting your changes "at the end of the line" instead of creating a fork.

The basic syntax: git rebase main from your feature branch. Your commits are recreated with new identifiers, but the content remains the same.

Git rebase syntax

git rebase <target-branch>

Replay your commits on the target branch

git rebase -i HEAD~n

Interactive rebase on the last n commits

git rebase --abort

Abort an in-progress rebase

git rebase --continue

Continue after resolving a conflict

Git rebase in action

Two concrete examples: a simple rebase and an interactive rebase.

Simple rebase

Interactive rebase

Git merge vs git rebase: what is the difference?

git merge

  • Preserves complete history (who did what, when)
  • Safe on shared branches
  • Creates an explicit merge commit
  • Non-linear history, harder to read
  • Merge commits that "pollute" the log
Recommended for shared branches

git rebase

  • Linear, clean and readable history
  • No unnecessary merge commits
  • Ideal for cleaning up before a final merge
  • Rewrites history (new commit hashes)
  • Dangerous on branches already shared
Recommended for personal branches

Rebase best practices

1

Never rebase a public branch

If other people are working on the branch, use merge. Rebase rewrites history and creates impossible conflicts for others.

2

Rebase your feature branch before merging

Before merging your branch into main, run git rebase main to integrate the latest changes and get a clean history.

3

Use interactive rebase to clean up

Before sharing your work, squash small fix commits with squash and clarify messages with reword.

4

When in doubt, use merge

Merge is always safe. If you're not sure about the situation, merge is the safest choice. You can always adopt rebase later.

Part of the Git Branching Workflow guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git rebase

Practice rebase in a safe environment

The GitQuest simulator lets you test merge and rebase without risk. Ideal for understanding the difference.

Try it in the terminal