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

后端 未结 15 1457

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

    git log can be a more effective way of searching for text across all branches, especially if there are many matches, and you want to see more recent (relevant) changes first.

    git log -p --all -S 'search string'
    git log -p --all -G 'match regular expression'
    

    These log commands list commits that add or remove the given search string/regex, (generally) more recent first. The -p option causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.

    Having found a relevant commit that adds the text you were looking for (for example, 8beeff00d), find the branches that contain the commit:

    git branch -a --contains 8beeff00d
    

提交回复
热议问题