Git Tips, Tricks and Notes

Find all commits that added/deleted the given string

$ git log -p -S 'api_key =' --source --all


Find all commits that added/deleted the given string, excluding the specified directories

$ git log -p -S some_method -- . ":(exclude)vendor/*"

Get the git blame of the previous commit for a given line

# git blame -L{LINE_NUMBER},+{CONTEXT} SHA^ -- {FILE_PATH}
$ git blame -L165,+1 5c2eb904^ -- app/models/public_body.rb


Run specs on each commit of a branch

$ git rev-list --reverse develop..feature_x | while read rev; do git co $rev && git clean -fd && bin/rspec; done; git co feature_x 


Show which files have been changed between two branches in a given directory

$ git diff --name-status master..release/0.22 app/views/

Show commits in current branch not in master

$ git log --cherry-pick --topo-order --reverse master...


Show commits in the current branch that modify a given file

# Assuming you're on a feature branch created from master
$ git log --follow master.. -- path/to/file