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.
| Command | What it does | Typical use |
|---|---|---|
git rebase main | Replays current branch commits onto main | Update a feature branch with upstream changes |
git rebase -i HEAD~5 | Opens an editable list of the last five commits | Clean up history before a pull request |
git rebase --onto new old | Moves commits after old onto new | Transplant part of a branch to another base |
git pull --rebase | Fetches and rebases instead of merging | Keep local commits on top of the remote |
git rebase --autosquash | Applies fixup and squash commits automatically | Fold review fixes into earlier commits |
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.
| Command | Effect on the commit | When to use it |
|---|---|---|
| pick | Keeps the commit unchanged | Default for commits that are already good |
| reword | Keeps changes, edits the message | Fix unclear or misspelled messages |
| edit | Stops so the commit can be amended | Split a commit or adjust its content |
| squash | Combines with the previous commit and merges messages | Join related small commits |
| fixup | Combines with the previous commit and drops its message | Absorb a correction quietly |
| drop | Removes the commit entirely | Discard an unwanted change |
| exec | Runs a shell command at that point | Run tests after each replayed commit |
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.
| Aspect | Git rebase | Git merge |
|---|---|---|
| History shape | Linear sequence of commits | Graph with merge commits |
| Commit identities | Rewritten with new hashes | Preserved unchanged |
| Conflict handling | Resolved per replayed commit | Resolved once in the merge commit |
| Safety on shared branches | Unsafe without coordination | Safe by default |
| Context of parallel work | Lost unless documented | Kept explicitly |
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 --continueresumes after the current conflict is resolved and staged.git rebase --skipdrops the commit that caused the conflict, which is appropriate only when its change is already present upstream.git rebase --abortrestores the branch to its exact state before the rebase, the safest exit when the situation becomes confusing.- Enabling rerere with
git config rerere.enabled truelets Git remember conflict resolutions and reapply them automatically.
The next table connects frequent problems with their causes and remedies.
| Symptom | Likely cause | Fix |
|---|---|---|
| Push rejected after rebase | Remote still has the old commits | Use git push --force-with-lease on a private branch |
| Collaborator sees duplicate commits | A shared branch was rebased | Coordinate and reset to the new remote branch |
| Work seems lost after rebase | Commits replaced by new hashes | Find the old tip in git reflog and reset to it |
| Repeated identical conflicts | Many commits edit the same lines | Squash first or enable rerere |
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.