Search code examples
gitgit-log

How to search a Git repository by commit message?


I checked some source code into Git with the commit message "Build 0051".

However, I can't seem to find that source code any more - how do I extract this source from the Git repository, using the command line?


Solution

  • To search the commit log (across all branches) for the given text:

    git log --all --grep='Build 0051'
    

    To do so while ignoring case in the grep search:

    git log --all -i --grep='Build 0051'
    

    To search the actual content of commits through a repo's history, use:

    git grep 'Build 0051' $(git rev-list --all)
    

    to show all instances of the given text, the containing file name, and the commit sha1.

    And to do this while ignoring case, use:

    git grep -i 'Build 0051' $(git rev-list --all)
    

    Note that this searches the entire content of the commit at each stage, and not just the diff changes. To search just the diff changes, use one of the following:

    git log -S[searchTerm]
    git log -G[searchTerm]
    

    Finally, as a last resort in case your commit is dangling and not connected to history at all, you can search the reflog itself with the -g flag (short for --walk-reflogs:

    git log -g --grep='Build 0051'
    

    If you seem to have lost your history, check the reflog as your safety net. Look for Build 0051 in one of the commits listed by

    git reflog
    

    You may have simply set your HEAD to a part of history in which the 'Build 0051' commit is not visible, or you may have actually blown it away. The git-ready reflog article may be of help.

    To recover your commit from the reflog: do a git checkout of the commit you found (and optionally make a new branch or tag of it for reference)

    git checkout 77b1f718d19e5cf46e2fab8405a9a0859c9c2889
    # alternative, using reflog (see git-ready link provided)
    # git checkout HEAD@{10}
    git checkout -b build_0051 # make a new branch with the build_0051 as the tip