Mus
Mus

Reputation: 7540

How can I find the file that a particular line of code existed within in my commit history?

There is a line of code that I wish to recover from a previous commit as it turns out that this line of code was very important and useful.

For example, let's say that this is my line of code:

df[df$type == "creator", ]

The problem is that I can't remember the file that this code was saved in, nor can I recall where/when it was committed.

I use SourceTree and have tried the Search option, but it only gives me the option to search Commit Message, File Changes and Authors.

I use the File Changes search option but the results show me previous commits whereas I would like to see a list of files that contained the line of code at some point in time.

Aside from manually looking through each version of each file in each commit, I'm not sure how to approach it.

How can I do this?

Note that I am also open to suggestions/solutions using the terminal also.

Update: The line of code can be in either one of three files and I know roughly the time it was committed (about two weeks ago).

Upvotes: 0

Views: 34

Answers (1)

David Ferenczy Rogožan
David Ferenczy Rogožan

Reputation: 25451

Try git log, for example:

git log --name-only -S"<text-you-search-for-goes-here>"

It will print all commits in which any file contained the searched text at any point in time and for each commit it displays:

  • commit hash
  • author
  • date of the commit
  • commit message
  • names of files in which the searched text appeared (thanks to --name-only)

Upvotes: 2

Related Questions