solution for apache2 (LAMP) server - you have 2 places to add .htaccess contents.. if 1 fails, try next
- for (development environment)
 
create .htaccess file in /var/www/html root directory and paste the code inside it
<Directorymatch "^/.*/\.git/">
  Order 'deny,allow'
  Deny from all
</Directorymatch>
- for (Production environment)
 
inside virtual host file (/etc/apache2/sites-enabled/)  >find your virtualhost file> open file >  after closing of virtualhost tag, paste
<Directorymatch "^/.*/\.git/">
  Order 'deny,allow'
  Deny from all
</Directorymatch>
no need to restart the server, it runs when page is called upon