git HEAD~ vs HEAD^ vs HEAD@{} 也称为波浪号 vs 插入符 vs at 符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26785118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HEAD~ vs HEAD^ vs HEAD@{} also known as tilde vs caret vs at sign
提问by Shaun Luttin
HEAD is a pointer at the current branch. I have seen a variety of notations for ancestors of HEAD including
HEAD 是当前分支的指针。我已经看到了 HEAD 祖先的各种符号,包括
HEAD~2
HEAD^2
HEAD@{2}
HEAD~~
HEAD^^
HEAD~2
HEAD^2
HEAD@{2}
HEAD~~
HEAD^^
What does each of the above mean, exactly? Where is the documentation for this?
以上每一项究竟是什么意思?这方面的文档在哪里?
回答by Tim
From the docs here.
从这里的文档。
HEAD~2
: 2 commits older than HEADHEAD^2
: the second parent of HEAD, if HEAD was a merge, otherwise illegalHEAD@{2}
: refers to the 3rd listing in the overview ofgit reflog
HEAD~~
: 2 commits older than HEADHEAD^^
: 2 commits older than HEAD
HEAD~2
: 2 次提交比 HEAD 早HEAD^2
: HEAD 的第二个父级,如果 HEAD 是合并,否则非法HEAD@{2}
: 参考概述中的第 3 个列表git reflog
HEAD~~
: 2 次提交比 HEAD 早HEAD^^
: 2 次提交比 HEAD 早
If HEAD was a merge, then
如果 HEAD 是一个合并,那么
- first parentis the branch into which we merged,
- second parentis the branch we merged.
- 第一个父级是我们合并到的分支,
- 第二个父级是我们合并的分支。
Some Combinations and Synonyms
一些组合和同义词
First Parent First Grandparent Second Parent Second Grandparent
HEAD~
HEAD^
HEAD~1 HEAD~2 HEAD^2 HEAD^2~
HEAD^1 HEAD^^ HEAD^2^
回答by Premraj
git reference suffixes (^N, ~N, @{...})
git 引用后缀 (^N, ~N, @{...})
ref~
is shorthand for ref~1
and means the commit's first parent. ref~2
means the commit's first parent's first parent. ref~3
means the commit's first parent's first parent's first parent. And so on.
ref~
是ref~1
并且表示提交的第一个父级的简写。ref~2
表示提交的第一个父级的第一个父级。ref~3
表示提交的第一个父级的第一个父级的第一个父级。等等。
ref^
is shorthand for ref^1
and means the commit's first parent. But where the two differ is that ref^2
means the commit's second parent (remember, commits can have two parents when they are a merge).
ref^
是ref^1
并且表示提交的第一个父级的简写。但是两者的不同之处在于,这ref^2
意味着提交的第二个父项(请记住,当它们是合并时,提交可以有两个父项)。
The ^ and ~ operators can be combined.
^ 和 ~ 运算符可以组合使用。
Here's a diagram showing how to reference various commits using HEAD as the starting point.
回答by mvanle
I count each ~
or ^
to mean "going back one level". If there is a number next to ~
(eg. ~n
), then n acts as a multiplier. If there is a number next to ^
(eg. ^n
), then n is the n'th parent to use (or sideways movement going from left-to-right column position in git log --graph
).
我计算每个~
或^
意思是“回到一个级别”。如果~
(例如~n
)旁边有一个数字,则 n 充当乘数。如果^
(例如^n
)旁边有一个数字,则 n 是要使用的第 n 个父项(或从 中从左到右列位置的横向移动git log --graph
)。
Example:
例子:
$ git log --oneline --graph
* 29392c8 (HEAD -> master, tag: A) A
|\
| * a1ef6fd (tag: C) C
| |
| \
*-. \ 8ae20e9 (tag: B) B
|\ \ \
| | |/
| | * 03160db (tag: F) F
| | |\
| | | * 9df28cb (tag: J) J
| | * 2afd329 (tag: I) I
| * a77cb1f (tag: E) E
* cd75703 (tag: D) D
|\
| * 3043d25 (tag: H) H
* 4ab0473 (tag: G) G
Coordinates for above tags:
以上标签的坐标:
A = = A^0
B = A^ = A^1 = A~1
C = A^2
D = A^^ = A^1^1 = A~2
E = B^2 = A^^2
F = B^3 = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2 = B^^2 = A^^^2 = A~2^2
I = F^ = B^3^ = A^^3^
J = F^2 = B^3^2 = A^^3^2
The git log --online --graph
output makes it hard to see which commits are on the same level, so here is another presentation (where "A" is the latest commit and older commits are at the top):
该git log --online --graph
输出使得它很难看到其提交都在同一水平线上,所以这里是另一个演示文稿(其中“A”是最新的提交及以上的提交是在顶部):
G H I J
\ / \ /
D E F
\ | / \
\ | / |
\|/ |
B C
\ /
\ /
A
(Illustrations excerpted from What's the difference between HEAD^ and HEAD~ in Git?).
(插图摘自Git 中 HEAD^ 和 HEAD~ 的区别是什么?)。