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

后端 未结 15 1458

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条回答
  •  猫巷女王i
    2020-11-22 06:45

    You should use the pickaxe (-S) option of git log.

    To search for Foo:

    git log -SFoo -- path_containing_change
    git log -SFoo --since=2009.1.1 --until=2010.1.1 -- path_containing_change
    

    See Git history - find lost line by keyword for more.


    As Jakub Narębski commented:

    • this looks for differences that introduce or remove an instance of . It usually means "revisions where you added or removed line with 'Foo'".

    • the --pickaxe-regex option allows you to use extended POSIX regex instead of searching for a string. Example (from git log): git log -S"frotz\(nitfol" --pickaxe-regex


    As Rob commented, this search is case-sensitive - he opened a follow-up question on how to search case-insensitive.

提交回复
热议问题