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

前端 未结 16 2048
醉酒成梦
醉酒成梦 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条回答
  •  -上瘾入骨i
    2020-11-22 11:34

    It is worth noting that git also has a syntax for tracking "from-where-you-came"/"want-to-go-back-now" - for example, HEAD@{1} will reference the place from where you jumped to new commit location.

    Basically HEAD@{} variables capture the history of HEAD movement, and you can decide to use a particular head by looking into reflogs of git using the command git reflog.

    Example:

    0aee51f HEAD@{0}: reset: moving to HEAD@{5}
    290e035 HEAD@{1}: reset: moving to HEAD@{7}
    0aee51f HEAD@{2}: reset: moving to HEAD@{3}
    290e035 HEAD@{3}: reset: moving to HEAD@{3}
    9e77426 HEAD@{4}: reset: moving to HEAD@{3}
    290e035 HEAD@{5}: reset: moving to HEAD@{3}
    0aee51f HEAD@{6}: reset: moving to HEAD@{3}
    290e035 HEAD@{7}: reset: moving to HEAD@{3}
    9e77426 HEAD@{8}: reset: moving to HEAD@{3}
    290e035 HEAD@{9}: reset: moving to HEAD@{1}
    0aee51f HEAD@{10}: reset: moving to HEAD@{4}
    290e035 HEAD@{11}: reset: moving to HEAD^
    9e77426 HEAD@{12}: reset: moving to HEAD^
    eb48179 HEAD@{13}: reset: moving to HEAD~
    f916d93 HEAD@{14}: reset: moving to HEAD~
    0aee51f HEAD@{15}: reset: moving to HEAD@{5}
    f19fd9b HEAD@{16}: reset: moving to HEAD~1
    290e035 HEAD@{17}: reset: moving to HEAD~2
    eb48179 HEAD@{18}: reset: moving to HEAD~2
    0aee51f HEAD@{19}: reset: moving to HEAD@{5}
    eb48179 HEAD@{20}: reset: moving to HEAD~2
    0aee51f HEAD@{21}: reset: moving to HEAD@{1}
    f916d93 HEAD@{22}: reset: moving to HEAD@{1}
    0aee51f HEAD@{23}: reset: moving to HEAD@{1}
    f916d93 HEAD@{24}: reset: moving to HEAD^
    0aee51f HEAD@{25}: commit (amend): 3rd commmit
    35a7332 HEAD@{26}: checkout: moving from temp2_new_br to temp2_new_br
    35a7332 HEAD@{27}: commit (amend): 3rd commmit
    72c0be8 HEAD@{28}: commit (amend): 3rd commmit
    

    An example could be that I did local-commits a->b->c->d and then I went back discarding 2 commits to check my code - git reset HEAD~2 - and then after that I want to move my HEAD back to d - git reset HEAD@{1}.

提交回复
热议问题