How to grep (search) committed code in the Git history

后端 未结 15 1208

I have deleted a file or some code in a file sometime in the past. Can I grep in the content (not in the commit messages)?

A very poor solution is to grep the log:

相关标签:
15条回答
  • 2020-11-22 06:48

    Scenario: You did a big clean up of your code by using your IDE. Problem: The IDE cleaned up more than it should and now you code does not compile (missing resources, etc.)

    Solution:

    git grep --cached "text_to_find"
    

    It will find the file where "text_to_find" was changed.

    You can now undo this change and compile your code.

    0 讨论(0)
  • 2020-11-22 06:48

    Adding more to the answers already present. If you know the file in which you might have made do this:

    git log --follow -p -S 'search-string' <file-path>
    

    --follow: lists the history of a file

    0 讨论(0)
  • 2020-11-22 06:50

    I took Jeet's answer and adapted it to Windows (thanks to this answer):

    FOR /F %x IN ('"git rev-list --all"') DO @git grep <regex> %x > out.txt
    

    Note that for me, for some reason, the actual commit that deleted this regex did not appear in the output of the command, but rather one commit prior to it.

    0 讨论(0)
  • 2020-11-22 06:55

    If you want to browse code changes (see what actually has been changed with the given word in the whole history) go for patch mode - I found a very useful combination of doing:

    git log -p
    # Hit '/' for search mode.
    # Type in the word you are searching.
    # If the first search is not relevant, hit 'n' for next (like in Vim ;) )
    
    0 讨论(0)
  • 2020-11-22 06:56

    Search in any revision, any file (unix/linux):

    git rev-list --all | xargs git grep <regexp>
    

    Search only in some given files, for example XML files:

    git rev-list --all | xargs -I{} git grep <regexp> {} -- "*.xml"
    

    The result lines should look like this: 6988bec26b1503d45eb0b2e8a4364afb87dde7af:bla.xml: text of the line it found...

    You can then get more information like author, date, and diff using git show:

    git show 6988bec26b1503d45eb0b2e8a4364afb87dde7af
    
    0 讨论(0)
  • 2020-11-22 06:59

    To search for commit content (i.e., actual lines of source, as opposed to commit messages and the like), you need to do:

    git grep <regexp> $(git rev-list --all)
    

    git rev-list --all | xargs git grep <expression> will work if you run into an "Argument list too long" error.

    If you want to limit the search to some subtree (for instance, "lib/util"), you will need to pass that to the rev-list subcommand and grep as well:

    git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util
    

    This will grep through all your commit text for regexp.

    The reason for passing the path in both commands is because rev-list will return the revisions list where all the changes to lib/util happened, but also you need to pass to grep so that it will only search in lib/util.

    Just imagine the following scenario: grep might find the same <regexp> on other files which are contained in the same revision returned by rev-list (even if there was no change to that file on that revision).

    Here are some other useful ways of searching your source:

    Search working tree for text matching regular expression regexp:

    git grep <regexp>
    

    Search working tree for lines of text matching regular expression regexp1 or regexp2:

    git grep -e <regexp1> [--or] -e <regexp2>
    

    Search working tree for lines of text matching regular expression regexp1 and regexp2, reporting file paths only:

    git grep -l -e <regexp1> --and -e <regexp2>
    

    Search working tree for files that have lines of text matching regular expression regexp1 and lines of text matching regular expression regexp2:

    git grep -l --all-match -e <regexp1> -e <regexp2>
    

    Search working tree for changed lines of text matching pattern:

    git diff --unified=0 | grep <pattern>
    

    Search all revisions for text matching regular expression regexp:

    git grep <regexp> $(git rev-list --all)
    

    Search all revisions between rev1 and rev2 for text matching regular expression regexp:

    git grep <regexp> $(git rev-list <rev1>..<rev2>)
    
    0 讨论(0)
提交回复
热议问题