Rebasing: Replaying Commits for a Linear History
What git rebase really does and when to use it: replaying commits onto another branch, resolving conflicts with continue, skip and abort, ours and theirs swapped, --onto, stacked branches with --update-refs, autostash, testing every commit, recovering from a bad rebase, and rebase versus merge.
kvi5hnuCode Sprout team member10 min readOn this page
Merging joins two lines of history with a merge commit. Rebasing takes a different approach: it moves your commits so they start from the tip of another branch, as if you had begun your work there. The result is a straight, linear history with no merge commit. It's one of Git's most useful commands, and one of the few that can cause trouble for others when used on shared commits. This article covers ordinary rebasing. The next one covers interactive rebase, which edits commits as it replays them.
What rebase does
Suppose lake was created from main at commit B, and main has since gained commit E. On lake, git rebase main:
- finds the commits on
lakethat aren't onmain:CandD, - switches to
main's tip,E, in detached HEAD, - replays each commit's changes on top, one by one, creating new commits
C'andD', - moves
laketo the last new commit.

The new commits make the same changes, with the same messages and authors, but they have new IDs: their parent is different, and a commit's ID depends on its parent. The originals still exist for a while, and the reflog can find them.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats for hire." >> lake.txt; git commit -q -am "Mention boat hire"
git switch -q main
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
echo "=== before"
git log --oneline --graph --all
echo "lake's commits: $(git rev-parse --short lake~1) $(git rev-parse --short lake)"
git switch -q lake
git rebase -q main
echo "=== after git rebase main"
git log --oneline --graph --all
echo "lake's commits: $(git rev-parse --short lake~1) $(git rev-parse --short lake)"
=== before
* 659d6e9 Mention boat hire
* d2a3ed1 Add lake page
| * 60dd215 Add stylesheet
|/
* 7fb62a0 Start project
lake's commits: d2a3ed1 659d6e9
=== after git rebase main
* f77b5be Mention boat hire
* df67ff8 Add lake page
* 60dd215 Add stylesheet
* 7fb62a0 Start project
lake's commits: df67ff8 f77b5be
The history is now a single line, and lake's two commits have new IDs. Merging lake into main now is a fast-forward. -q hides the Rebasing (1/2) progress lines. git rebase main lake does the same from any branch: it switches to lake first.
Resolving conflicts during a rebase
Each replayed commit can conflict, just like a merge. Git stops at that commit and tells you what to do:
git init -q trail-guide && cd trail-guide
echo "Ridge loop: 8 km" > trails.txt; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge loop: 8.5 km" > trails.txt; git commit -q -am "Correct ridge distance"
git switch -q main
echo "Ridge loop: 9 km" > trails.txt; git commit -q -am "Measure ridge again"
git switch -q lake
git rebase -q main
echo "=== status"
git status | head -9
echo "=== resolve, add, continue"
echo "Ridge loop: 8.5 km" > trails.txt
git add trails.txt
git rebase --continue
git log --oneline
Auto-merging trails.txt
CONFLICT (content): Merge conflict in trails.txt
error: could not apply 57e378a... Correct ridge distance
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply 57e378a... # Correct ridge distance
=== status
interactive rebase in progress; onto 3b63fba
Last commands done (2 commands done):
pick b85ea80 # Add lake page
pick 57e378a # Correct ridge distance
No commands remaining.
You are currently rebasing branch 'lake' on '3b63fba'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
=== resolve, add, continue
[detached HEAD c1b8fad] Correct ridge distance
1 file changed, 1 insertion(+), 1 deletion(-)
c1b8fad Correct ridge distance
bcca448 Add lake page
3b63fba Measure ridge again
8a049ba Start project
The first commit, "Add lake page", replayed cleanly. The second conflicted, and Git paused with HEAD detached at the partly rebased branch. The options at that point:
| Command | Effect |
|---|---|
resolve, git add, git rebase --continue | finish this commit and carry on with the next |
git rebase --skip | drop this commit entirely and carry on. Use it when the commit is no longer needed, for example because main already contains the same fix |
git rebase --abort | stop and put the branch back exactly as it was before the rebase |
Resolving works as in article 13. A rebase may stop several times, once per conflicting commit, and the same conflict can come back in later commits that touch the same lines. Enabling rerere (article 13) helps with that.
Ours and theirs are swapped
During a rebase, HEAD is the new base, main plus the commits replayed so far, and the commit being applied is yours. So in conflict markers, in git checkout --ours/--theirs, and in merge tools:
- "ours" (
HEAD,LOCAL) is the branch you're rebasing onto, heremain. - "theirs" (
REMOTE) is your own commit being replayed.
git init -q trail-guide && cd trail-guide
echo "Ridge loop" > trails.txt; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Ridge loop: 9 km (lake branch)" > trails.txt; git commit -q -am "Distance from lake branch"
git switch -q main
echo "Ridge loop: 8 km (main)" > trails.txt; git commit -q -am "Distance from main"
git switch -q lake
git rebase -q main > /dev/null 2>&1
git checkout -q --ours trails.txt; echo "--ours: $(cat trails.txt)"
git checkout -q --theirs trails.txt; echo "--theirs: $(cat trails.txt)"
git rebase --abort
--ours: Ridge loop: 8 km (main)
--theirs: Ridge loop: 9 km (lake branch)
Don't rebase commits others have
Rebasing replaces commits with new ones. If you've pushed lake and a teammate has based work on it, rebasing lake leaves them with the old commits while the remote has new ones. Their next pull tries to combine both, duplicating every commit, and a force push was needed to publish the rebase in the first place.
The usual rule, sometimes called the golden rule of rebasing:
- Rebase freely while commits are only yours: unpushed, or on a branch nobody else uses, such as your own pull request branch.
- Don't rebase commits on shared branches, such as
main, or a feature branch several people push to, unless everyone agrees.
Rebasing your own pull request branch onto main is normal, even after pushing it. You then push with git push --force-with-lease, which refuses if someone else pushed to the branch since you last fetched (article 20).
Rebase or merge?
Both combine work. They produce different histories:
| Merge | Rebase | |
|---|---|---|
| History | true, with branches and merge commits | linear, as if work happened one after another |
| Existing commits | unchanged | replaced by new commits |
| Conflicts | resolved once, in the merge commit | resolved per commit being replayed |
| Safe on shared branches | yes | only for your own commits |
| Finding when a feature arrived | the merge commit marks it | nothing marks where the feature begins or ends, unless merged with --no-ff or squashed |
A common combination: rebase your feature branch onto main to keep it up to date and clean, then merge it into main, with or without a merge commit. git pull --rebase does the same when updating your branch from the remote, replaying your local commits on top of what others pushed, instead of creating "Merge branch 'main' of ..." commits (article 19).
Choosing the new base: --onto
git rebase main replays the commits on your branch that aren't in main, onto main. --onto separates the two decisions: which commits to take, and where to put them:
git rebase --onto <new-base> <old-base> [<branch>]
It replays the commits after <old-base> up to <branch> (the current branch by default) onto <new-base>. Two common uses:
- A branch started from the wrong place. You created
fixfromfeatureinstead ofmain.git rebase --onto main feature fixmoves onlyfix's own commits ontomain, leavingfeature's behind. - Dropping commits from the middle.
git rebase --onto HEAD~3 HEAD~1replays only the last commit ontoHEAD~3, removing the two before it.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c feature
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page (unfinished)"
git switch -q -c fix # oops: created from feature, not main
sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix title capitalization"
git rebase -q --onto main feature fix
git log --graph --all --format='%h %s%d' --decorate
* 5dbbb29 Add lake page (unfinished) (feature)
| * 9778704 Fix title capitalization (HEAD -> fix)
|/
* 7fb62a0 Start project (main)
fix now has only its own commit, on top of main, and can be merged without bringing the unfinished lake page along.
Stacked branches: --update-refs
Sometimes branches build on each other: part1, then part2 started from part1's tip, perhaps reviewed as separate pull requests. Rebasing part2 onto main moves part1's commits too, as copies, but leaves the part1 branch on the old commits. --update-refs (Git 2.38) moves every branch that points into the rebased commits:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c part1
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c part2
echo "Boats" >> lake.txt; git commit -q -am "Mention boat hire"
git switch -q main
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
git switch -q part2
git rebase -q --update-refs main
git log --graph --all --format='%h %s%d' --decorate
* 95f732c Mention boat hire (HEAD -> part2)
* 0bfdc24 Add lake page (part1)
* 60dd215 Add stylesheet (main)
* 7fb62a0 Start project
Both part1 and part2 moved onto the new main. Set rebase.updateRefs=true to make this the default.
Rebasing with uncommitted changes: autostash
Git refuses to start a rebase with uncommitted changes to tracked files. --autostash stashes them first and reapplies them when the rebase finishes, or when it's aborted:
git rebase --autostash main
git config --global rebase.autoStash true # always
If reapplying the stash conflicts with the rebased commits, Git keeps the stash so you don't lose it, and tells you. git pull --rebase --autostash works the same way.
Testing every commit: --exec
git rebase --exec "<command>" main, or -x, runs a command after replaying each commit, and stops if it fails, so you can fix that commit before carrying on. It's how you check that every commit in a branch builds and passes tests before asking for review:
git rebase -x "npm test" main
To run it without moving the branch, rebase onto the commit the branch already starts from: git rebase -x "make test" --keep-base main. Without a real change of base, Git replays the commits in place.
Recovering from a bad rebase
A rebase you regret is easy to undo, even after it finished:
- During the rebase:
git rebase --abort. - Right after:
git reset --hard ORIG_HEAD. Git setsORIG_HEADto the branch's old tip when a rebase starts. - Later:
git refloglists every position ofHEAD, including therebase (start)entry and the commit before it.git reset --hard <that commit>restores the branch, as doesgit reset --hard lake@{1}, the branch's previous position (article 26).
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q main
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
git switch -q lake
echo "lake before: $(git rev-parse --short HEAD)"
git rebase -q main
echo "lake after: $(git rev-parse --short HEAD)"
git reset -q --hard ORIG_HEAD
echo "lake undone: $(git rev-parse --short HEAD)"
lake before: 340af72
lake after: 0bfdc24
lake undone: 340af72
Merge commits in a rebase
By default, rebase drops merge commits and replays the individual commits from all merged branches as one straight line. Usually that's fine for a feature branch. To keep the branch structure, including merges, use --rebase-merges, which recreates the merges on top of the new base. Conflicts resolved in the original merges may have to be resolved again.
What's current
- Rebase uses the merge machinery (
ort) by default, so it handles renames well. The older--applybackend is still available but rarely needed. --update-refs(Git 2.38) andrebase.updateRefshandle stacked branches.rebase.autoStash,pull.rebaseandrerere.enabledare the three settings that make day-to-day rebasing smoother.- Git 2.54 to 2.56 added an experimental
git historycommand, withreword,split,fixupanddropsubcommands, for editing individual commits without a full interactive rebase (article 15).
Common questions
Is rebasing dangerous?
Not for your own unpushed or unshared commits: nothing is lost, and ORIG_HEAD and the reflog undo any rebase. It causes trouble when other people have the old commits, because their history and yours then disagree.
Why does Git say "interactive rebase in progress" when I didn't use -i?
Plain rebases now run on the same machinery as interactive ones, so status messages use the same wording. Nothing different happens.
Why did the same conflict appear several times? Each replayed commit is applied separately. If three commits each touched the conflicting lines, you resolve three times. Squash first with interactive rebase (article 15), enable rerere, or merge instead.
After rebasing, git push is rejected. Why?
The remote branch has the old commits, and yours replaced them, so a normal push would lose them. If the branch is yours alone, use git push --force-with-lease (article 20).
Should I use git pull --rebase?
For updating your local branch with others' work, it's usually the better choice: no merge commits for routine updates. Many people set pull.rebase=true (article 19).
Summary
git rebase mainreplays the current branch's commits on top ofmain, creating new commits with new IDs, for a linear history.- On conflicts: resolve,
git add,git rebase --continue, or--skipthe commit, or--abortthe rebase. - During a rebase, "ours" is the branch you're rebasing onto and "theirs" is your own commit.
- Rebase your own commits freely. Don't rebase commits others have based work on.
--onto <new> <old>chooses which commits move and where, and--update-refsmoves stacked branches together.--autostashhandles uncommitted changes, and-xruns a command after every commit.git reset --hard ORIG_HEAD, or the reflog, undoes a rebase.
Practice questions
You're on feature, which has three commits that main doesn't. main has two new commits. After git rebase main, how many commits does feature have that main doesn't, and are their IDs the same as before?
Answer
Still three, and their IDs are new. Rebase replays the three commits on top of main's tip, creating new commits with the same changes and messages but different parents, and so different IDs. main itself is unchanged.
A rebase stopped on a commit whose only change is a fix that main already contains in exactly the same form. What do you do?
Answer
Usually Git notices identical changes and drops the commit automatically. If it stops with a conflict instead, because the fix was made slightly differently on main, and the commit adds nothing you need, run git rebase --skip to drop it and continue. Check the result with git log and a test run afterwards.
You created hotfix from develop by mistake. It has one commit of its own, and it should be based on main. Which command fixes it?
Answer
git rebase --onto main develop hotfix
It takes the commits on hotfix after develop, which is just the hotfix commit, and replays them onto main. The develop commits that hotfix accidentally included are left behind.
Previous: Resolving Merge Conflicts: Conflict Markers, ours and theirs, Merge Tools and rerere
Next: Interactive Rebase: Squash, Fixup, Reword, Reorder, Edit and Autosquash

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.