When exploring an unfamiliar part of a repo, I often find it somewhat time-consuming to figure out which files are related to which files. I whipped up a little shell script to help me with that.

git log --follow --format=format:%H $1 |

xargs -L1 git diff-tree --no-commit-id --name-only -r |

sort | uniq -c |

sort -r

The script takes in a filepath, grabs all the commits from the history of that file, lists the other filepaths that were touched by each of those commits, then sorts them by the number of times those filepaths were touched.

Here’s an example I pulled from work (with filepaths replaced with nonspecific filepaths).

$ git-related ./app/models/foo/bar.rb | head -n 5

18 app/models/foo/bar.rb

7 app/models/foo/foo.rb

5 config/baz.rb

5 app/views/qux/foo.haml

5 app/views/layouts/foo.haml

Now, before you squawk about how related files should be in related places in the file tree, just remember that none of us are perfect developers. And that some of us are Rails developers.

Anyway, I think it’s pretty handy. Feel free to copy the source here or from my GitHub.