Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find out which git commit a file was taken from

Tags:

git

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?

like image 531
carsten Avatar asked Jul 15 '15 09:07

carsten


People also ask

How do you check details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How do I find a specific commit in git?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do I see the commit history of a branch?

git log --oneline is a great way to view commit history by displaying the first seven characters of the SHA-1 hash and commit message of the commits on the current branch. git log --oneline --graph presents commit history in a ASCII graph displaying the different branches in the repository and their commits.


2 Answers

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.

like image 121
Alderath Avatar answered Oct 16 '22 03:10

Alderath


I'd create a new branch with the colleague's change and then use git merge-base:

git merge-base finds best common ancestor(s) between two commits to use in a three-way merge. One common ancestor is better than another common ancestor if the latter is an ancestor of the former. A common ancestor that does not have any better common ancestor is a best common ancestor, i.e. a merge base. Note that there can be more than one merge base for a pair of commits.

like image 37
Jiri Kremser Avatar answered Oct 16 '22 02:10

Jiri Kremser