Search code examples
gitgithubgit-commitgit-loggit-rev-list

Get latest commit which contains a particular string


I want to get the latest commit which contains a string. For e.g.

String = TAG_2021_09_0051

I have tried git log --grep "TAG_2021_09_0051" which gives me below ouput as string is present in two commits. But I want the latest commit from which I want to fetch the commit ID.

commit 12345678
Author: none
Date:   Fri Oct 15 21:39:56 2016 +0000

    @: 1234 - TAG_2021_09_0051

commit 45678965
Author: none
Date:   Fri Oct 14 21:39:56 2016 +0000

    @: 1234 - TAG_2021_09_0051

Is there any way to get the latest git commit which contains a particular string even if string is present in multiple commits?

The actual output should be below commit

commit 12345678
    Author: none
    Date:   Fri Oct 15 21:39:56 2016 +0000
    
        @: 1234 - TAG_2021_09_0051

Solution

  • From the git log documentation you want the -n option (https://git-scm.com/docs/git-log):

    git log --grep "TAG_2021_09_0051" -n1