Git: interactive rebase lists incorrect (too many) commits

Jakob Jingleheimer picture Jakob Jingleheimer · Oct 15, 2014 · Viewed 8.6k times · Source

When I run git rebase -i HEAD~2, it lists 11 commits instead of 2. Why?

What I've done prior to this was:

  1. Checked out upstream/branchA
  2. Rebased my new local copy of branchA with master
  3. Tried to push my local branchA back to upstream
    • Git complained that the branches were out of sync, and to first pull in upstream
  4. Pulled upstream/branchA into local branchA
  5. Pushed local branchA to upstream/branchA (success)

Answer

Willem Van Onsem picture Willem Van Onsem · Oct 15, 2014

It depends on how your git tree looks like. A "merge" commit for instance can have two or more parents. Depending on this, your commit can have multiple grandparents.

You probably need to rebase with

git rebase -i HEAD^1^2
git rebase -i HEAD^2^1
git rebase -i HEAD^2^2

(one of these three).

See here for more details about git's relative commit notation.