27

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)
4
  • 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. Commented Oct 15, 2014 at 0:28
  • So…how would one get it to behave "normally" ? Commented Oct 15, 2014 at 0:45
  • HEAD~2^2 I guess? See here for more details on git's relative commit notation. Commented Oct 15, 2014 at 0:48
  • Depends on the shape of the commit graph and what, precisely, you want to rebase / have as your result. Commented Oct 15, 2014 at 0:57

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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" ?
2

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.