I want to be able to find a certain string which was introduced in any commit in
any branch, how can I do that? I found something (that I modified for Win32),
but git whatchanged
doesn't seem to be looking into the different branches
(ignore the py3k chunk, it's just a msys/win line feed fix)
git whatchanged -- <file> | \
grep "^commit " | \
python -c "exec(\"import sys,msvcrt,os\nmsvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)\nfor l in sys.stdin: print(l.split()[1])\")" | \
xargs -i% git show origin % -- <file>
It doesn't really matter if your solution is slow.
--reverse is also helpful since you want the first commit that made the change:
git log --all -p --reverse --source -S 'needle'
This way older commits will appear first.
Mark Longair’s answer is excellent, but I have found this simpler version to work for me.
git log -S whatever
Messing around with the same answers:
$ git config --global alias.find '!git log --color -p -S '
Now you can do
$ git find <whatever>
or
$ git find <whatever> --all
$ git find <whatever> master develop
git log -S"string_to_search" # options like --source --reverse --all etc
Pay attention not to use spaces between S and "string_to_search". In some setups (git 1.7.1), you'll get an error like:
fatal: ambiguous argument 'string_to_search': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
While this doesn't directly answer you question, I think it might be a good solution for you in the future. I saw a part of my code, which was bad. Didn't know who wrote it or when. I could see all changes from the file, but it was clear that the code had been moved from some other file to this one. I wanted to find who actually added it in the first place.
To do this, I used Git bisect, which quickly let me find the sinner.
I ran git bisect start
and then git bisect bad
, because the revision checked out had the issue. Since I didn't know when the problem occured, I targetted the first commit for the "good", git bisect good <initial sha>
.
Then I just kept searching the repo for the bad code. When I found it, I ran git bisect bad
, and when it wasn't there: git bisect good
.
In ~11 steps, I had covered ~1000 commits and found the exact commit, where the issue was introduced. Pretty great.
Not sure why the accepted answer doesn't work in my environment, finally I run below command to get what I need
git log --pretty=format:"%h - %an, %ar : %s"|grep "STRING"
Source: Stackoverflow.com