Git: Show commits that have touched specific text in a file

If you want to find the commits that touched a specific text in a file, use

git log -S 'text in the code' -- path/to/file

If you use tig you may run a similar command to get a navigatable list of affected files:

tig -S'text in the code'

Example

Here is an example, where the move of the convert_number_column_value(value) method in active record is traced (simplified output):

git log -n 1 --pretty=oneline -S 'convert_number_column_value(value)' -- activerecord/lib/active_record/base.rb
ceb33f84933639d3b61aac62e5e71fd087ab65ed Split out most of the AR::Base code into separate modules :cake:
git show ceb33f84933639d3b61aac62e5e71fd087ab65ed

 activerecord/lib/active_record/base.rb
 -      def convert_number_column_value(value)		
 -        if value == false		
 -          0		
 -        elsif value == true		
 -          1		
 -        elsif value.is_a?(String) && value.blank?		
 -          nil		
 -        else		
 -          value		
 -        end		
 -      end

 activerecord/lib/active_record/attribute_methods/write.rb
 +        def convert_number_column_value(value)
 +          if value == false
 +            0
 +          elsif value == true
 +            1
 +          elsif value.is_a?(String) && value.blank?
 +            nil
 +          else
 +            value
 +          end
 +        end
git log -n 1 --pretty=oneline -S 'convert_number_column_value(value)' -- activerecord/lib/active_record/attribute_methods/write.rb
5dec3dd59c9f4b5c881860b0f629c8d6b3706f90 delegate attribute typecasting to the column
git show 5dec3dd59c9f4b5c881860b0f629c8d6b3706f90

 activerecord/lib/active_record/attribute_methods/write.rb
 -        def convert_number_column_value(value)		 
 -          if value == false		
 -            0		
 -          elsif value == true		
 -            1		
 -          elsif value.is_a?(String) && value.blank?		
 -            nil		
 -          else		
 -            value		
 -          end

 activerecord/lib/active_record/connection_adapters/column.rb
 +      def type_cast_for_write(value)
 +        return value unless number?
 +
 +        if value == false
 +          0
 +        elsif value == true
 +          1
 +        elsif value.is_a?(String) && value.blank?
 +          nil
 +        else
 +          value
 +        end
 +      end
Emanuel De almost 5 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more