首页 > git > 返回最后合并的Pull请求Git的编号

返回最后合并的Pull请求Git的编号 (Return the Number of the Last Merged Pull Request Git)

2019-03-07 git

问题

我想返回最新合并拉取请求的数量。Git命令git show --merges --count目前实现了这一点(见下文),但是,我想返回最新的合并拉取请求号。就我而言,#402

知道怎么做到这一点?值得一提的是,我也试过了,git rev-list --count HEAD但这只返回了存储库中的提交总数。

C:\Dev\home>git show --merges --count

commit 46kh4k56h4h56hk45h6k4h56k4jh56kjh45k6h
Merge: 4564hhf5 4h456hr
Author: kerbol 
Date:   Mon Feb 15 10:02:04 2019

Merge pull request #402 from FSP/JIRA-992_job_import

JIRA-992. job_import

解决方法

尝试grep对你想要的模式做一个,然后限制到第一个匹配:

git log --grep="Merge pull request #[0-9]\+" --pretty=oneline -1
    | sed -En "s/.*#([[:digit:]]\+).*/\1/p";

这将显示与模式匹配的第一个提交Merge pull request#[0-9]+。如果你想要多个,例如三个,那么只需-3在上面命令的末尾使用。

编辑:

如果您还想从git log上面的输出中提取提交号,请考虑将其输入sed

问题

I want to return the number of latest merged pull request. The Git command git show --merges --count currently achieves this (see below) however, I want to only return the latest merged pull request number. In my case, #402.

Any idea how to achieve this? It's worth mentioning that I also tried git rev-list --count HEAD but this only returns the total number of commits on the repository.

C:\Dev\home>git show --merges --count

commit 46kh4k56h4h56hk45h6k4h56k4jh56kjh45k6h
Merge: 4564hhf5 4h456hr
Author: kerbol 
Date:   Mon Feb 15 10:02:04 2019

Merge pull request #402 from FSP/JIRA-992_job_import

JIRA-992. job_import

解决方法

Try doing a grep on the pattern you want, then limit to just the first match:

git log --grep="Merge pull request #[0-9]\+" --pretty=oneline -1
    | sed -En "s/.*#([[:digit:]]\+).*/\1/p";

This would show the first commit matching the pattern Merge pull request#[0-9]+. If you want more than one, e.g. three, then just use -3 at the end of the above command.

Edit:

If you additionally want to extract the commit number from the output of git log above, then consider piping it into sed.

相似信息