Is there a command that would allow me to check if the string "xyz"
was ever in file foo.c
in the repository and print which revisions they were found in?
Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression. For the examples that follow, we'll search through the source code for Git itself.
git checkout recovers old versions of files.
This will print any commits where the diff contains xyz
git log -Sxyz foo.c
This will print any commits where the diff contains xyz. Note the --
separating the filename from the rest of the command.
git log -Sxyz -- foo.c
Without the --
, I get this error:
fatal: ambiguous argument 'foo.c': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
I originally wanted to comment on CaptainPicard's answer to add a correction, but I don't have sufficient reputation yet. If someone would like to edit that answer to mention this correction I'll be happy to take this answer down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With