What's the difference between HEAD^ and HEAD~ in Git?

前端 未结 16 2049
醉酒成梦
醉酒成梦 2020-11-22 10:43

When I specify an ancestor commit object in Git, I\'m confused between HEAD^ and HEAD~.

Both have a \"numbered\" version like HEAD^3<

16条回答
  •  醉梦人生
    2020-11-22 11:15

    ^ BRANCH Selector
    git checkout HEAD^2
    Selects the 2nd branch of a (merge) commit by moving onto the selected branch (one step backwards on the commit-tree)

    ~ COMMIT Selector
    git checkout HEAD~2
    Moves 2 commits backwards on the default/selected branch


    Defining both ~ and ^ relative refs as PARENT selectors is far the dominant definition published everywhere on the internet I have come across so far - including the official Git Book. Yes they are PARENT selectors, but the problem with this "explanation" is that it is completely against our goal: which is how to distinguish the two... :)

    The other problem is when we are encouraged to use the ^ BRANCH selector for COMMIT selection (aka HEAD^ === HEAD~).
    Again, yes, you can use it this way, but this is not its designed purpose. The ^ BRANCH selector's backwards move behaviour is a side effect not its purpose.

    At merged commits only, can a number be assigned to the ^ BRANCH selector. Thus its full capacity can only be utilised where there is a need for selecting among branches. And the most straightforward way to express a selection in a fork is by stepping onto the selected path / branch - that's for the one step backwards on the commit-tree. It is a side effect only, not its main purpose.

提交回复
热议问题