如何在 Git 中获取特定提交的父级?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44112593/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:59:09  来源:igfitidea点击:

How to get parent of specific commit in Git?

git

提问by MPAW

I have commit number. I would like to get previous commit number(parent). I need commits from current branch.

我有提交号。我想获得以前的提交编号(父)。我需要从当前分支提交。

回答by MPAW

git log --pretty=%P -n 1 "$commit_from"

回答by Nayagam

To get Parent Commit

获取父提交

git cat-file -p commit_id

tree tree_id
parent parent_commit_id
[parent other_parent_commit_id] # present only in case of merge commits
author xxx <[email protected]> 1513768542 +0530
committer xxx <[email protected]> 1513768542 +0530 

回答by William Pursell

If ${SHA} is the commit you know and you want its parent (assuming it's not a merge commit and has only one parent):

如果 ${SHA} 是您知道的提交并且您想要它的父级(假设它不是合并提交并且只有一个父级):

git rev-parse ${SHA}^

回答by Magne

To get the parent, just add a ^after the commit SHA of the commit you want to see.

要获取父级,只需在要查看的提交的提交 SHA 后添加^

To see a commit: git show <SHA>Example: git show bb05425c504575145d005c0a887e0a80b885ced0

查看提交: git show <SHA>示例: git show bb05425c504575145d005c0a887e0a80b885ced0

To see the parent: git show <SHA>^
Example: git show bb05425c504575145d005c0a887e0a80b885ced0^

查看父级: git show <SHA>^
示例: git show bb05425c504575145d005c0a887e0a80b885ced0^

回答by matvore

You can use git rev-parsefor this.

您可以git rev-parse为此使用。

# Output hash of the first parent
git rev-parse $commit^

# Nth parent
git rev-parse $commit^N

# All parents
git rev-parse $commit^@

These constructs are explained in git help rev-parse:

这些构造在git help rev-parse

 <rev>^, e.g. HEAD^, v1.5.1^0
     A suffix ^ to a revision parameter means the first
     parent of that commit object.  ^<n> means the <n>th
     parent (i.e.  <rev>^ is equivalent to <rev>^1). As a
     special rule, <rev>^0 means the commit itself and is
     used when <rev> is the object name of a tag object that
     refers to a commit object.

 ...

 <rev>^@, e.g. HEAD^@
     A suffix ^ followed by an at sign is the same as listing
     all parents of <rev> (meaning, include anything
     reachable from its parents, but not the commit itself).

回答by nate

If trying to get all parents and using revision parameter syntax, you may try using logsubcommand with --no-walkoption.

如果尝试获取所有父项并使用修订参数语法,您可以尝试使用log--no-walk选项的子命令 。

An example, if we have the following:

一个例子,如果我们有以下内容:

$ git --oneline --graph
*   A
|\
| * B
| * C
| * D
* |   E

In this example, I'll use ^@to get all parents and the --no-walkoption to show only the parents and not their ancestors.

在此示例中,我将使用^@获取所有父项和--no-walk仅显示父项而不显示其祖先的选项。

$ git log --no-walk A^@
commit B
    ........

commit E
    ........

Check out git rev-parsefor more detail about revision parameter.

查看git rev-parse以获取有关修订参数的更多详细信息。

回答by mljrg

If you only want the ids of the parents of an input commit with id <SHA>then run this command:

如果您只想要带有 id 的输入提交的父代的 id,请<SHA>运行以下命令:

git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print ; next}{exit}}'

This will work for normal and shallow clones.

这适用于普通和浅层克隆。