找出从哪个git提交文件 [英] find out which git commit a file was taken from

查看:120
本文介绍了找出从哪个git提交文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用版本控制的协作者向我发送了一些本地修改的文件。现在他休假了。我想知道他的编辑是基于哪个版本的。



我认为最有前途的方法是迭代最近的提交并检查diff的长度是最小的。

有没有一些现成的功能,或者我需要自己编写代码?不是自己的git专家,最有希望的方法是什么?

解决方案

我不知道任何标准的git命令可以做到这一点。但是一个简单的脚本可以帮助这项任务。首先,创建一个 tmp-branch 并将该文件提交到该分支。然后创建一个如下所示的简单脚本来打印该文件与该文件的最新版本中的每个版本的差异。

 <$ c 

BRANCH =tmp-branch
FILE =path / to / file.txt
RECENT_COMMITS = $(git rev- list -50 master - $ FILE)

在$ RECENT_COMMITS中提交COMMIT

echo -n$ COMMIT:
git diff $ BRANCH $ COMMIT - -shortstat - $ FILE
完成

不完全自动,但它会给你输出如下所示。在此输出中,您可以识别最少更改的版本。在我的示例中,我用作示例的简单更改基于 edff0c0

  e2b2c157a81e0523e7d4a0a52df79cb4fce981ac:1个文件已更改,12个插入(+),16个删除( - )
154d84736f4df3dd968450599dc254cda56f2057:1个文件已更改,12个插入(+),13个删除( - )
ba11ecc3a4d8268f43589fb929f0877e65879f13:1个文件改变的,11个插入(+),13个缺失( - )
017a7a5abdffeb37671a03c0db2e32c37b0ee6bd:1个文件改变时,8个插入(+),9个缺失( - )
cc97d3453ebde37b02a42ca7263bf7a983222d4d:1文件改变时,8个插入(+), 5个删除( - )
a84adb9e337d2cf1e851924cf27f5f0bfdca790f:1个文件已更改,7个插入(+),4个删除( - )
9a3c10cefc133792377851b1b5cb8a69d3ffd788:1个文件已更改,7个插入(+),3个删除( - )
edff0c0155b77e39599402574ba1c4aa02c1bbac:1个文件改变时,6个插入(+),2个缺失( - )
413800ab0de606548c0c69b4b35e50b527d33d7f:1个文件改变时,13个插入(+),2个缺失( - )
af689f1d6 d76303d8e39311f48a977b87260586e:1个文件已更改,13个插入(+),2个删除( - )
25123d4196533a0f3ce718a288bc3c5d975ad865:1个文件已更改,24个插入(+),3个删除( - )
e7ca01b247f7e32010f256b55696c3ecb1d72144: (+),5个缺失( - )
6e9c2a561cc606f34ccb2cc918b297187c2e8c42:1文件改变时,33个插入(+),23个缺失( - )

我不确定这种方法是否万无一失。你也许应该看看周围的一些提交。


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 based on.

I suppose the most promising approach is to somehow iterate over the most recent commits and check for which the length of the diff is minimal.

Is there some ready-made functionality for this, or would I need to code this myself? Not being a git expert myself, what would be the most promising way to proceed?

解决方案

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.

这篇关于找出从哪个git提交文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆