2

I tried using git grep -i 'search' README.md and the output found some lines I was interested in looking at, but these lines did not print out the git sha's so that I could get more information about these commits. The output looked like this:

README.md:ElastiCache, ElasticSearch, and RDS setup with Terraform

How do I see the git sha for this line? I looked at the documentation and it didn't even contain the word "commit" or "sha" in it.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    I believe your current use of `git grep` will only search the working directory and blobs. If you want to search actual commit history, you will need a [more complex command](http://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history). – Tim Biegeleisen Jun 08 '16 at 00:57
  • I don't think you understand quite what `git grep` does. It's really just a `grep -r` that excludes the `.git` directory. Perhaps you're looking for `git log --grep` or `git log -S`. – Mort Jun 08 '16 at 01:25

2 Answers2

2

Without additional specifications, git grep just looks at the current commit.

To look in other commit(s) (or the index), you must name them (or use --cached). For instance, compare:

$ git grep asdf
Documentation/rev-list-options.txt:  ``asdf'', and a file `quux` exists with con
t/t5516-fetch-push.sh:  test_must_fail git push >.git/bar --porcelain asdfasdfas
t/t9100-git-svn-basic.sh:       echo asdf > dir &&
t/t9132-git-svn-broken-symlink.sh:asdf
t/t9132-git-svn-broken-symlink.sh:test_expect_success SYMLINKS '"bar" is a symli
t/t9132-git-svn-broken-symlink.sh:      (cd x && test xasdf = x"$(git cat-file b

vs:

$ git grep asdf HEAD^ HEAD~3
HEAD^:Documentation/rev-list-options.txt:  ``asdf'', and a file `quux` exists wi
HEAD^:t/t5516-fetch-push.sh:    test_must_fail git push >.git/bar --porcelain as
HEAD^:t/t9100-git-svn-basic.sh: echo asdf > dir &&
HEAD^:t/t9132-git-svn-broken-symlink.sh:asdf
HEAD^:t/t9132-git-svn-broken-symlink.sh:test_expect_success SYMLINKS '"bar" is a
HEAD^:t/t9132-git-svn-broken-symlink.sh:        (cd x && test xasdf = x"$(git ca
HEAD~3:Documentation/rev-list-options.txt:  ``asdf'', and a file `quux` exists w
HEAD~3:t/t5516-fetch-push.sh:   test_must_fail git push >.git/bar --porcelain as
HEAD~3:t/t9100-git-svn-basic.sh:        echo asdf > dir &&
HEAD~3:t/t9132-git-svn-broken-symlink.sh:asdf
HEAD~3:t/t9132-git-svn-broken-symlink.sh:test_expect_success SYMLINKS '"bar" is 
HEAD~3:t/t9132-git-svn-broken-symlink.sh:       (cd x && test xasdf = x"$(git ca

If you list multiple trees (or commit IDs) to search, the results have your specifiers prefixed, so that you know which one they go with.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Is there a way to do this with a range of commits, i.e. `git grep asdf HEAD^..HEAD~3`? – Jeff Puckett Jun 08 '16 at 03:39
  • @JeffPuckettII Not very well with `git grep`; for that you'd want `git log` with `-S` or `-G`, usually. Though I'll note that you can use `git rev-list` to make arbitrarily large commit-ID lists and pass those to `git grep` with `$(...)` or xargs... – torek Jun 08 '16 at 04:07
1

git blame README.md | grep -i 'search'

ElpieKay
  • 27,194
  • 6
  • 32
  • 53