Selfy
Selfy

Reputation: 133

Search all previous Git commits of file for who added a (mispelled) string

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?

Upvotes: 2

Views: 120

Answers (1)

Michael Durrant
Michael Durrant

Reputation: 96454

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 <[email protected]>
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 <[email protected]>
Date:   Sun Feb 23 19:33:09 2014 -0500

    Switch application to Asset Pipeline (many changes).

Upvotes: 4

Related Questions