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

后端 未结 15 1461

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:56

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

    git rev-list --all | xargs git grep 
    

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

    git rev-list --all | xargs -I{} git grep  {} -- "*.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
    

提交回复
热议问题