Kerbol
Kerbol

Reputation: 696

Return the Number of the Last Merged Pull Request Git

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

Upvotes: 1

Views: 2249

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

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.

Upvotes: 2

Related Questions