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
| Flag | Meaning |
|---|---|
| -i | interactive: reorder, squash, reword, drop commits |
| --onto <base> | move the range onto a different base |
| --continue | carry on after fixing a conflict |
| --abort | put the branch back exactly as it was |
| --skip | drop the commit that will not apply |
| --autostash | stash uncommitted work and put it back afterwards |
| --autosquash | apply fixup!/squash! commits automatically |
Examples
git rebase mainreplays your commits on top of main, giving them new hashes
git rebase -i HEAD~3squash or reword the last three commits
git rebase --abortcancel 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