1

I have a file in my repo which has been moved and changed several times since I created it, and I'm trying to find the original version. I know where it was, so I would like to grep the location in a contatenated list of all git diffs since the creation of the repo.

Note that I have removed this file from this repo to put it in a more appropriate one. I guess that can make it harder... Is there an easy way to find the commit where this file was introduced even though it does not exist any more ?

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Please explain: You're trying to find the original version, but you say you know where it was. So what is it you're trying to find? – Adi Levin Jan 16 '16 at 17:18
  • @AdiLevin I am trying to find the commit which first moved it to another location because I believe somehting wrong happened. I'm looking for something like `old/path/file -> new/path/file`. This kind of things shows in `git show`, which is why I would like to show all commits ever and grep on it – Dici Jan 16 '16 at 17:32
  • @Jubobs I don't have the commit that's exactly the problem. It could be in any on my 300 commits – Dici Jan 16 '16 at 17:52
  • @Dici On second thought, this looks like a good job for [`git bisect`](https://git-scm.com/docs/git-bisect). I'm thinking along similar lines to [this](https://wikigurus.com/Article/Show/54106/Find-and-restore-a-deleted-file-in-a-Git-repo). – jub0bs Jan 16 '16 at 18:17
  • @Jubobs nice command, I'm going to take a look – Dici Jan 16 '16 at 18:21
  • @Jubobs I have found the offending commit thanks to Adi Levin's script but I would also have found it with `git bisect`. I think it would be useful to put is as an answer, I would upvote it. Thanks for your help ! – Dici Jan 16 '16 at 18:31
  • Related: http://stackoverflow.com/a/953507/2541573 – jub0bs Jan 19 '16 at 07:56

2 Answers2

1

You could find renames of the file foobar.cs like this

git log --summary --follow foobar.cs | grep -2 rename

The --follow will force git log to follow renames, and --summary will output text describing when renames were made. The grep extracts the renames, and -2 shows the two surrounding rows, including the commit that renamed.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • Thanks for your answer, please see my edit, as it may affect your answer. I forgot one important detail, sorry about that – Dici Jan 16 '16 at 18:01
1

You can capture the full path of a file in every commit, if you know its name, using ls-tree -r with a grep:

#!/bin/bash
filename_to_look_for=SpecRunner.html
commit_range=1cb1d..e172

echo Looking for file $filename_to_look_for in commit range $commit_range
echo

list_of_commits=($(git rev-list $commit_range))
num_of_commits=${#list_of_commits[@]}

look_for_file_in_commit() { git ls-tree -r $1 | grep $filename_to_look_for; }

for c in "${list_of_commits[@]}"
do
  echo Commit $c ":"
  look_for_file_in_commit $c
  echo
done

This is an example of an output:

$ bash lstree.sh
Looking for file SpecRunner.html in commit range 1cb1d..e172

Commit e172774592f13c9fc1bdcd22099e1a104c5d1208 :
100644 blob 33ce97139315d7240ea3d09a5c62f5ea89887cd7    TestPlans/e2e/SpecRunner.html

Commit 14310bc0cf69967d4781e0aec2fd2cca21d72ac6 :
100644 blob 33ce97139315d7240ea3d09a5c62f5ea89887cd7    TestPlans/e2e/SpecRunner.html

Commit 20e22a4b88f36f1f9109680c0bed8b6b28941e9f :
100644 blob 33ce97139315d7240ea3d09a5c62f5ea89887cd7    TestPlans/e2e/SpecRunner.html

Commit fb80ab129f10225117c7a8b25ab51d1e7842e752 :
100644 blob 33ce97139315d7240ea3d09a5c62f5ea89887cd7    TestPlans/e2e/SpecRunner.html

Commit 8d67498dd04ddb1bd27fd110554021d2a7b7c7f1 :
100644 blob 33ce97139315d7240ea3d09a5c62f5ea89887cd7    TestPlans/e2e/SpecRunner.html
Adi Levin
  • 5,165
  • 1
  • 17
  • 26
  • I'm on Windows right now, using git for Windows, but that sounds promising. git for Windows supports some bash commands, I'm not sure whether it has full support or not, but I'm going to try – Dici Jan 16 '16 at 18:05
  • I'm also on Windows, using git bash. – Adi Levin Jan 16 '16 at 18:24
  • I'm using Git Extensions, and it has a "git bash" menu command that opens a command-line. I used it to run the above script. – Adi Levin Jan 16 '16 at 18:28