git rebase Command
git rebase moves your branch’s commits to start from a different commit, creating new commits with new hashes but the same content. The result is a linear history — your work appears to have always been built on top of the latest base.
The simplest rebase
Move feature-x onto current main
# Make sure main is up to date first git switch main git pull # Rebase your branch onto main git switch feature-x git rebase main
Read this as: “take feature-x and replay its commits on top of main.”
What Git actually does
Finds the common ancestor of
feature-xandmain.Records the diffs of every commit on
feature-xthat is not inmain.Resets
feature-xto the tip ofmain.Re-applies each recorded diff as a NEW commit (new SHA, new parent, same content).
If a diff fails to apply cleanly → conflict; you fix and continue.
Before and after
Before
A───B───C───F ◀── main
\
D───E ◀── feature-xAfter 'git rebase main' (on feature-x)
A───B───C───F───D'───E' ◀── feature-x D and E are now D' and E' — same content, new SHAs, new parents. The old D and E are unreachable, kept in reflog for a while.
Aborting and continuing
# Resolved a conflict; continue replay git rebase --continue # Skip the current commit (use with care) git rebase --skip # Cancel everything; restore pre-rebase state git rebase --abort
Conflicts during rebase
Each replayed commit can conflict separately. The conflict-resolve cycle is identical to a normal merge, but notice that --ours and --theirs are swapped during a rebase:
--ours— the branch you are rebasing onto (here:main).--theirs— your branch (the commits being replayed).
--onto for precision
Move only the commits in a range
# Take everything on feature-x that's not on old-base # and replay it onto new-base. git rebase --onto new-base old-base feature-x
See the dedicated --onto page for the full picture.
Interactive rebase
git rebase -i main # Opens an editor letting you pick/reword/squash/drop each commit
Interactive rebase is the polish-your-history tool. See its own page.
Pulling with rebase
Avoid merge commits on pull
git pull --rebase # Equivalent to: git fetch + git rebase origin/<branch> # Configure as default git config --global pull.rebase true
Pushing after a rebase
Force-push the rewritten branch
# Safer: refuses if the remote moved unexpectedly git push --force-with-lease # Older / more dangerous git push --force
Useful options
--keep-empty— keep commits that became empty after rebasing (default is to drop them).--autosquash— used with-i, automatically positionsfixup!andsquash!commits.--autostash— stash uncommitted changes before rebasing, pop after.--exec "cmd"— run a command after each commit (useful to run tests on every replayed commit).-r/--rebase-merges— preserve the topology of merge commits during rebase.
Inspecting before rebasing
What will be replayed?
git log main..feature-x --oneline # Lists the commits that will be re-applied
git config --global rerere.enabled true. “Rerere” = reuse recorded resolution. The next time the same conflict appears (very common during long rebases), Git resolves it automatically.