Your approach may fail to work in case of insignificant differences (e.g. line-ending style, or differences due to clean/smudge filters) between the local and repository versions of the file.
The following script works via git diff
rather than relying on hashes. It accepts diff options after the file name.
Usage examples:
# list all commits that introduce the file README.md in its local state
list_introducing_commits README.md
# list all commits that introduce the file README.md in its local state
# ignoring any difference in whitespace
list_introducing_commits README.md -w
list_introducing_commits (couldn't find a better name):
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: $(basename $0) path/to/file [<diff-options>]"
exit 1
fi
file="$1"
shift 1
for rev in $(git log --pretty=%H -- "$file")
do
if git diff --exit-code $@ $rev -- $file &> /dev/null
then
echo $rev
fi
done