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.
Not sure why the accepted answer doesn't work in my environment, finally I run below command to get what I need
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:
--reverse is also helpful since you want the first commit that made the change:
This way older commits will appear first.
Messing around with the same answers:
Now you can do
or
You can do:
To find all commits that added or removed the fixed string
whatever
. The--all
parameter means to start from every branch and--source
means to show which of those branches led to finding that commit. It's often useful to add-p
to show the patches that each of those commits would introduce as well.Versions of git since 1.7.4 also have a similar
-G
option, which takes a regular expression. This actually has different (and rather more obvious) semantics, explained in this blog post from Junio Hamano.As thameera points out in the comments, you need to put quotes around the search term if it contains spaces or other special characters, for example:
Here's an example using
-G
to find occurrences offunction foo() {
:Mark Longair’s answer is excellent, but I have found this simpler version to work for me.