Rebase
Last updated
Ana's pancit-canton branch started from an older main. Merging would join them with a merge commit. Rebasing does something else: it replays her commits on top of the newest main, as if she'd started there. In this lesson you'll rebase a branch, handle a conflict partway through, and pull with --rebase.
By the end of this lesson you can
- Rebase a branch onto another with git rebase
- Explain that rebase makes new copies of your commits
- Settle a conflict during a rebase with --continue, or stop with --abort
- Use git pull --rebase to keep a straight line
Replaying commits on a new base
git rebase main, on a branch, means: take my commits since this branch split from main, and replay them one by one on top of main's newest commit.
Each replayed commit has the same message, author and change, but a new parent, so it's a new commit with a new hash (lesson 6.1). The branch label moves to the last copy. The originals are left behind for the reflog.
$ git log --oneline --graph --all -5 * cf91abf (main) Welcome corrections in the README | * cf121f8 (HEAD -> pancit-canton) Add shrimp to pancit canton | * a1f23d8 Add pancit canton recipe |/ * 67e435d Cover the leche flan with foil * ba04ca2 Add lumpiang shanghai recipe
cf91abfmain Welcome corrections in the READMEcf121f8HEAD →pancit-canton Add shrimp to pancit cantona1f23d8Add pancit canton recipe67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README
and 3 older commits
Rebase pancit-canton
Now:
cf91abfmain Welcome corrections in the READMEcf121f8HEAD →pancit-canton Add shrimp to pancit cantona1f23d8Add pancit canton recipe67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index page
and 7 older commits
You run git rebase main. Which graph do you get?
- On pancit-canton, rebase onto main
- Switch to main and bring pancit-canton in. It should fast-forward
- Find the old, pre-rebase commits in the reflog
Working folder
no changes
Staging area
nothing staged
Repository
12 commits on pancit-canton
You are in ~/kusina on pancit-canton. main has moved on since you made this branch.
Commit graph
cf91abfmain Welcome corrections in the READMEcf121f8HEAD →pancit-canton Add shrimp to pancit cantona1f23d8Add pancit canton recipe67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Conflicts during a rebase
Each commit is replayed in turn, so a conflict can stop the rebase partway, at one commit:
git rebase main Auto-merging README.md CONFLICT (content): Merge conflict in README.md error: could not apply a1f23d8... Mention pancit canton in the README 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".
Then:
- settle the file as in lesson 3.4,
git addit, andgit rebase --continueto replay the rest. (Nogit commit: rebase makes the commit.) - or
git rebase --abortto put everything back as before the rebase.git rebase --abortSafe: only adds or reads; nothing is lost - or
git rebase --skipto drop this one commit and carry on.git rebase --skipCan lose work: can throw away work Git never saved, or rewrite shared history: that commit's change is left out.
During a rebase, "ours" and "theirs" swap: ours is the branch you're rebasing onto (main), theirs is your commit being replayed. It's confusing for everyone; read the markers' labels.
git pull --rebase
git pull --rebase fetches, then rebases your new local commits onto the fetched branch, instead of merging. No merge commit for "Joaquin pushed while I was working": your commits simply go after his. Lesson 4.7 showed how to make it the default with pull.rebase true.
- Pull Joaquin’s commit with
git pull --rebase - Push. A straight line on the host too
Working folder
no changes
Staging area
nothing staged
Repository
11 commits on main
Remote
origin: example.com/ana/kusina.git · you are 1 ahead (as of your last fetch)
You are in ~/kusina with one new commit. Joaquin pushed a commit to origin a few minutes ago.
Commit graph
4b9596cHEAD →main Add halo-halo recipe67e435dorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Spot the trap: rebasing a shared branch
Ana and Joaquin both work on the desserts branch, both pushing to it. Ana rebases desserts onto main and pushes with --force.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-rebase: upstream, --continue, --abort, --skip
- Pro Git: Rebasing: the basic rebase, and the perils of rebasing
- Git documentation: git-pull, --rebase: rebasing the current branch on the upstream after fetching