Quantcast
Viewing latest article 22
Browse Latest Browse All 53

Git Tricks: Find Most Recent Occurrence

Git aficionados will know of git grep, which is effectively just grep, limited to tracked files. This avoids the auto-generated crud that tends to pollute large projects, speeds up the search, and is generally easier to deal with.

Sometimes, though, it's useful to be able to do a textual search on the history as well - perhaps you want to find the last commit that had a certain identifier in it. Easy enough: git grep optionally takes a <tree> specifying in what revision you want to run the search.

$ git grep someIdent 54f6ebba

Couple that with rev-list --all (which prints out all revisions in reverse chronological order), and you can quickly search backwards through history to find the most recent occurrence of a string. For instance, to find when a Makefile (notable for having "all:") was last present:

$ for r in `git rev-list --all`;doif git grep all: $r;thenbreak;fi;done

You can find the earliest such revision instead by passing --reverse to rev-list. Or maybe you only want to search tagged releases (which is faster, and probably good enough):

$ for t in `git tags`;doif git grep myIdent $t;thenbreak;fi;done

If you want to go back to reverse chronological order (tags defaults to chronological), and you're on a machine with GNU's coreutils, just pipe the output of tags through tac.


Viewing latest article 22
Browse Latest Browse All 53

Trending Articles