Your Exposed .git Directory Is a Source-Code Fire Sale: What Attackers Actually Pull From It
Most teams treat an exposed /.git/ directory as a cosmetic misconfiguration. It is worse: with the config, index, HEAD and reflog readable, an attacker can enumerate your tracked files, your remotes, your commit timeline β and reconstruct the repository byte-for-byte offline. Here is what each file actually leaks, and how to prove (not guess) whether yours is exposed.
π Table of contents
A deployment shortcut that has survived two decades of tooling improvements: git clone your project onto the web server, point the vhost at the checkout, ship. It works, so nobody questions it β until someone notices that the directory your site lives in also contains a .git folder, and that most web servers happily serve dot-directories unless told otherwise. One GET /.git/config later, your repository metadata is in an attacker's terminal.
The usual response when this shows up in a scan report is a shrug: "it's just config, the real code isn't there." That response is wrong in an expensive way. The .git directory is not a leftover β it is the repository. The working tree is the disposable part. Here is what each readable file hands over, and how our free website security scan proves the exposure with structured evidence rather than a bare existence check.
π¨ TL;DR β Why This Finding Is Critical
π£ In one sentence: if
/.git/is readable on your website, an attacker can download your entire source code history β every version of every file β with free tools and zero exploitation.
- π No exploit needed β just plain HTTP
GETrequests- π¦ Tools like
dvcs-ripperrebuild the full repository offline from/.git/objects/- π Credentials embedded in the remote URL in
/.git/configleak in plaintext- πΊοΈ The index enumerates every filename in your project, including files no public URL exposes
- π οΈ Fix: deploy builds (not checkouts) + block dot-directories at the web server (one rule)
π What Each File in .git Actually Leaks
π§ /.git/config β your remotes and sometimes credentials
This INI file lists every remote your deployment used, including the clone URL. If your team cloned over HTTPS with an embedded token, or uses a remote that embeds a username, that string is sitting in plaintext one request away. Even without credentials, the remote reveals where your private code lives (GitLab self-hosted? GitHub Enterprise? A CI mirror?), which internal hostnames exist, and often the project's real name β useful for phishing your developers with frightening accuracy.
π /.git/index β a complete inventory of every tracked file
The index is a binary file with a well-documented format: magic bytes DIRC, a format version, an entry count, and one entry per tracked file with its path and SHA-1 object hash. An attacker who reads the index doesn't need to guess whether config/production.yml or admin_panel_v2/ exists β the index enumerates every filename in your project, including files that no public URL would ever expose. It is a filesystem map of your application that you published by accident.
πΏ /.git/HEAD and /.git/packed-refs β the branch layout
HEAD shows the checked-out branch or a detached commit hash. packed-refs lists branch and tag hashes. Together they tell an attacker which branch is deployed and give the exact object identifiers needed for the next step.
π /.git/logs/HEAD β your commit timeline
The reflog carries timestamps and commit messages. Suddenly your deployment cadence, the commit that shipped last Thursday's hotfix, and the typo'd message describing an internal incident are all public. Attackers use reflog timestamps to correlate with other recon and to identify the juiciest recent changes to diff.
π£ /.git/objects/ β the payload
With object files (or pack files, listed via objects/info/packs) readable, everything above collapses into the worst case: tools like dvcs-ripper walk the object database and reconstruct the entire repository offline β every version of every file, full history included. Your source is not "partially exposed"; it is downloaded.
π§ͺ Existence Checks vs Proof
Many scanners flag .git exposure by fetching /.git/HEAD and grepping for ref: β and stop there. The problem is that plenty of servers answer 200 with the site's HTML page for any path (SPA routers and catch-all rewrite rules do this constantly), so a naive grep produces both false positives and, worse, shallow findings that read as low severity.
Our git_history_leak scanner takes a proof-level approach with three confirmation gates on every probe. First, the HTTP status must be a real 200 β redirect chains that dissolve into some other page die here. Second, the body must have the exact shape of the file it claims to be: a binary DIRC magic header with a sane entry count for the index, structural INI parsing of [core] and [remote "..."] blocks for the config, strict line formats for HEAD, packed-refs and reflog. A catch-all HTML page cannot fake a DIRC header. Third, soft-404 baselines are compared so that custom error pages which return 200 OK never produce a finding.
When the scanner does fire, the report shows structured evidence: the parsed remote URL with credentials masked, the index entry count with up to twenty sampled tracked paths, the branch from HEAD, reflog timestamps β each probe individually confirmed, so you know exactly which files are readable and how deep the leak goes. Read-only GETs only: the scanner never fetches a git object, never writes, never exploits.
π‘οΈ What to Do About It
π§― If it is exposed right now
Treat it as a source-code disclosure incident, not a config tweak. Rotate any credential that ever appeared in the repository (assume the full history was downloaded), then remove the exposure. Deleting .git from the web root is the minimum; if you were cloned with a token in the remote URL, revoke that token too.
π Prevent it permanently
- ποΈ Deploy builds, not checkouts. Run
git archive, a CI artifact, or rsync of the built output to the server β the repository never touches the web root. - π« Block dot-directories at the web server. Nginx:
location ~ /\. { deny all; }. Apache:RedirectMatch 404 /\.or equivalent. This protects.git,.svn,.hgand friends in one rule. - π Verify from the outside. Point the 2MNY scanner at your site and read the Git History Leak section β it fetches each probe through the same Tor-routed, browser-fingerprinted transport a real attacker would use, so a finding means a real external observer can read it, not just a localhost test.
An exposed .git directory is one of the few findings that upgrades itself with zero attacker effort: no exploit, no bypass, no race β just HTTP GETs against a file your server should never have shown. Close it before someone patient finds it, because on the internet, someone patient always does.
Security engineer building 2MNY β 292 free website-security checks β and the open-source Website Vulnerability Scanner. I find exposed secrets, misconfigurations and real vulnerabilities, then explain how to fix them in plain language.
1 What is an exposed .git directory?
An exposed .git directory means your website serves the internal Git metadata folder ( /.git/ ) that was created when the code was cloned or deployed to the server. Because that folder IS the repository β index, objects, history β anyone who can request /.git/config, /.git/index or /.git/objects/ over plain HTTP can download or reconstruct your full source code, including every historical version.
2 How do I check if my .git directory is exposed?
Open https://your-site.com/.git/HEAD in a browser or curl. If you see plain text starting with 'ref: refs/heads/...' instead of an error page, your repository metadata is exposed. For proof-level detection with false-positive filtering, run 2MNY's free website security scan and read the Git History Leak section β it validates the exact file structure (DIRC headers, INI blocks, reflog formats) before reporting.
3 Can attackers really recover my full source code from .git?
Yes. If /.git/objects/ (or the pack files listed in objects/info/packs) is readable, free tools like dvcs-ripper walk the object database and rebuild the entire repository offline β every version of every file, complete history included. Even when objects are blocked, the config, index, HEAD and reflog still leak your remotes, internal hostnames, every tracked filename, and your commit timeline.
4 Is deleting the .git folder from the server enough to fix it?
It closes the exposure, but it is not the whole fix. Treat the event as a source-code disclosure incident: rotate every credential that ever appeared in the repository (assume the full history was downloaded), revoke any token embedded in the deployment remote URL, and check whether secrets were committed historically. Then prevent recurrence by deploying built artifacts instead of git checkouts and blocking dot-directories at the web server.
5 How do I block .git access in nginx or Apache?
Nginx: add 'location ~ /\. { deny all; }' to your server block (return 404 works too). Apache: use 'RedirectMatch 404 /\.' in your vhost or .htaccess. Both rules cover .git, .svn, .hg and other dot-directories in one line. After applying, verify from an external network β and re-test with 2MNY's scanner to confirm the probes now fail.
6 Does checking someone's .git exposure count as hacking?
A single unauthenticated GET to /.git/HEAD is the same request every browser makes to any URL, and it is exactly what security scanners (including 2MNY's) check when you scan your own sites. Using the exposed data to download someone else's source code, however, is unauthorized access in most jurisdictions. Scan only assets you own or are authorized to test.