Reputation: 49141
I saw an answer to a question here that helps restore a deleted file in git.
The solution was
git checkout <deleting_commit>^ -- <deleted_file_path>
What does the caret character (^
) do? I’ve seen it elsewhere doing very useful things in git. It’s magical. Someone please spoil it for me and tell me what it does?
Upvotes: 170
Views: 60444
Reputation: 139441
HEAD^
means the first parent of the tip of the current branch.
Remember that git commits can have more than one immediate parent. HEAD^
is short for HEAD^1
, and you can also address HEAD^2
and so on as appropriate. Any commit with multiple parents is the product of a merge or a merge commit.
You can get to parents of any commit, not just HEAD
. You can also move back through generations to any reachable ancestor: for example, master~2
means the grandparent of the tip of the master branch, favoring the first parent in cases of ambiguity. These specifiers can be chained arbitrarily. For example:
HEAD^^^
, HEAD~~~
, or HEAD~3
all refer to the great-grandparent commit, three generations back.
HEAD^3
is different: it means the third immediate parent of a merge commit — and strains the analogy to biology.topic~3^2
starts at the tag or tip of the branch named topic
, goes three generations back, and selects that merge commit’s (the great-grandparent’s) second immediate parent — the great-great-grandfather if you think of git history as being primarily matrilineal.See also:
HEAD^
and HEAD~
in Git?git rev-parse --help
Upvotes: 186
Reputation: 1219
OP: What does the caret (^) character mean in Git?
The difference between HEAD^
(Caret) and HEAD~
(Tilde) is how they traverse history backwards from a specified starting point, in this particular case HEAD
.
<rev>~[<n>]
= select <n>th
generation ancestor, following only first* parents
<rev>^[<n>]
= select <n>th
parent of first generation ancestors
*First parent is always the left hand side of the merge, e.g. the commit on the branch that got merged into.
As seen in the illustration below the two selectors ~ and ^ can be used in combination. Also note that instead of using HEAD
as a starting point, any regular reference can be used such as a branch, tag or even a commit hash.
Further more, depending on what ancestor is intended to be selected ^ and ~ can be used interchangeably as seen below in the table.
Source: A thorough rundown can be found in this blog post on the subject.
Upvotes: 7
Reputation: 3449
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.
Upvotes: 15
Reputation: 415
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
Upvotes: 2
Reputation: 116958
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) .
Upvotes: 30
Reputation: 1077
The (^) gets the parent source of the command i.e. HEAD^ will get the parent of HEAD.
Upvotes: 4
Reputation: 526573
The carat represents a commit offset (parent). So for instance, HEAD^
means "one commit from HEAD" and HEAD^^^
means "three commits from HEAD".
Upvotes: 4
Reputation: 410592
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.
Upvotes: 27
Reputation: 10685
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).
Upvotes: 9