Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 607
readonly Avatar asked Sep 18 '08 00:09

readonly


People also ask

What files can be searched in using git grep `?

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.

Which command is used to explore the previous versions of a project?

git checkout recovers old versions of files.


2 Answers

This will print any commits where the diff contains xyz

git log -Sxyz foo.c 
like image 67
CaptainPicard Avatar answered Oct 11 '22 15:10

CaptainPicard


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.

like image 36
Nwyfiant Avatar answered Oct 11 '22 15:10

Nwyfiant