[英]How can I get the SHA number of previous commit?
For my research I need few data, one of which is the SHA number for the previous commit ie if I give a particular commit number/SHA number, I should be able to get the SHA number before it. 对于我的研究,我需要的数据很少,其中之一是上一次提交的SHA编号,即,如果我给出特定的提交编号/ SHA编号,则应该能够在此之前获得SHA编号。
Help me with the git command to get the same. 使用git命令帮助我获得相同的结果。
The command to translate any specifier into a hash ID is git rev-parse
. 将任何说明符转换为哈希ID的命令是git rev-parse
。
The syntax that means "find the parent commit of a commit" is from gitrevisions , and is commit ^
or commit ~
—use whichever you prefer to type. 这意味着 “寻父提交的承诺”是从语法 gitrevisions ,并commit ^
或commit ~
无论你喜欢打字次使用。 The commit
part can be nearly any valid commit specifier, including a branch name, another commit hash ID, the special name HEAD
, or another one of these suffixed expressions. commit
部分几乎可以是任何有效的提交说明符,包括分支名称,另一个提交哈希ID,特殊名称HEAD
或这些后缀表达式中的另一个。
Hence: 因此:
HEAD^
is the parent of HEAD
, and: 是HEAD
的父级,并且:
HEAD^^
is the parent of HEAD^
, and so on. 是HEAD^
的父级,依此类推。
The tilde syntax is actually a compressed version of many hat suffixes, so: 波浪号语法实际上是许多帽子后缀的压缩版本,因此:
HEAD~5
means the same thing as: 与以下内容含义相同:
HEAD^^^^^
If the number after the tilde is missing, Git assumes you meant 1
. 如果波浪号后的数字丢失,则Git假定您的意思是1
。
What all this means, put together, is that: 综上所述,这意味着:
git rev-parse HEAD^
(or HEAD~
) will get you the actual hash ID. (或HEAD~
)将为您获取实际的哈希ID。 But you can just write HEAD^
or HEAD~
instead, with any normal Git command. 但是,您可以使用任何普通的Git命令来编写HEAD^
或HEAD~
。 Similarly, if 1234567
is a valid shortened commit hash ID, you can write 1234567^
or 1234567~1
to refer to its parent commit. 同样,如果1234567
是有效的缩短的提交哈希ID,则可以编写1234567^
或1234567~1
来引用其父提交。
(Commits that are merge commits have two or more parents, in which case there is more syntax available to extract each parent one at a time, or to refer to all parents. Again, see gitrevisions for details.) (属于合并提交的提交有两个或多个父级,在这种情况下,可以使用更多语法来一次提取每个父级,或引用所有父级。再次,请参见gitrevisions以获得详细信息。)
git rev-list --parents -n 1 SOME_COMMIT
将为您提供您指定的提交的SHA,然后是其父级的SHA。
git log --format="%H" -n 1 <commit>~
仅输出哈希值(%H),并将输出限制为提交父项(〜)的1个条目(而不显示所有祖先)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.