Searching for a word in git code commits
2013-05-22
Sometimes you need to find when a certain piece of code was first introduced into your code base. Perhaps you're wondering who wrote the first version of a certain function. It's pretty easy to find what you're looking for with git's log command. All you need to do is use the -S
flag with it:
git log -SmyFunction
This will return all the git commits that introduce or remove the string "myFunction". Here is git's manual entry on the flag:
-S<string>
Look for differences that introduce or remove an instance of <string>. Note that this is different than the string simply appearing in diff
output; see the pickaxe entry in gitdiffcore(7) for more details.
Once you have the commits that you're looking for, you can take a close look at them with git show
:
git show aae88c084c5b1c888f1e26a5b327739a3825d1df
where "aae88c084c5b1c888f1e26a5b327739a3825d1df" is the SHA of the commit you want to inspect. This will show all the files that were changed in the commit and a diff of each version, before and after the commit.