How to find the commit in which a given file was added?

Viewed 68544

Say I have a file foo.js that was committed some time ago. I would like to simply find the commit where this file was first added.

After reading the answers and my own tinkering, this works for me

git log --follow --diff-filter=A --find-renames=40% foo.js
5 Answers

If it's an alternative to do it non-programatically, this is very quick. Open gitk GUI.

gitk file

Then just scroll to the first commit

The above answers worked for me on files in the root directory of the database with no spaces in the filename. However it didn't work on files in a subfolder with spaces in the filename.

I am a GitLab user, so I was able to browse for the file and then click on the file name, and on the next page click "History". Then I could find the first commit in the file's history.

Related