What does the caret (^) character mean?

前端 未结 8 904
广开言路
广开言路 2020-11-29 17:24

I saw an answer to a question here that helps restore a deleted file in git.

The solution was

git checkout ^ -- 

        
相关标签:
8条回答
  • 2020-11-29 18:03

    The ^ (caret) can also be used when specifying ranges.

    To exclude commits reachable from a commit, a prefix ^ notation is used. E.g. ^r1 r2 means commits reachable from r2 but exclude the ones reachable from r1.

    <rev>

    Include commits that are reachable from (i.e. ancestors of) .

    ^<rev>

    Exclude commits that are reachable from (i.e. ancestors of) .

    0 讨论(0)
  • 2020-11-29 18:12

    Greg Bacon gave a great link, but it's pretty dense. The Git introductory docs online also introduce revision and range specifiers:

    https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

    0 讨论(0)
  • 2020-11-29 18:14

    It means "parent of". So HEAD^ means "the parent of the current HEAD". You can even chain them together: HEAD^^ means "the parent of the parent of the current HEAD" (i.e., the grandparent of the current HEAD), HEAD^^^ means "the parent of the parent of the parent of the current HEAD", and so forth.

    0 讨论(0)
  • 2020-11-29 18:14

    The carat represents a commit offset (parent). So for instance, HEAD^ means "one commit from HEAD" and HEAD^^^ means "three commits from HEAD".

    0 讨论(0)
  • 2020-11-29 18:16

    Here's a visual explanation. Suppose you have a history like so:

                          master  
      ... <- B <- C <- D
                 /
    ... <- E <- F
                  feature
    

    When feature was merged into master, C was created with two ancestors. Git assigns these ancestors numbers. The mainline ancestor B is assigned 1 and the feature ancestor F is assigned 2.

    Thus C^1 refers to B and C^2 refers to F. C^ is an alias for C^1.

    You would only ever use <rev>^3. if you had performed a merge of three branches.

    0 讨论(0)
  • 2020-11-29 18:16

    The caret refers to the parent of a particular commit. E.g. HEAD^ refers to the parent of the current HEAD commmit. (also, HEAD^^ refers to the grandparent).

    0 讨论(0)
提交回复
热议问题