Rewriting history · lesson 2 of 6
Commands and ideas in this lesson
Agit rebase mainreplay this branch on top of main
B--continue, --abort, --skipduring a rebase conflict
Cgit pull --rebasefetch, then rebase

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
  1. cf91abf main Welcome corrections in the README
  2. cf121f8 HEAD →pancit-canton Add shrimp to pancit canton
  3. a1f23d8 Add pancit canton recipe
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README

and 3 older commits

1 of 3 pancit-canton split from main two commits ago. Since then, main got Joaquin’s README change.

Rebase pancit-canton

Predict the graph

Now:

  1. cf91abf main Welcome corrections in the README
  2. cf121f8 HEAD →pancit-canton Add shrimp to pancit canton
  3. a1f23d8 Add pancit canton recipe
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page

and 7 older commits

You run git rebase main. Which graph do you get?

Choose a graph
Git sandbox
  1. On pancit-canton, rebase onto main
  2. Switch to main and bring pancit-canton in. It should fast-forward
  3. 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

  1. cf91abf main Welcome corrections in the README
  2. cf121f8 HEAD →pancit-canton Add shrimp to pancit canton
  3. a1f23d8 Add pancit canton recipe
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README
  11. 9c53888 Add leche flan recipe
  12. 1f359a2 Add sinigang recipe
  13. 601c654 Add adobo recipe

Conflicts during a rebase

Each commit is replayed in turn, so a conflict can stop the rebase partway, at one commit:

Terminal
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 add it, and git rebase --continue to replay the rest. (No git commit: rebase makes the commit.)
  • or git rebase --abort to put everything back as before the rebase. git rebase --abortSafe: only adds or reads; nothing is lost
  • or git rebase --skip to 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.

Git sandbox
  1. Pull Joaquin’s commit with git pull --rebase
  2. 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

  1. 4b9596c HEAD →main Add halo-halo recipe
  2. 67e435d origin/main Cover the leche flan with foil
  3. ba04ca2 Add lumpiang shanghai recipe
  4. 98a5edc Add a script that builds the index page
  5. d078487 Make the sinigang steps clearer
  6. 145dc1e Add pancit bihon recipe
  7. 52d1f4d Say how much salt goes in the adobo
  8. 1c17fad Add a README
  9. 9c53888 Add leche flan recipe
  10. 1f359a2 Add sinigang recipe
  11. 601c654 Add adobo recipe

Spot the trap: rebasing a shared branch

Spot the trapAna

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

After git rebase main, do your branch’s commits have the same hashes as before?
A rebase stops with a conflict. You’ve settled the file. What next?
When is rebasing a branch a bad idea?

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.

All sources for Simple Git