I want to see who made a change to a file and when. There's a comment written in broken English that is unattributed. I'd like to search (binary search?) through all of the commits to this file for the first instance of that comment. Can this even be done in an automated fashion?
Asked
Active
Viewed 114 times
2
-
2Have you tried a simple git blame? – Mark Silverberg Aug 09 '14 at 02:25
-
Besides `git blame` which might be best for this problem, there is also `git grep`, see for example: http://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history – eckes Aug 09 '14 at 02:30
-
I thought of git grep, which is a great option that some folks don't know. However given the requirements of knowing who the editor is and the instance of first edit, it didn't give me what I needed for this question. Good tool to know all the same. – Michael Durrant Aug 09 '14 at 02:37
1 Answers
4
git blame filename | grep [string]
will do it
Within the repo do git blame filename
Example:
$ git blame app/models/link.rb
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 1) class Link < ActiveRecord::Base
5252167d (Michael Durrant 2012-09-03 21:39:48 -0400 2)
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 3) belongs_to :group
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 4) validates_presence_of :url_address
^772df05 (Michael Durrant 2011-04-23 23:12:24 -0400 5) validates_presence_of :group_id
...
So to search for a string, just grep
for it:
$ git blame app/models/link.rb | grep the_dt
00000000 (Not Committed Yet 2014-08-08 22:29:15 -0400 20) def verified_date=(the_dt)
00000000 (Not Committed Yet 2014-08-08 22:29:15 -0400 21) verified_date=the_dt
If you just want the last line(first instance), use tail
:
$ git blame app/models/link.rb | grep url | tail -1
00000000 (Not Committed Yet 2014-08-08 22:33:19 -0400 26) def verify_url
There is also git log -S
, e.g.
Note that you have to use lower case even if you are searching for upper case (as in pipeline/Pipeline in this example).
$ git log -Spipeline
commit c0fdeb8a603dd6f61e487ff4b5f7de4df4d43677
Author: Michael Durrant <m2@snap2web.com>
Date: Sun Feb 23 19:33:09 2014 -0500
Switch application to Asset Pipeline (many changes).
There is also git log -G
which lets you use a regular expression.
This is useful even if you just have spaces in the search with string, e.g.
$ git log -Gasset.*pipeline
commit c0fdeb8a603dd6f61e487ff4b5f7de4df4d43677
Author: Michael Durrant <m2@snap2web.com>
Date: Sun Feb 23 19:33:09 2014 -0500
Switch application to Asset Pipeline (many changes).

Michael Durrant
- 93,410
- 97
- 333
- 497
-
2You can also use the `-L` option to narrow the output to a specific line or lines matching a regex instead of using grep – JKillian Aug 09 '14 at 02:31