如何在 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

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

How to get the parents of a merge commit in git?

gitmerge

提问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
 ...

gitoutputs 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-listcan 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>^1and <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-parseman 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