24

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)
Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
  • 1
    While `HEAD~2` means "go back 2", it more specifically means "the *first* parent of the *first* parent", emphasis on "first" because there can be a second parent (or even more). When you find "extra" commits like this that means there's a merge commit (a 2nd parent) in the mix and you're including the commits on both sides of the merge. – torek Oct 15 '14 at 00:28
  • So…how would one get it to behave "normally" ? – Jakob Jingleheimer Oct 15 '14 at 00:45
  • `HEAD~2^2` I guess? See [here](http://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git) for more details on `git`'s relative commit notation. – Willem Van Onsem Oct 15 '14 at 00:48
  • Depends on the shape of the commit graph and what, precisely, you want to rebase / have as your result. – torek Oct 15 '14 at 00:57

2 Answers2

17

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.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks for the explanation! I ended up just doing a forced push because, 1. my branch was actually up-to-date (and no-one else was working from the old copy); and 2. I didn't want that weird multi-parent behaviour to persist. If it happens again, tho, I'll try the above. After doing one of the above, will `git rebase -i HEAD~2` return to "normal" ? – Jakob Jingleheimer Oct 15 '14 at 16:47
1

I fixed this by doing a rebase with master first and then for the rest of the commits

git rebase master
git rebase -i HEAD~n
Sheraz Ahmed
  • 564
  • 9
  • 29