Git Rebase
Rebasing moves a series of commits to a new starting point. Instead of joining two lines of history with a merge commit, Git replays your commits one by one on top of another branch, as if you had started your work from there. The result is a straight, linear history.
before: C --- D feature after: C' --- D' feature
/ /
A --- B --- E main A --- B --- E mainC' and D' contain the same changes as C and D but are new commits with new hashes, because their parent changed.
A basic rebase
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Initial version"
git switch -q -c feature/search
echo "search" > search.txt && git add . && git commit -q -m "Add search box"
echo "filters" >> search.txt && git commit -q -am "Add search filters"
git switch -q main
echo "fix" > fix.txt && git add . && git commit -q -m "Fix startup crash"
echo "=== before:"
git log --oneline --graph --all
git switch -q feature/search
git rebase -q main && echo "rebased feature/search onto main"
echo "=== after:"
git log --oneline --graph --all=== before:
* fdaaea5 Add search filters
* 86872c1 Add search box
| * 6b212ff Fix startup crash
|/
* 3e99e51 Initial version
rebased feature/search onto main
=== after:
* 113d723 Add search filters
* ac8564f Add search box
* 6b212ff Fix startup crash
* 3e99e51 Initial version
Now feature/search sits on top of main, so merging it into main would be a simple fast-forward.
Merge or rebase?
git merge main |
git rebase main |
|
|---|---|---|
| History | true record, with merge commits | linear, as if work happened in sequence |
| Existing commits | unchanged | replaced by new copies |
| Conflicts | resolved once, in the merge commit | possibly once per replayed commit |
| Safe on shared branches | yes | no |
Warning: The golden rule of rebasing: don't rebase commits that others already have. Rebasing replaces commits; anyone who built on the old ones ends up with duplicates and confusing conflicts. Rebase your own local or PR branches; merge shared ones.
Conflicts during a rebase
If a replayed commit conflicts, the rebase pauses on that commit. Fix the files, git add them, then git rebase --continue. git rebase --abort returns everything to how it was before:
mkdir app && cd app && git init -q
echo "title=Shop" > config.ini && git add . && git commit -q -m "Add config"
git switch -q -c rename
echo "title=Ada's Shop" > config.ini && git commit -q -am "Rename shop"
git switch -q main
echo "title=Tea Shop" > config.ini && git commit -q -am "Use tea shop title"
git switch -q rename
git rebase main 2>&1 | grep -E "CONFLICT|could not apply"
echo "Ada's Tea Shop" | sed 's/^/title=/' > config.ini
git add config.ini
GIT_EDITOR=true git rebase --continue
git log --oneline
cat config.iniCONFLICT (content): Merge conflict in config.ini
error: could not apply d755f66... Rename shop
[detached HEAD 354409a] Rename shop
1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/rename.
354409a Rename shop
23da3d0 Use tea shop title
4c53fd1 Add config
title=Ada's Tea Shop
GIT_EDITOR=true accepts the commit message unchanged (in your terminal, Git opens your editor so you can adjust it).
Interactive rebase: rewriting your own commits
git rebase -i <base> opens a todo list of the commits after <base> and lets you edit it before replaying:
pick 1a2b3c4 Add product page
pick 5d6e7f8 Fix typo
pick 9a8b7c6 WIP debugging
pick 4c3d2e1 Add cart button| Command | Effect |
|---|---|
pick |
keep the commit |
reword |
keep it, but edit the message |
edit |
stop after it so you can amend it or split it |
squash |
merge into the previous commit, combining messages |
fixup |
merge into the previous commit, discarding this message |
drop (or delete the line) |
remove the commit |
| reorder lines | reorder commits |
In your terminal you edit this list in your editor. Here a sed command plays the editor's role (via GIT_SEQUENCE_EDITOR): it folds the typo fix into the commit before it and drops the debugging commit:
mkdir app && cd app && git init -q
echo "shop" > README.md && git add . && git commit -q -m "Initial commit"
echo "<h1>Prodcut</h1>" > product.html && git add . && git commit -q -m "Add product page"
sed -i 's/Prodcut/Product/' product.html && git commit -q -am "Fix typo"
echo "console.log(1)" > debug.js && git add . && git commit -q -m "WIP debugging"
echo "<button>Add to cart</button>" >> product.html && git commit -q -am "Add cart button"
git log --oneline
GIT_SEQUENCE_EDITOR="sed -i -e 's/^pick \(.*\) Fix typo/fixup \1 Fix typo/' -e '/WIP debugging/s/^pick/drop/'" \
git rebase -q -i HEAD~4
echo "=== after:"
git log --oneline
lse831bbe Add cart button
4b1fb21 WIP debugging
58c922a Fix typo
bb5a09b Add product page
dd72b71 Initial commit
=== after:
2d790fe Add cart button
056ae33 Add product page
dd72b71 Initial commit
README.md
product.html
Four messy commits became two clean ones. This is how people tidy a branch before opening a pull request.
More rebase tools
| Command | Use |
|---|---|
git pull --rebase |
rebase your unpushed commits onto the fetched ones |
git rebase -i --autosquash |
apply fixup!/squash! commits automatically (see Amend) |
git rebase --onto main old-base feature |
move only the commits after old-base onto main |
git rebase -x "npm test" main |
run a command after each replayed commit; stop if it fails |
git rebase --update-refs |
also move other branches that pointed into the rebased commits |
Test yourself
What does git rebase main do when run on feature?
- Merges feature into main
- Replays feature's commits on top of main, creating new commits
- Deletes main
- Copies main into feature with a merge commit
Why shouldn't you rebase commits that are already on a shared branch?
- Rebase is slow
- It replaces those commits, breaking the history others have built on
- It deletes files
- GitHub doesn't support rebased commits
In an interactive rebase, which command combines a commit into the previous one and discards its message?
- pick
- squash
- fixup
- drop