Use --follow option in git log to view a file's history
I’ve always used the following command to check the history of a particular file in a repo:
But imagine that this file used to be a plain JS file sometime in the past, and had got renamed to TS. In this case, the first commit would be a single file-creation commit, even though a mostly-similar file had existed before. In these cases, I’d change the command to something like this:
This lists out the commits of both the files. Still, the first commit of the TS file would be a single creation commit, and the last commit of the JS file would be a single deletion one.
A better way to make git logfollow the history of a particular file is to use the command-line switch…er…--follow and turn on the --find-renames (or -M shorthand) option:
Sometimes the changes are too much for git to detect a particular change as a rename. In those cases, you can further hint to Git specifying the similarity of a change to be considered as a rename. This is passed as a percentage to the -M/--find-renames option.
It’s kind of fascinating the things I get to learn every day in Git, even if I think/feel I’m pretty fluent in it. I had been using a command that did most of what I wanted to do, but one more flag would’ve made it much easier!