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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With