Git Reference ยท Merging and Rebasing
git rebase -i command
Reference for git rebase --interactive: the todo list commands pick, reword, edit, squash, fixup, drop, exec and more, plus --autosquash and scripted use.
git rebase -i (--interactive) opens a todo list with every commit that is about to be replayed, one per line, oldest first. You change the word at the start of a line, reorder lines, or delete them, save the file, and Git carries out your plan. It is the standard tool for cleaning up local history before sharing it: squashing "fix typo" commits, rewording messages, splitting a commit, or dropping an experiment.
The range is the commits after the argument you give. git rebase -i HEAD~4 lets you edit the last four commits; git rebase -i main the ones not yet on main. Nothing is lost permanently: the pre-rebase tip stays in the reflog, and git rebase --abort returns to it at any time.
git rebase -i HEAD~3 # pick 1a2b3c first -> change "pick" to reword / edit / squash / fixup / drop git rebase -i --autosquash main # honor "fixup!" and "squash!" commits
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git rebase -i [<options>] [<upstream> [<branch>]] git rebase -i --root [<branch>] git rebase --edit-todo
Todo list commands (the first word of each line; most have a one-letter form):
| Command | Description |
|---|---|
pick (p) |
Use the commit as it is. |
reword (r) |
Use the commit, but stop to edit its message. |
edit (e) |
Use the commit, then stop so you can amend it, add commits (to split it), or change files. Finish with git rebase --continue. |
squash (s) |
Merge into the previous commit and combine the messages in an editor. |
fixup (f) |
Merge into the previous commit and keep only the previous message. fixup -C takes this commit's message, -c also opens an editor. |
drop (d) |
Remove the commit. Deleting the line does the same. |
exec (x) |
Run a shell command; the rebase stops if it fails. |
break (b) |
Stop here. Continue with git rebase --continue. |
label, reset, merge |
Recreate merge topology (used with --rebase-merges). |
update-ref |
Move a branch to this point (used with --update-refs). |
| Option | Description |
|---|---|
--autosquash |
Reorder fixup!, squash! and amend! commits made with git commit --fixup/--squash and mark them. rebase.autoSquash turns it on by default. |
--exec <cmd> |
Insert an exec line after every commit. |
--root |
Include the first commit. |
--keep-base |
Keep the current merge base as the base. |
--edit-todo |
Reopen the todo list during a stopped rebase. |
Environment: GIT_SEQUENCE_EDITOR (or sequence.editor) overrides the editor for the todo list only; GIT_EDITOR (core.editor) is used for commit messages. Both let you script a rebase, as above.
Usage notes
- Order is oldest first. This is the opposite of
git log. The top line is applied first. - Reordering can conflict. Moving a commit before one it depends on makes Git replay it on a different base; expect conflicts and use
--continue. - Squash versus fixup. Use
fixupwhen the message of the earlier commit is already right; usesquashwhen you want to rewrite the combined message. - Splitting a commit. Mark it
edit; when the rebase stops rungit reset HEAD^, then stage and commit the pieces separately, andgit rebase --continue. - Deleting all lines (or saving an empty file) aborts the rebase without changes.
- Pick-up point. A todo line for a commit that no longer exists in the repository (garbage collected) fails the rebase; recent commits are always available.
- Commit hashes in the list do not matter for the commits you keep; they change as the commits are rewritten. Do not use interactive rebase on commits you have already pushed unless you can force-push safely:
git push --force-with-lease. - Fixup workflow.
git commit --fixup=<commit>while working, thengit rebase -i --autosquash <base>once at the end gives clean commits with almost no effort. - Recovering.
git rebase --abortduring the session, orgit reflogafterwards, brings the old history back. - Empty commits. A commit that becomes empty is dropped; the
--emptyoption controls this.