Git Rebase
Last updated
git rebase moves your branch's commits so they replay on top of another branch, producing a straight, linear history instead of a merge commit. Interactive rebase (-i) goes further, letting you squash, reorder, edit, or drop commits. Because rebasing rewrites commits, only do it to work you haven't shared yet.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git rebase main | Replay your commits on top of main |
git rebase -i HEAD~3 | Interactively edit the last 3 commits |
git rebase --onto main old new | Move a range of commits onto main |
git rebase --continue | Resume after resolving conflicts |
git rebase --abort | Cancel the rebase, restore the branch |
git pull --rebase | Pull and rebase instead of merging |
Interactive rebase keywords
In the editor -i opens, change pick to one of these.
| Keyword | What it does |
|---|---|
pick | Keep the commit |
reword | Keep the commit, edit its message |
squash | Fold into the previous commit, merge messages |
fixup | Fold in, discard this message |
drop | Remove the commit entirely |
Git rebase FAQ
What does git rebase do?
It takes the commits on your current branch and replays them one by one on top of another branch, as if you'd started your work from that branch's latest commit. The result is a linear history with no merge commit. The commits are rewritten (they get new hashes) in the process.
What is interactive rebase?
git rebase -i <base> opens an editor listing the commits since <base>, where you can reorder them, squash several into one, reword messages, edit a commit, or drop it. It's the main tool for cleaning up a messy series of commits before sharing them.What's the golden rule of rebasing?
Never rebase commits that others have already based work on - typically anything pushed to a shared branch. Because rebase rewrites commits, doing so forces everyone else to reconcile a diverged history. Rebase freely on local, unpushed commits; use merge or revert for public ones.
How do I fix or cancel a rebase with conflicts?
When a rebase stops on a conflict, resolve the files,
git add them, then run git rebase --continue. Use git rebase --skip to drop the current commit, or git rebase --abort to cancel entirely and return the branch to how it was before the rebase started.Can I practice this online?
Yes. Open the terminal playground to run
git rebase in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers rebasing step by step.