Cory Klein Cory Klein - 1 year ago 87
Git Question

How do I find the most recent git commit that modified a file?

I want to find the most recent commit that modified a source file.

I can use

git blame
to see all the dates for commits by each line, but it’s difficult to see exactly which commit was the last one to touch the file.

How can I find the last commit that touched a given file in my git repository?

Answer Source

git log supports looking at the history of specific files (and directories), so you can write this:

git log my/file.c

If you really only want to list the one most recent commit, for example to use it in a script, you can use -n 1:

git log -n 1 --pretty=format:%h -- my/file.c

--pretty=format:%h tells git log to show only the commit hash (thanks to the TheBamf). The -- separater stops the file name from getting interpreted as a commit name, just in case it's ambiguous.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download