Is your website's .git folder public? Find it and block it
Short answer: If your website was deployed by running git clone or git pull straight into the public web folder, the hidden .git folder may be downloadable by anyone, and with it your site's source code and its full change history, including any passwords or API keys that were ever committed. Check your own site for /.git/HEAD, block the folder at the web server today, then change how you deploy so the folder never reaches the public directory. Treat any secret that was ever in the repository as exposed and rotate it.
Why a .git folder on a web server is a problem
Every Git working copy has a .git directory. Git's documentation on repository layout describes what it holds: an object store with the project's content, refs recording the tip commit of each branch and tag, HEAD naming the current branch, and a repository-specific config file. Together, those files are the project, not just its current state but every committed version.
If the web server serves that folder like any other directory, someone who knows the layout can request the files one by one and rebuild the repository. Automated scanners look for this constantly. What leaks depends on what was ever committed:
- The source code of your site or app, including admin logic.
- Configuration files with database credentials or API keys, even ones deleted in a later commit, since old versions remain in history.
- Internal notes, email addresses in commit metadata, and URLs of staging systems.
- The remote URL in
.git/config, which sometimes carries an access token if someone pasted one into it.
Static sites are at lower risk than apps with server code, but a leaked history can still expose deploy keys, form-handler code and private drafts.
Step 1: Check your own site
Only test sites you own or manage. In a browser or with curl, request:
https://yourdomain.com/.git/HEADhttps://yourdomain.com/.git/config
If either returns content such as ref: refs/heads/main or a configuration block, the folder is exposed. A 403 or 404 is what you want. Also check subdomains, staging sites and any subfolder where a separate app lives, such as a shop or booking system installed in /book/.
Note that a 403 for /.git/ with a trailing slash doesn't prove safety. Directory listing may be off while individual files are still served, so test the files directly.
Step 2: Block it at the web server now
This is the emergency fix, not the final one.
nginx. nginx's access module lets you deny requests in a location block. A common pattern is a location matching paths that start with /.git and a deny all; inside it, or a rule that returns 404 for any hidden dot-path except /.well-known/, which must stay reachable for certificate checks and other standards.
Apache. Apache's mod_alias provides RedirectMatch, which can return a 404 status for any path matching /\.git. Alternatively, a directory-level rule can deny access to the folder.
Managed hosting or a CDN. Many hosts and CDNs let you add a rule that blocks paths beginning with /.git. Use it if you can't edit the server configuration.
After the change, repeat Step 1 and confirm you get 403 or 404 for HEAD and config.
Step 3: Stop deploying the folder at all
Blocking is a safety net. The real fix is that the public web directory shouldn't contain a repository:
- Deploy build output, not a working copy. Your pipeline builds the site, then copies only the files the site needs to the server.
- Use an export rather than a clone.
git archiveproduces the files of a commit without the.gitfolder, andrsyncwith an exclude rule can skip it. - Keep the repository outside the web root. If you must pull on the server, keep the clone in a private path and point the web server at a
publicsubfolder that contains no repository metadata. - Watch for other leftovers:
.envfiles,.DS_Store, editor backups likeconfig.php~, and.svnor.hgfolders from older version-control tools.
Step 4: Assume secrets are compromised
If the folder was reachable, you can't know who downloaded it. Work through:
- Search the repository history for credentials: database passwords, API keys, SMTP logins, payment keys, OAuth client secrets and deploy tokens.
- Rotate every one you find, starting with payment and email providers.
- Remove any token embedded in remote URLs and use a credential helper or deploy key instead.
- Check web server logs for requests to
/.git/paths, to understand whether and when it was downloaded. - Add secret scanning to your repository so new commits are checked.
Rewriting history to remove a secret is worth doing for hygiene, but it doesn't help after exposure. Rotation does.
An illustrative example
A hypothetical Cork bakery-supplies firm ran its order site on a small virtual server where a contractor had cloned the repository into the web root years earlier. A routine check showed /.git/config returning content. The team added an nginx rule returning 404 for dot-paths except /.well-known/, rotated the database password and the payment provider's API keys found in old commits, and changed deployment to build artefacts copied by the CI pipeline. Logs showed scanner requests to /.git/HEAD going back months.
FAQ
Is my site safe if directory listing is turned off?
Not necessarily. Files can still be requested by exact path even when the folder can't be listed. Test /.git/HEAD directly.
Do hosted website builders have this problem?
Usually not, because you don't deploy files yourself. It mostly affects sites deployed to servers or shared hosting with Git or FTP.
Should I delete the .git folder from the server?
Yes, once deployment no longer depends on it. Keep the repository on your Git host, not in the public folder.
Will blocking .git break anything?
No. Visitors never need it. Keep /.well-known/ reachable when writing broad dot-file rules.
If you'd like your server checked for exposed repository files and your deployment changed so they never go live, tell us where and how your site is hosted.