Home > Software design >  How do I get git to give me all revisions of a single file
How do I get git to give me all revisions of a single file

Time:02-24

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

CodePudding user response:

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.

CodePudding user response:

As you can see from the help text...

usage: git rev-list [OPTION] <commit-id>... [ -- paths... ]

...you can't run git rev-list without providing a commit id (or something that resolves to a commit id) as the first argument. For example, running git rev-list HEAD path/to/file.py would show you all commits that modified the file on the current branch.

If you're looking for all commits that modified the file in all branches, you could use git for-each-ref to get a list of branches, and then use the output of that command to drive the git rev-list command:

git for-each-ref refs/heads/\* --format='%(objectname)' |
xargs -iCOMMIT git rev-list COMMIT path/to/file.py |
sort -u

(The sort -u is necessary because given revisions will show up in multiple branches.)

  • Related