2

I am debugging an app I am working on and noticed that some code is missing that should be there. I am pretty sure that it existed at one point in time and was accidentally (or maybe purposefully) deleted a while ago. I want to know how I can search a commit history to see if the code ever existed. When I ran the gitk command it brought up a GUI with a search function, but I found that the search only looked at commit messages and not the content of the commit. How can I search for the missing code in version control?

Tyler Hilbert
  • 2,107
  • 7
  • 35
  • 55

1 Answers1

3

You can use the git log with the -S option which will look for a given string in all the commits.

git log -Sfunction_name

mrida
  • 1,157
  • 1
  • 10
  • 16
  • Did you mean to type git log -S function_name? it looks like you missed a space. – Tyler Hilbert Oct 07 '16 at 16:16
  • use the text you need to look for instead of the "function_name". It doesn't include a space though. – mrida Oct 07 '16 at 16:18
  • This link might be helpful https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History – mrida Oct 07 '16 at 16:18
  • 2
    @TylerHilbert: `-Swhatever` and `-S whatever` mean the same thing in Git (the parsing of switches in Git is pretty complicated, sometimes you can omit spaces, sometimes it's `--foo=bar` or `--foo bar`, in a few cases `--foo` has acquired an *optional* `=bar` so there's no `--foo bar` form, and so on; so it's just a big mishmash of special cases, although there are general rules that *mostly* work.) – torek Oct 07 '16 at 22:27