Home·Terminal commands

git rebase

git

Usage

git rebase [-i] [--onto <base>] <upstream>

Replays your commits on a new base; the hashes change, so never rebase a branch other people already pulled — merge there instead.

Common flags

FlagMeaning
-iinteractive: reorder, squash, reword, drop commits
--onto <base>move the range onto a different base
--continuecarry on after fixing a conflict
--abortput the branch back exactly as it was
--skipdrop the commit that will not apply
--autostashstash uncommitted work and put it back afterwards
--autosquashapply fixup!/squash! commits automatically

Examples

git rebase main

replays your commits on top of main, giving them new hashes

git rebase -i HEAD~3

squash or reword the last three commits

git rebase --abort

cancel a rebase and return to the original branch

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git rebase do?

Replays your commits on a new base; the hashes change, so never rebase a branch other people already pulled — merge there instead.

Q. How do I type it?

git rebase [-i] [--onto <base>] <upstream> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

7 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-rebase