简体   繁体   English

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

[英]Find a file in HEAD from its old name in an old git commit

Say I know the name of a file in an old commit. 假设我知道旧提交中的文件名。 But since then, it's been renamed or moved. 但是从那以后,它被重命名或移动了。 How can I find it in the HEAD revision, and how can I look at its history from the known old commit up to the present HEAD? 如何在HEAD版本中找到它,如何查看从已知旧提交到当前HEAD的历史?

For example, I know in Linux kernel version 3.8, there was a file drivers/usb/gadget/u_serial.c . 例如,我知道在Linux内核版本3.8中,有一个文件drivers/usb/gadget/u_serial.c But in more recent kernels I see that file is no longer there. 但是在较新的内核中,我看到该文件不再存在。 I'd like to see the file's history from the point I knew in 3.8, up to the present HEAD. 我想从3.8到现在的HEAD的角度查看文件的历史记录。

It's a bit sloppy, but the following script will trace the rename history starting from a file which no longer exists: 这有点草率,但是以下脚本将从一个不再存在的文件开始跟踪重命名历史记录:

#!/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

and then you can git log --follow the last filename that's returned to you to trace its history. 然后,您可以git log --follow返回给您的最后一个文件名来跟踪其历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM