Home / Guides / Rebase
Git guide / 01

Rebase moves the base, not the work

A rebase replays commits from one branch on top of another commit. The changes can stay the same; the commits that carry them become new commits.

Suppose feature started at commit A. You made commits B and C while main moved on to D. Rebasing feature onto D asks Git to replay B and C there. The result is D → B′ → C′. The primes matter: B′ and C′ have new parentage and new commit IDs. The old commits are not edited in place.

Move a private branch onto the latest main

First inspect the working tree. Finish or set aside unrelated edits, then fetch so origin/main reflects the remote branch. Run the rebase while you are on the branch to be moved.

git status --short
git switch feature
git fetch origin
git rebase origin/main
git log --oneline --graph --decorate -8

The final log should show the new feature commits after the current origin/main. A rebase does not upload anything; pushing is a separate decision.

When Git stops at a conflict

Git pauses at the commit it cannot apply. git status names the files and the current step. Edit the conflict into the result you want, review the diff, stage the resolved files, and continue. Repeat if another commit conflicts.

git status
git diff
# edit the conflicted files
git add path/to/resolved-file
git rebase --continue

If the replay was a mistake, git rebase --abort returns the branch to its pre-rebase state. git rebase --skip drops the current commit from the replay; use it only when you have checked that its change is already present or truly unwanted.

Choose between rebase and merge

Rebase is useful for tidying a branch you control before review. A merge joins histories without replacing existing commits. If other people have based work on the published branch, coordinate before rebasing it: their copies still point to the old commits.

Already pushed? A rewritten branch may need a force push. Use git push --force-with-lease only when you understand who else uses that branch; the lease refuses to overwrite remote work you have not fetched. For a shared branch, a normal merge often avoids the rewrite altogether.

For a small cleanup before publication, git rebase -i HEAD~3 opens a list of the last three commits. You can reorder them or mark one as squash. Review the resulting history and run the relevant tests before publishing it.

If you need to locate the earlier branch tip after a completed rebase, inspect git reflog. It records recent local reference movements; it is a recovery aid, not a substitute for a backup or a shared history.

Next: find the commit that introduced a regression →