Git rebase interactive the last n commits

Thomas S. picture Thomas S. · Jan 4, 2017 · Viewed 21.3k times · Source

I have made a bunch of unpushed commits in my feature branch and now want to reorder and partly squash belonging commits visually. I reckon the solution somehow lies in the Git interactive, but how to invoke it?

$ git rebase --interactive --onto <the-ID-of-the-first-commit-to-rewrite>

just pops up the VI with a

noop

content followed by commented information. After exiting, my head is reset to the specified commit.

How to correctly trigger the interactive rebase for modifying the commits since a certain commit?

Answer

Chris Maes picture Chris Maes · Jan 4, 2017

you should use

git rebase --interactive <sha1>

where <sha1> should not be the sha of the first commit you want to rewrite, but the sha of the commit just before.

if your history looks like this:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit

then you can there are different ways to indicate the commit on which to rebase:

git rebase -i cb85072
git rebase -i 5116d42^

where

  • ^ means the commit just before.
  • -i is just short for --interactive