readonly
readonly

Reputation: 355474

Using git to grep through a file's previous versions?

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?

Upvotes: 33

Views: 5271

Answers (2)

CaptainPicard
CaptainPicard

Reputation: 973

This will print any commits where the diff contains xyz

git log -Sxyz foo.c

Same thing but more readable:

git log -S "xyz" foo.c

Upvotes: 44

Nwyfiant
Nwyfiant

Reputation: 109

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.

Upvotes: 10

Related Questions