Andrew
Andrew

Reputation: 113

How to find an existing old file in git repo

If I have a repo located in c:\myrepo\source and a file in the repo named myfile.txt with multiple versions and I have an old copy of myfile.txt in another folder eg c:\temp\myfile.txt is it possible to check if that version exists in the repo without copying it over the current file ?

Upvotes: 2

Views: 179

Answers (2)

basin
basin

Reputation: 4190

First you need to obtain the blob id for the file:

c:\myrepo\source>git hash-object "c:\temp\myfile.txt"
f70d6b139823ab30278db23bb547c61e0d4444fb

Then you can use the id to find the path and the commit where it was added.

With git 2.16+ it's easy:

c:\myrepo\source>git describe --always f70d6b139823ab30278db23bb547c61e0d4444fb
e76967c:path/to/myfile.txt

For older git versions you will need a script to crawl all the commits and trees to find this blob as in this answer: https://stackoverflow.com/a/223890/447503

For text files there's a caveat when there's different CRLF normalization (the default one and in the repo). Then git hash-object may print a wrong value.

On Cygwin, for example, this will help:

$ git hash-object "c:\temp\myfile.txt"
4a2cdc2c8fc21f625d69b9b9197004fbbd2de76b
basin@BASIN /cygdrive/c/myrepo/source
$ git -c core.autocrlf=true hash-object "c:\temp\myfile.txt"
f70d6b139823ab30278db23bb547c61e0d4444fb

Upvotes: 2

VonC
VonC

Reputation: 1324347

You can:

If one of those diff is empty, you know your old copy was already versioned.

Upvotes: 0

Related Questions