git: when was a file introduced?

2022-01-09

 | 

~2 min read

 | 

374 words

How can you use Git’s CLI to figure out when a file was initially introduced? When doing so, how do you handle renames?

Think of this as another tool in the tool box for git archeology.

A few different solutions were proposed in this Stack Overflow conversation on the topic:

  1. git log --diff-filter=A -- foo.js
  2. git log --follow --diff-filter=A --find-renames=40% foo.js
  3. git log --follow --diff-filter=A --find-renames=40% foo.js | tail -n 1

Let’s look at what each of these options do to understand when we would want to use which solution:

Diff-Filter

The diff filter selects only files that match the filter.

While there are a number of filters, in this case we are only interested in files that were added.

Follow

Per the documentation, follow: “Continue[s] listing the history of a file beyond renames (works only for a single file).”

In our case, then, this means that we’ll be able to track changes in a file name as it goes from baz.js to foo.js.

Find Renames

If you’ve ever moved a file in Git and then made a number of changes to the file before you committed, you might have noticed than instead of a rename, you wound up with a new addition to the git log as well as a deletion.

Find Renames is a way to address that by setting the sensitivity of similarity required to be considered a rename.

For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed. Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.

Pipe To Tail Operator

The third operation has includes a pipeline to tail -n 1. Why?

This handles situations where a file was added, deleted, and then subsequently added again in a different commit (i.e., not a rename).

In this situation, we might get multiple results with the previous approaches. With this tail, we’ll take only the most recent version.



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!