How do I get git to give me all revisions of a single file

159 Views Asked by At

I'm trying to figure out the last time a file had a certain string in it

I found this:

git rev-list --all | xargs git grep <search str> | grep <file name>

But that runs git grep against every single file that's ever been modified, then throws out all matches that aren't in the file of interest

So, it works, but it's really slow

How do I tell git rev-list to only give me revisions for a specified file?

I tried

git rev-list --all path/to/file.py

And it just gave me every file. So I tried each of these

git rev-list path/to/file.py
git rev-list -- path/to/file.py

But for both git returned help text

1

There are 1 best solutions below

2
jthill On

I'm trying to figure out the last time a file had a certain string in it

git log --oneline --no-merges -p -S 'that string' -- that/file

or if you want to know all the places that deletion was merged, too

git log --oneline -m -p -S 'that string' -- that/file

and check the patches for whether the text was deleted or added.


git rev-list doesn't default to the HEAD commit, you have to specify exactly where to start, that's why your later tries were failing.


From comments, you're not just looking for complete removal of a string from a file, you're looking for changes removing that string from particular places in a file. For those, use -G not -S. -G looks for that pattern appearing in diffs where -S ignores diffs that don't change the total number of occurrences -- -S does what you asked for, but not what you want.