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 ?

2 answers

  • answered 2018-07-11 04:24 VonC

    You can:

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

  • answered 2018-07-11 07:41 basin

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

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

    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

    For older git versions you will need a script to crawl all the commits and trees to find this blob as in this answer:

    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"
    basin@BASIN /cygdrive/c/myrepo/source
    $ git -c core.autocrlf=true hash-object "c:\temp\myfile.txt"