Last Updated: February 25, 2016
·
692
· greggroth

Trace Git History of a Particular Line

Sometimes you would like to see the full history of a line of code, however git-blame only shows the last commit that touched a line. Using the Rails repository as an example, say you want to see the commits (with the diff included) that touch the #create method defined in ActiveRecord::Persistence. You can use the following command:

git log -p -G'def create' activerecord/lib/active_record/persistence.rb

The key is the -G flag which takes a regular expression. The -p flag will generate the patch for each commit.