如何在git中检测分支点? [英] How to detect a branch point in git?

查看:78
本文介绍了如何在git中检测分支点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定分支中的第一次提交.我还阅读了其他各种SO文章(包括使用Git查找分支点吗?),但他们没有给我我想要的东西.

I'm trying to determine the first commit in a branch. I've read various other SO posts (including Finding a branch point with Git?), but they don't give me what I'm after.

通过查看分支图,我可以确定分支何时与主分支分离(在随附的图像上,紫色线来自主分支上的第二次提交"),但是我无法复制该分支使用命令行工具在文本中输入文字(git log --graph可以使用,但不是我想要的).

From looking at a branch graph I can see that it is possible to determine when a branch diverged from master (on the included image, the purple line coming from "2nd commit on master"), however I can't replicate this in text using command-line tools (git log --graph works but is not what I'm after).

我要执行的提交是0124fc8,即对feature/new-branch的第一次提交.

The commit I'm after is 0124fc8, i.e, the first commit to feature/new-branch.

所有合并回母版的操作均通过--no-ff完成.

All merges back to master are done with --no-ff.

我已经尝试过git merge-base feature/new-branch master,但这给了我0f5e36f(--all也是一样)

I've tried git merge-base feature/new-branch master, however this gives me 0f5e36f (--all does the same)

我也尝试过git rev-list --boundary feature/new-branch...master,它给了我几次提交,但我没有想要的.

I've also tried git rev-list --boundary feature/new-branch...master, which gives me several commits, but none are the one that I want.

推荐答案

在没有合并回master的情况下,git rev-list master..feature/new-branch的最后一项将是您想要的.修订列表中的a..b为您提供了b历史中所有不在a历史中的提交.但是由于您已经合并,所以Feature/new-Branch历史中的所有提交也都在master的历史中,因此我们必须变得更加聪明.

In the absence of merges back onto master, the last entry of git rev-list master..feature/new-branch would be the one you want. a..b in a rev-list gives you all the commits in b's history that are not in a's history. But since you've merged, all the commits in feature/new-branch's history are also in master's history, so we have to be a bit cleverer.

您链接的问题(而不是此问题的答案)提供了一个很好的开始.诀窍是使用--first-parent来避免将所有合并到master中的右侧都视为master历史的一部分. git rev-list --first-parent master为您提供直接提交给master的所有提交,并且不显示任何已合并的提交. <,--first-parent不会按照我们想要的方式与..结合,因此我们必须使用diff来使自己的..等效.我们可以使用diff来获取特征/新分支的历史记录中所有提交的列表,并删除master的直接"历史记录中的所有提交.我使用了反斜杠转义来将一个命令分成几行:-

The question you linked (rather, this answer to it) gives a very good start. The trick is to use --first-parent to avoid considering the right-hand-side of all the merges into master as being part of master's history. git rev-list --first-parent master gives you all the commits that were committed directly into master, and doesn't show any commits that were merged. Alas, --first-parent doesn't combine with .. in the way we want, so we have to make our own .. equivalent, using diff. We can use diff to take our list of all the commits in feature/new-branch's history, and remove all the commits that's are in master's "direct" history. I've used backslash escaping to split the one command across several lines:-

diff --new-line-format=%L --old-line-format= --unchanged-line-format= \
    <(git rev-list --first-parent master) \
    <(git rev-list --first-parent feature/new-branch)

diff的选项使其仅打印出第二个输入中的行,而不打印第一个输入,没有+或-填充,也没有标题.我对POSIX不太了解,因此可能只有GNU diff具有这些选项.同样,对于其他两行上的<( )运算符,您需要bash或zsh.如果必须使用其他外壳程序,则可能需要使用临时文件.

The options to diff make it only print out the lines that are in the second input but not the first input, with no + or - stuff and no headers. I'm not well up on POSIX, so it may be that only GNU diff has those options. Similarly, for the <( ) operator on the other two lines, you need bash or zsh. If you have to use some other shell, you'll probably need to use temporary files.

第一个输入(要忽略的行之一)是要掌握的直接"历史记录,如上所述.第二个输入是要素/新分支的直接"历史记录.该命令的输出是直接"提交给功能部件/新分支的所有提交,而不是直接"提交给主控的所有提交,其中直接"表示不通过合并".在第二个输入中包含--first-parent是必要的,以避免包括在分支功能/新分支之前合并到主中的任何提交,但是这样做的副作用是将提交排除在其他任何内容之外合并到功能/新分支中的分支(如果您只希望第一次提交,则不会有什么不同.)

The first input (the one of lines to ignore) is the "direct" history to master, as we found out above; the second input is the "direct" history of feature/new-branch. The output of the command is all the commits that were committed to feature/new-branch "directly", but not to master "directly", where "directly" means "not through a merge". Including --first-parent on the second input is necessary to avoid including any commits that were merged into master before branching feature/new-branch, but it'll have the side-effect of excluding commits from any other branches that were merged into feature/new-branch (which doesn't make a difference if you only want the first commit).

与往常一样,输出是相反的顺序,因此输出的最后一行是您感兴趣的提交.

The output is in reverse order, as usual, so the last line of the output is the commit you're interested in.

我真的很想知道您要使用这些信息来实现什么,因为这似乎根本不符合Git的世界模型. Git用户知道并期望分支的历史包括分支祖先的历史,以及任何合并到分支中的历史.

I'm really curious to know what you're trying to achieve with this information, as it seems like it doesn't fit with Git's model of the world at all. Git users know and expect that the history of a branch includes the history from the branch's ancestor, and the history of any merges into it.

这篇关于如何在git中检测分支点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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