2

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 ?

Andrew
  • 113
  • 2
  • 8
  • Check if the blob ID exists already? It's a trivial header and sha ... maybe harder on windows where you don't have basic tools available. – o11c Jul 11 '18 at 04:33
  • See this answer: https://stackoverflow.com/a/48027778/447503 – basin Jul 11 '18 at 06:58

2 Answers2

2

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
basin
  • 3,949
  • 2
  • 27
  • 63
  • Looking for the hash is more efficient than my answer. +1. Note, you don't need Cygwin. A simple bash session is enough. – VonC Jul 11 '18 at 08:27
0

You can:

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250