find out which git commit a file was taken from

前端 未结 2 919
夕颜
夕颜 2021-02-20 08:41

A collaborator that does not use version control has sent me a file with some local modifications. Now he went on vacation. I would like to find out which version his edits were

2条回答
  •  伪装坚强ぢ
    2021-02-20 09:27

    I do not know about any standard git command to do this. But a simple script could aid this task. First, create a tmp-branch and commit the file to this branch. Then create a simple script like the one below to print how much different the file is from each of the 50 most recent versions of that file.

    #!/bin/bash
    
    BRANCH="tmp-branch"
    FILE="path/to/file.txt"
    RECENT_COMMITS=$(git rev-list -50 master -- $FILE)
    
    for COMMIT in $RECENT_COMMITS
    do
        echo -n "$COMMIT: "
        git diff $BRANCH $COMMIT --shortstat -- $FILE
    done
    

    Not fully automatic, but it would give you output like the following. In this output you identify the version with the least changes. In my example, the simplistic change i used as an example was based on edff0c0.

    e2b2c157a81e0523e7d4a0a52df79cb4fce981ac:  1 file changed, 12 insertions(+), 16 deletions(-)
    154d84736f4df3dd968450599dc254cda56f2057:  1 file changed, 12 insertions(+), 13 deletions(-)
    ba11ecc3a4d8268f43589fb929f0877e65879f13:  1 file changed, 11 insertions(+), 13 deletions(-)
    017a7a5abdffeb37671a03c0db2e32c37b0ee6bd:  1 file changed, 8 insertions(+), 9 deletions(-)
    cc97d3453ebde37b02a42ca7263bf7a983222d4d:  1 file changed, 8 insertions(+), 5 deletions(-)
    a84adb9e337d2cf1e851924cf27f5f0bfdca790f:  1 file changed, 7 insertions(+), 4 deletions(-)
    9a3c10cefc133792377851b1b5cb8a69d3ffd788:  1 file changed, 7 insertions(+), 3 deletions(-)
    edff0c0155b77e39599402574ba1c4aa02c1bbac:  1 file changed, 6 insertions(+), 2 deletions(-)
    413800ab0de606548c0c69b4b35e50b527d33d7f:  1 file changed, 13 insertions(+), 2 deletions(-)
    af689f1d6d76303d8e39311f48a977b87260586e:  1 file changed, 13 insertions(+), 2 deletions(-)
    25123d4196533a0f3ce718a288bc3c5d975ad865:  1 file changed, 24 insertions(+), 3 deletions(-)
    e7ca01b247f7e32010f256b55696c3ecb1d72144:  1 file changed, 26 insertions(+), 5 deletions(-)
    6e9c2a561cc606f34ccb2cc918b297187c2e8c42:  1 file changed, 33 insertions(+), 23 deletions(-)
    

    I'm not sure if this method is foolproof. You should probably have a look at a couple of the surrounding commits also.

提交回复
热议问题