How do I see the commit sha when I use git grep?

1k Views Asked by At

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.

2

There are 2 best solutions below

2
On

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

2
On

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.