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

后端 未结 15 1211

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 07:01

    For anyone else trying to do this in Sourcetree, there is no direct command in the UI for it (as of version 1.6.21.0). However, you can use the commands specified in the accepted answer by opening Terminal window (button available in the main toolbar) and copy/pasting them therein.

    Note: Sourcetree's Search view can partially do text searching for you. Press Ctrl + 3 to go to Search view (or click Search tab available at the bottom). From far right, set Search type to File Changes and then type the string you want to search. This method has the following limitations compared to the above command:

    1. Sourcetree only shows the commits that contain the search word in one of the changed files. Finding the exact file that contains the search text is again a manual task.
    2. RegEx is not supported.
    0 讨论(0)
  • 2020-11-22 07:05

    Jeet's answer works in PowerShell.

    git grep -n <regex> $(git rev-list --all)
    

    The following displays all files, in any commit, that contain a password.

    # Store intermediate result
    $result = git grep -n "password" $(git rev-list --all)
    
    # Display unique file names
    $result | select -unique { $_ -replace "(^.*?:)|(:.*)", "" }
    
    0 讨论(0)
  • 2020-11-22 07:09

    For simplicity, I'd suggest using GUI: gitk - The Git repository browser. It's pretty flexible

    1. To search code:

    2. To search files:

    3. Of course, it also supports regular expressions:

    And you can navigate through the results using the up/down arrows.

    0 讨论(0)
提交回复
热议问题