如何在 Git 中获取合并提交的父项? [英] How can I get the parents of a merge commit in Git?

查看:64
本文介绍了如何在 Git 中获取合并提交的父项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些 Git 命令以父级作为修订;其他(例如 git revert),作为父编号.两种情况下我怎样才能找到父母?

Some Git commands take the parent as a revision; others (such as git revert), as a parent number. How can I 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.

推荐答案

Simple git log 调用合并提交显示其父级的缩写哈希:

Simple git log <hash> called for a merge commit shows abbreviated hashes of its parents:

 $ git log -1 395f65d
 commit 395f65d438b13fb1fded88a330dc06c3b0951046
 Merge: 9901923 d28790d
 ...

git 根据他们的数量输出父母:第一个(最左边的)哈希是第一个父母,依此类推.

git outputs parents according to their number: the first (leftmost) hash is for the first parent, and so on.

如果你想要的只是哈希值,两个等效的选择是:

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 也可以显示父母的哈希值,尽管它会首先列出提交的哈希值:

git rev-list can also show the parents' hashes, though it will first list the hash for a commit:

$ git rev-list --parents -n 1 <commit>

如果你想检查父母,你可以直接用克拉来指代他们作为^1^2,例如:

If you want to examine the parents, you can refer to them directly with carats as <commit>^1 and <commit>^2, e.g.:

git show <commit>^1

这确实概括了;对于章鱼合并,您可以将 nth 父项称为 ^n.您可以使用 <commit>^@ 来引用所有父项,尽管这在需要单次提交时不起作用.附加后缀可以出现在第 nth 父语法之后(例如 ^2^^2^@),而它们不能在 ^@ 之后(^@^ 无效).有关此语法的更多信息,请阅读 rev-parse 手册页.

This does generalize; for an octopus merge you can refer to the nth parent 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 nth parent 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.

这篇关于如何在 Git 中获取合并提交的父项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆