how can git checkout HEAD~2 go 10 commits back

2019-02-20 20:29发布

问题:

There's a remote branch my-issue-branch and a local branch of the same name. We performed

git pull --rebase

to get the latest commits locally, but the two last commits are breaking the build. We want to go 2 commits back to build the project and do

git checkout HEAD~2

which takes us about 10 commits back, instead of just two.

Only

git checkout <commit-hash>

helps the situation.

Keeping in mind, that four people work on this branch, what can be wrong?

回答1:

Here's a simplified diagram:

...--o--*-----o------o--o   <-- you are here
         \          /
          o--o--o--o

You are at the marked commit, towards the right. You need to walk to the * commit towards the left, following one or the other lines, or maybe even following both at the same time. How many os will you traverse? How many os are there "between" commit * and the right hand edge one?

Now consider what happens with Git when there are merge commits (you must have some near the tip of your current branch, to be seeing this). The ~2 count walks back two commits on some particular line. What about all the commits on the other lines? What happens to them?