How to block access to the .git directory on your website
Block public requests to /.git/ in your web server settings, or better, keep the folder out of the published web root altogether. Then change any passwords or keys that were ever stored in the project history.
An exposed .git folder means anyone can type an address like yoursite.com/.git/config into a browser and download the hidden history of your website's code. It usually happens when a site is deployed by running git pull or git clone straight into the folder the web server publishes.
What people see
Most owners see nothing at all. The website works normally, and the problem is only found when a scanner, a security researcher or an attacker checks for it.
If the folder is exposed, opening /.git/HEAD shows a short line of text such as ref: refs/heads/main, and /.git/config shows the repository settings. Some servers also show a file listing. Automated tools look for this folder on many sites, so assume an exposed one will be found.
Why it matters
- Source code: Git stores every file and every past version. Tools exist that rebuild a project from an exposed
.gitfolder, even when the server does not show a file listing. - Secrets: Passwords, API keys and database details that were committed once stay in the history, even if a later change removed them.
- Access tokens: The
configfile can contain the address of your code host, and sometimes a token inside that address. - Trust and sales: Security scanners flag it, and customers who send security questionnaires will notice. A leak of customer data is far more costly than the fix.
- Search: It is not a ranking factor on its own, but a site that gets compromised can be flagged by browsers and search engines.
How to check if your site has it
These checks are free and take a minute.
- Open
https://yourdomain.com/.git/HEADin a browser. - Open
https://yourdomain.com/.git/configas well. - Or run
curl -I https://yourdomain.com/.git/HEADin a terminal and read the status line. - Repeat for subdomains, staging sites and old copies of the site.
| What you see | What it means |
|---|---|
Text starting with ref: refs/heads/ | Exposed, fix now |
Settings text from /.git/config | Exposed, fix now |
| 404 Not Found | Not reachable |
403 Forbidden on HEAD and config | Blocked |
| Your normal homepage | Probably a catch-all page, but confirm with config |
A 403 on /.git/ alone does not prove anything. Always test the actual files, HEAD and config.
How to fix it
There are two layers. Do the first if you can, and the second in any case.
1. Keep .git out of the web root. Deploy only the files the site needs, for example by uploading built files or using a deployment tool, instead of cloning into the public folder. If the repository must live on the server, place it above the public folder. If you do not need it on the server, delete the .git folder from the published directory. Look at what is there and keep a backup first.
2. Block it in the server settings. This is the safety net. The exact place depends on your server.
Nginx, inside the server block:
location ~ /\.git {
return 404;
}
Apache, in the site's .htaccess file or virtual host:
RedirectMatch 404 /\.git
IIS, in web.config:
<system.webServer>
<security>
<requestFiltering>
<hiddenSegments>
<add segment=".git" />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
Reload or restart the server after changing Nginx or Apache virtual host files. Step by step guides: Nginx and Apache.
Common platforms
- cPanel: Open File Manager, enable hidden files and look in
public_htmlfor.git. Move or delete it, or add the Apache rule to.htaccess. - Plesk: Check the Git extension for the repository path and point it outside the site's public folder. Extra server rules go under Apache and nginx settings for the domain (the name varies by version).
- Cloudflare: A WAF custom rule that blocks requests where the URI path contains
/.gitstops outside visitors. It does not remove the folder from your server, so still fix the origin. - Netlify: Only the publish directory is served, so the problem appears only if
.gitended up inside it. Check what your publish directory contains. - WordPress and shared hosting: Look in the folder that contains
wp-content, and ask your host if you cannot see hidden files.
Guides: cPanel, Plesk, Cloudflare, Netlify and IIS.
Change anything that was exposed
Blocking the folder does not undo past access. If it was public for any length of time, assume it was copied. Change database passwords, API keys, tokens and any other secrets that were ever committed, including ones you deleted later. Check your access logs for requests to /.git/ to see whether anyone looked. Rewriting the history helps for the future, but it cannot take back what was already downloaded.
How to confirm the fix worked
- Repeat the browser or
curlchecks on/.git/HEADand/.git/config. - You want a 404 or 403 and no file content.
- Clear any site cache or CDN cache, since an old response can linger.
- Test the other hostnames too, with and without
www.
The security scan in week 2 of SiteMonday checks for this kind of exposure and lists it in your report with a ranked fix list. It only runs after you prove you own the site.
How to stop it coming back
- Deploy built files rather than cloning into the public folder.
- Add a rule for hidden files and folders in your server settings, with an exception for
/.well-known/which is used for SSL certificates. - Keep passwords and keys out of the repository. Use environment settings outside the web root.
- Check for other leftovers in the web root, such as
.envfiles, exposed backup files and old copies of the site. - Test again after a server move, a new staging site or a change of hosting.
The scan can find and explain the problem, but the fix itself belongs to you or your developer.
Step by step for your platform
- Exposed .git folder on Nginx
- Exposed .git folder on Apache
- Exposed .git folder on cPanel
- Exposed .git folder on IIS
- Exposed .git folder on Cloudflare
- Exposed .git folder on Plesk
- Exposed .git folder on Netlify
Questions
Is an exposed .git folder dangerous if my site has no secrets?
It still gives away your full source code and its history, which makes other weaknesses easier to find. Most projects also contain at least some settings or keys, so treat it as a real problem.
Why does /.git/ show 403 but the site is still flagged?
A 403 on the folder can hide the fact that individual files such as `HEAD` and `config` are still readable. Test those files directly, and fix the server rule so it covers everything under `/.git`.
Can I just add .git to robots.txt?
No. The robots.txt file is public and only asks well behaved crawlers to stay away, so it can even point people to the folder. You need to block access on the server or remove the folder.
Do I need to delete the .git folder from the server?
Not always, but it is the safest option if you do not use it there. If you keep it, place it outside the published folder and block access as a second layer.
Will blocking .git break my website?
No. Visitors never need files inside `.git`, so blocking it does not affect pages, images or forms. Just make sure your rule matches only `.git` and not other paths.
I fixed it, do I still need to change my passwords?
Yes, if secrets were ever committed to the repository. Once a folder has been public, assume someone copied it, and change every key and password it may have held.