Git rebase

On this page 7

Git rebase is the Git operation that moves or replays a sequence of commits onto a new base commit, creating new commits with the same changes but different parents. Among programming development tools, it is the main alternative to merging for integrating work from one branch into another, and it is also the standard way to rewrite local history before sharing it.

Where a merge preserves the exact shape of parallel work and records it with a merge commit, rebase produces a linear history that reads as if the work had been done sequentially. That clarity comes at a price: rebasing rewrites commit identities, which makes it safe on private branches and risky on branches that others already use.

How Git rebase replays commits

Running git rebase main on a feature branch makes Git find the merge base, the last commit the two branches share. Git then temporarily sets aside the feature commits made after that point, moves the branch pointer to the tip of main, and reapplies each set-aside commit as a new patch, one at a time and in order.

Because a commit hash depends on its content, its metadata and its parent commit, every replayed commit receives a new SHA-1 or SHA-256 identifier even when its changes are identical. The original commits are not deleted immediately; they remain reachable through the reflog for a period that defaults to 90 days for reachable entries and 30 days for unreachable ones, which makes most rebase mistakes recoverable.

The following table describes the main forms of the command.

CommandWhat it doesTypical use
git rebase mainReplays current branch commits onto mainUpdate a feature branch with upstream changes
git rebase -i HEAD~5Opens an editable list of the last five commitsClean up history before a pull request
git rebase --onto new oldMoves commits after old onto newTransplant part of a branch to another base
git pull --rebaseFetches and rebases instead of mergingKeep local commits on top of the remote
git rebase --autosquashApplies fixup and squash commits automaticallyFold review fixes into earlier commits
Common forms of the git rebase command

Interactive rebase and history editing

Interactive rebase, started with git rebase -i, opens a todo list of commits in the configured editor and lets the developer decide what happens to each commit before replay. It is the primary tool for turning a messy series of work-in-progress commits into a clean, reviewable sequence of logical changes.

CommandEffect on the commitWhen to use it
pickKeeps the commit unchangedDefault for commits that are already good
rewordKeeps changes, edits the messageFix unclear or misspelled messages
editStops so the commit can be amendedSplit a commit or adjust its content
squashCombines with the previous commit and merges messagesJoin related small commits
fixupCombines with the previous commit and drops its messageAbsorb a correction quietly
dropRemoves the commit entirelyDiscard an unwanted change
execRuns a shell command at that pointRun tests after each replayed commit
Interactive rebase commands

Reordering lines in the todo list reorders the commits. Combined with exec lines that run the test suite, interactive rebase can verify that every intermediate commit builds, which keeps tools such as git bisect reliable later.

Rebase compared with merge

Rebase and merge both integrate changes, but they record history differently. A merge adds one new commit with two parents and leaves existing commits untouched, while a rebase creates new copies of commits and discards the branching structure. Neither is universally better; teams choose based on how they value traceability versus readability.

AspectGit rebaseGit merge
History shapeLinear sequence of commitsGraph with merge commits
Commit identitiesRewritten with new hashesPreserved unchanged
Conflict handlingResolved per replayed commitResolved once in the merge commit
Safety on shared branchesUnsafe without coordinationSafe by default
Context of parallel workLost unless documentedKept explicitly
Git rebase and git merge side by side

Many hosting platforms, including GitHub and GitLab, offer rebase and merge or squash and merge buttons for pull requests, which apply these strategies on the server. A common team policy is to rebase privately, then integrate with a fast-forward or squash merge so that the main branch stays linear.

Handling conflicts during a rebase

A rebase stops whenever a replayed commit does not apply cleanly to the new base. Git marks the conflicting files, and the developer resolves them, stages the result with git add and continues with git rebase --continue. Because commits are replayed individually, the same conflict can appear several times if many commits touch the same lines.

  • git rebase --continue resumes after the current conflict is resolved and staged.
  • git rebase --skip drops the commit that caused the conflict, which is appropriate only when its change is already present upstream.
  • git rebase --abort restores the branch to its exact state before the rebase, the safest exit when the situation becomes confusing.
  • Enabling rerere with git config rerere.enabled true lets Git remember conflict resolutions and reapply them automatically.

The next table connects frequent problems with their causes and remedies.

SymptomLikely causeFix
Push rejected after rebaseRemote still has the old commitsUse git push --force-with-lease on a private branch
Collaborator sees duplicate commitsA shared branch was rebasedCoordinate and reset to the new remote branch
Work seems lost after rebaseCommits replaced by new hashesFind the old tip in git reflog and reset to it
Repeated identical conflictsMany commits edit the same linesSquash first or enable rerere
Rebase problems and fixes

The golden rule and common mistakes

The widely quoted golden rule of rebasing says: never rebase commits that exist outside your repository and that others may have based work on. Rewriting public history forces every collaborator to reconcile divergent copies of the same changes, which produces duplicate commits and confusing conflicts.

Other mistakes are subtler. Using plain git push --force instead of –force-with-lease can overwrite a colleague’s new commits on the remote. Rebasing a long-lived branch with hundreds of commits may require many conflict resolutions, where a single merge would be simpler. Finally, a linear history can hide when integration actually happened, which matters for audits and release forensics in regulated environments.

Understanding it simply

Git rebase moves your recent work so it starts from a newer point. Imagine writing chapters for a shared book while a colleague revises the opening. Rebase takes your chapters and places them after the updated opening, as if you had started later.

The result is a tidy, straight story of changes. The catch is that your chapters get new labels. That is fine for private drafts, but it confuses people who already copied the old version.

Applied in practice

A developer on a five-person web team spends three days on a feature branch and makes eleven commits, several of them named fix typo or try again. Meanwhile, the main branch receives twenty new commits from colleagues, including a refactor of the same configuration module.

Before opening a pull request, the developer runs git fetch and then git rebase -i origin/main. In the todo list, the small fixes are marked as fixup, two commits are reordered, and the series shrinks to four focused commits. One conflict appears in the configuration module; it is resolved once, rerere records it, and the rebase finishes.

The developer pushes with git push --force-with-lease to the private branch. Reviewers read four clear commits on top of the current main branch, CI runs against the latest code, and the team merges with a fast-forward, keeping the main history linear.