从旧git提交中的旧名称在HEAD中查找文件
假设我知道旧提交中的文件名。但从那时起,它被重命名或移动。我怎样才能在HEAD修订版中找到它,怎样才能看到它从已知的旧版本到现在的HEAD的历史从旧git提交中的旧名称在HEAD中查找文件,git,Git,假设我知道旧提交中的文件名。但从那时起,它被重命名或移动。我怎样才能在HEAD修订版中找到它,怎样才能看到它从已知的旧版本到现在的HEAD的历史 例如,我知道在Linux内核版本3.8中,有一个文件drivers/usb/gadget/u_serial.c。但在最近的内核中,我看到该文件不再存在。我想查看从我在3.8中知道的点到当前头部的文件历史记录。这有点草率,但以下脚本将跟踪从不再存在的文件开始的重命名历史记录: #!/bin/sh file=$1 while true do #
例如,我知道在Linux内核版本3.8中,有一个文件
drivers/usb/gadget/u_serial.c
。但在最近的内核中,我看到该文件不再存在。我想查看从我在3.8中知道的点到当前头部的文件历史记录。这有点草率,但以下脚本将跟踪从不再存在的文件开始的重命名历史记录:
#!/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--跟踪返回给您的最后一个文件名以跟踪其历史记录。您可能认为可以在git log
中将--follow
与--reverse
相结合。您应该能够,但不能。git log--diff filter=D--drivers/usb/gadget/u_serial.c
。如果知道当前名称,则需要使用--follow和--diff filter=R
。它同时适用于--follow
和--diff filter=RD
,无论--
后面跟的是它的旧名称还是当前名称。