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

前端 未结 16 2182
醉酒成梦
醉酒成梦 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:24

    Simplistically:

    • ~ specifies ancestors
    • ^ specifies parents

    You can specify one or more branches when merging. Then a commit has two or more parents and then ^ is useful to indicate parents.

    Suppose you are on branch A and you have two more branches: B and C.

    On each branch the three last commits are:

    • A: A1, A2, A3
    • B: B1, B2, B3
    • C: C1, C3, C3

    If now on branch A you execute the command:

    git merge B C
    

    then you are combining three branches together (here your merge commit has three parents)

    and

    ~ indicates the n'th ancestor in the first branch, so

    • HEAD~ indicates A3
    • HEAD~2 indicates A2
    • HEAD~3 indicates A1

    ^ indicates the n'th parent, so

    • HEAD^ indicates A3
    • HEAD^2 indicates B3
    • HEAD^3 indicates C3

    The next use of ~ or ^ next to each other is in the context of the commit designated by previous characters.

    Notice 1:

    • HEAD~3 is always equal to: HEAD~~~ and to: HEAD^^^ (every indicates A1),

            and generally:

    • HEAD~n is always equal to: HEAD~...~ (n times ~) and to: HEAD^...^ (n times ^).

    Notice 2:

    • HEAD^3 is not the same as HEAD^^^ (the first indicates C3 and the second indicates A1),

            and generally:

    • HEAD^1 is the same as HEAD^,
    • but for n > 1: HEAD^n is always not the same as HEAD^...^ (n times ~).

提交回复
热议问题