如何在 git 中获取合并提交的父级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9059335/
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
How to get the parents of a merge commit in git?
提问by Casebash
Some git commands take the parent as a revision; others (such as git revert
), as a parent number. How to get the parents for both cases. I don't want to use the graphical log command as that often requires scrolling down a long tree to find the second parent.
一些 git 命令将父级作为修订;其他(例如git revert
),作为父编号。如何为这两种情况找到父母。我不想使用图形日志命令,因为这通常需要向下滚动一棵长树才能找到第二个父级。
回答by Cascabel
Simple git log <hash>
called for a merge commit shows abbreviated hashes of its parents:
git log <hash>
对合并提交的简单调用显示其父项的缩写哈希:
$ git log -1 395f65d
commit 395f65d438b13fb1fded88a330dc06c3b0951046
Merge: 9901923 d28790d
...
git
outputs parents according to their number: the first (leftmost) hash is for the first parent, and so on.
git
根据他们的数量输出父母:第一个(最左边的)哈希是第一个父母,依此类推。
If all you want is just the hashes, the two equivalent choices are:
如果你想要的只是散列,两个等效的选择是:
$ git log --pretty=%P -n 1 <commit>
$ git show -s --pretty=%P <commit>
git rev-list
can also show the parents' hashes, though it will first list the hash for a commit:
git rev-list
也可以显示父母的哈希值,尽管它会首先列出提交的哈希值:
$ git rev-list --parents -n 1 <commit>
If you want to examine the parents, you can refer to them directly with carats as <commit>^1
and <commit>^2
, e.g.:
如果你想检查父母,你可以直接用克拉指代他们作为<commit>^1
和<commit>^2
,例如:
git show <commit>^1
This does generalize; for an octopus merge you can refer to the nthparent as <commit>^n
. You can refer to all parents with <commit>^@
, though this doesn't work when a single commit is required. Additional suffixes can appear after the nthparent syntax (e.g. <commit>^2^
, <commit>^2^@
), whereas they cannot after ^@
(<commit>^@^
isn't valid). For more on this syntax, read the rev-parse
man page.
这确实概括了;对于章鱼合并,您可以将第 n个父代称为<commit>^n
. 您可以使用 引用所有父级<commit>^@
,但在需要一次提交时这不起作用。附加后缀可以出现在第 n个父语法之后(例如<commit>^2^
, <commit>^2^@
),而它们不能出现在^@
(<commit>^@^
无效) 之后。有关此语法的更多信息,请阅读rev-parse
手册页。
回答by user1483344
The following is the simplest way I've found to view the parents of a merge
以下是我发现的查看合并父项的最简单方法
git show --pretty=raw 3706454