That directive doesn't have to reside in .htaccess files. It works just as well inside a Directory, Virtual Host and Server contexts as well.
ErrorDocument 404 /404.php
<Directory "/.git">
ErrorDocument 404 "Ah ah ah! You didn't say the magic word"
</Directory>
https://httpd.apache.org/docs/2.4/mod/core.html#errordocumen...