Searching your Git history for a string or regular expression

Git seems to always have the feature I need, but often I’m not able to find it right away. While searching for a commit where a certain constant had been removed, I stumbled upon the deprecated git-diff-grep, which, in its readme, led me to the solution I could have easily found by just reading the, you know, manual (i.e. man git-log) :)

-S<string>
    Look for differences that introduce or remove an instance of
    <string>.Note that this is different than the string simply
    appearing in diff output; see the pickaxe entry in gitdiffcore(7)
    for more details.

-G<regex>
    Look for differences whose added or removed line matches the given
    <regex>.

Knowing that, you can simply do the following:

$ git log -S"WHERE_DID_IT_GO"
$ git log -G"WHERE_DID.*"

If you want to see the commit’s diff as well, just supply the -p option.

Setting the Tab Width for the git-diff command

I usually work with a tab width of 4 when editing code, but git-diff (that is the *nix less program) seems to use a default tab width of 8. You can change this behavior by setting the core.pager option to something like this:

less -x4

This would result in a tab width of 4. If you are not familiar with git config, here’s how you set the option:

$ git config --global core.pager 'less -x4'

If you remove the --global flag, the option will be changed only for the current git repository, otherwise it will be changed globally for the current user account.

Keeping track of foreign codebase modifications with Git

I’ve been there a lot: I’m using some kind of existing framework / tool / open source software which is awesome, but there is that tiny little thing that annoys me, and I’d like to change it.

Continue reading