Using Git, how could I search for a string across all branches?

前端 未结 5 2034
一个人的身影
一个人的身影 2020-12-22 15:25

Using Git, how could I search within all files in all local branches for a given string?

GitHub specific: is it possible to perform the above search across all GitHu

5条回答
  •  暖寄归人
    2020-12-22 16:02

    In many cases git rev-list --all can return a huge number of commits, taking forever to scan. If you, instead of searching through every commit on every branch in your repository history, just want to search all branch tips, you can replace it with git show-ref --heads. So in total:

    git grep "string" `git show-ref --heads`
    

    or:

    git show-ref --heads | xargs git grep "string"
    

    Tip: You can write output in file to view in an editor:

    nano ~/history.txt
    git show-ref --heads | xargs git grep "search string here" >> ~/history.txt
    

提交回复
热议问题