Git - 如何找到特定分支的第一次提交

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

Git - how to find first commit of specific branch

git

提问by user2699113

In following example tree:

在以下示例树中:

A-B-C-D-E (master branch)
    \
     F-G-H (xxx branch)

I'm looking for F - the first commit in xxx branch. I think that it is possible with:

我正在寻找 F - xxx 分支中的第一次提交。我认为这是可能的:

git log xxx --not master

and the last listed commit should be F. Is it correct solution or maybe there are some disadvantages of it?

最后列出的提交应该是 F。它是正确的解决方案还是它有一些缺点?

I know that there were similar questions on stackoverflow, but nobody proposed such solution, and I'm not sure if I do it right.

我知道在 stackoverflow 上也有类似的问题,但是没有人提出这样的解决方案,我不确定我是否做得对。

回答by konyak

git log master..branch --oneline | tail -1

Where "branch" is your specific branch name. The dot-dot gives you all of the commits that the branch has that master doesn't have. tail -1returns the last line from the previous output.

其中“分支”是您的特定分支名称。点-点为您提供了该分支具有而 master 没有的所有提交。tail -1返回上一个输出的最后一行。

回答by Fabrizio Stellato

You should use the merge-basefunctionality which is designed to solve exactly this:

您应该使用merge-base旨在解决此问题的功能:

git merge-base remotes/origin/<branch> develop 

回答by Nelu

git cherry master -v | head -n 1

回答by Nayagam

If your branch (old one) once again merged back to master doesn't give expected result.I have using python script to find initial branch commit Id.

如果您的分支(旧分支)再次合并回 master 并没有给出预期的结果。我使用 python 脚本来查找初始分支提交 ID。

git rev-list --first-parent changeset

git rev-list --first-parent 变更集

--first-parentfollow only the first parent commit upon seeing a merge commit.

--first-parent在看到合并提交时只关注第一个父提交。

Iterate changeset's from above command until parent branch found.

从上面的命令迭代变更集,直到找到父分支。

def status_check(exec_command, exec_dir=None, background=False):
    if exec_dir:
        os.chdir(exec_dir)
    res = subprocess.Popen(exec_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if not background:
        result = res.communicate()
    return result



def findNewBranchCommits(changeset=None):
    cmd = "git rev-list --first-parent "+ changeset
    rev_list = status_check(cmd,self.module_dir)
    rev_list = str(rev_list[0]).split('\n')
    rev_list = list(filter(None, rev_list))
    for x in rev_list:                      # Iterate until branch base point
        rev_cmd = "git branch --contains " + x
        rev_cmd = status_check(rev_cmd,self.module_dir)
        rev_cmd = str(rev_cmd[0]).split('\n')
        if(len(rev_cmd) > 2): 
            print "First Commit in xxx branch",x
            break

findNewBranchCommits(changeset)

回答by Doc Unid

git cherry master -v | tail -1   

however this will only give you the first commit on branch xxx that is not in master, not the first commit on branch xxx ever. the latter would be difficult if branch xxx has been deleted and/or re-created one or more times. it that case you could try the following:

然而,这只会给你不在 master 的分支 xxx 上的第一次提交,而不是分支 xxx 上的第一次提交。如果分支 xxx 已被删除和/或重新创建一次或多次,后者将很困难。在这种情况下,您可以尝试以下操作:

git reflog | grep checkout | grep xxx | tail -1   

回答by wedens

git rev-list --ancestry-path $(git merge-base master xxx)..xxx | tail -1

git rev-list --ancestry-path $(git merge-base master xxx)..xxx | tail -1