在旧的git提交中从旧名称中查找HEAD中的文件

时间:2017-06-08 07:12:24

标签: git

说我知道旧提交中文件的名称。但从那以后,它被重命名或移动了。如何在HEAD修订版中找到它,如何从已知的旧提交到现在的HEAD查看其历史记录?

例如,我知道在Linux内核版本3.8中,有一个文件drivers/usb/gadget/u_serial.c。但是在最近的内核中,我看到该文件不再存在。我希望从3.8中知道的点到目前的HEAD看到文件的历史。

1 个答案:

答案 0 :(得分:0)

它有点草率,但以下脚本将从不再存在的文件开始跟踪重命名历史记录:

#!/bin/sh

file=$1

while true
do
    # find the commit in which $file disappeared
    revhash=$(git log --pretty=%H --diff-filter=D -n 1 $revspec -- $file)

    # break out if $file has not disappeared since $revhash
    test -z "$revhash" && break
    revspec=${revhash}..
    echo -n "$revhash renamed $file => "

    # $revhash corresponds to the commit in which $file was removed, i.e. renamed
    # so we combine grep, cut, sed, and diff-tree to figure out what it was renamed to
    file=$(git diff-tree -M --numstat $revhash | grep '=>' | cut -f3- | sed "s/\(.*\){\(.*\) => \(.*\)}\(.*\)/\1\2\4 => \1\3\4/;s/\/\//\//g" | grep $file | sed "s/.* => //")
    echo "$file"
    test -z "$file" && echo -e "???\nNo rename target found, presumably file was deleted." && break
done

然后您可以git log --follow返回给您的最后一个文件名来跟踪其历史记录。