git rebase -i <commit>~<number>
This command lets you interactively rewrite a range of commits before a given point in history so you can clean up your commit log by editing, reordering, squashing, or removing commits before they are shared.
This command uses rebase in interactive mode (enabled by the -i flag, which can also be written as --interactive) and starts the rebase from the commit that is <number> steps before <commit>, thanks to the ~<number> syntax that walks backwards through the commit's ancestry; in practice, Git pops up an editor listing those commits, allowing you to change the action for each (pick, reword, edit, squash, fixup, or drop) and then replays the edited list on top of the parent of the oldest selected commit. The <commit> parameter is typically a commit hash or a name like HEAD, a branch name like main, or a tag, and combining it with ~<number> (for example HEAD~3) selects exactly <number> commits for interactive editing immediately before the specified commit. Because rebase rewrites history, this is usually applied to local feature branches before pushing, allowing you to produce a cleaner, more meaningful series of commits.
Beyond the base usage, this command supports the same options as other interactive rebases, such as -i --root to start from the very first commit, or targeting a range with a different ancestor like git rebase -i <upstream> to rewrite everything on your current branch that isn’t in <upstream>; related commands include git commit --amend for fixing just the latest commit without touching earlier ones, and git reset --soft <commit> followed by new git commit commands to manually rebuild a set of commits as an alternative to using the interactive todo list. The closest variants are things like git rebase -i HEAD~<number> (a more common shorthand when you just want to rewrite the last few commits on the current branch) and git rebase -i <commit> (which implicitly rewrites everything after <commit> instead of exactly <number> commits before it), while non-interactive git rebase <upstream> simply moves your current branch on top of another base without manual commit editing.
Examples:
git rebase -i HEAD~3git rebase -i abc1234~5git rebase -i main~2