Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: check whether file exists in some version

In my application, I'm using git for version management of some external files

I'm using commands like git show HEAD~1:some_file to get a certain version (based on git tag, commit hash or relation to HEAD) of a file. When the file does not exist, a 'fatal' message is outputted (I think to the stderr pipe). e.g.

fatal: Path 'some_file' does not exist in 'HEAD~1'

Is there a clean command to check whether a file exists in a certain version?

like image 904
Len Avatar asked Aug 27 '13 09:08

Len


2 Answers

How are you using this? In a script? You can always check the exit code. And git cat-file may be better suited:

git cat-file -e HEAD~1:some_file

With the -e option, it will exit with non-zero error code when object doesn't exist and with 0 when it does. It will, however, print the fatal: Not a valid object name as well, but that can be easily suppressed:

    git cat-file -e HEAD~1:some_file 2> /dev/null && echo Not found. || echo Found.
like image 112
manojlds Avatar answered Oct 19 '22 02:10

manojlds


Best thing I've found so far is:

git ls-tree -r HEAD~1 --name-only

Which outputs the list of file names in a specific version. I'll need to parse the output and filter a specific file. Therefore its a bit less direct than I hoped, but a piece of cake.

like image 45
Len Avatar answered Oct 19 '22 02:10

Len