CheckWebSecurity Brand Logo

Article

An exposed .git directory: why it gives away your whole repository (one curl check)

Published 2026-11-19

An exposed .git directory: why it gives away your whole repository (one curl check)

If your deployment is a git clone or git pull on the server, there is a .git directory sitting in your web root right now. Whether that is a problem comes down to one question: can a stranger fetch it over HTTP? One request answers that:

curl -sk https://example.com/.git/HEAD

If the reply is ref: refs/heads/main, your repository metadata is being served to the public. A 403 or 404 is what you want. The rest of this article is about why that one line matters more than it looks, how to check properly, and what the fix actually is, because the obvious fix is only half of it.

What .git actually contains

Most people think of .git as "the settings folder". It is closer to a compressed copy of the project's entire memory. It holds every commit you have ever made, including the files you deleted, the passwords you rotated out of the code, and the branch names nobody was supposed to see. git rm records a deletion in a new commit; the old file stays in the object database underneath.

The pieces that matter to an attacker:

HEAD. One line naming the current branch, or a bare commit hash on a detached checkout. It is the entry point, and the cheapest thing to probe.

config. Your remotes, in plain text. This file is the single most expensive one in the directory, and it gets its own section below.

index. The staged tree: every path in the working copy with its hash. Even if nothing else is readable, a fetchable index hands over the complete list of your file names, which is a map of the application by itself.

refs/ and objects/. The commit graph and the blobs it points to. Together they are the repository.

This is the part most people get wrong. A leaked wp-config.php gives away one secret. A leaked .git is a source control export: the code that runs today, every version that ran before it, and the commit messages you wrote at 2am thinking nobody would ever read them.

Why switching off directory listing does not help

The most common reaction is "we disabled directory listing years ago". It does not matter here. Git's layout is the same everywhere and needs no listing to walk. HEAD names the branch, refs/heads/main gives up the commit hash, and from there the object files are addressable by their own hashes. Free tools like git-dumper and the GitTools suite do exactly this walk: fetch, resolve, fetch again, then rebuild a working repository on the attacker's machine. A directory listing would be a convenience, not a requirement.

One honest caveat. A 200 on .git/HEAD is the signal, not the full verdict. Reconstruction depends on the files under .git/objects/ being individually reachable, and occasionally a server denies those while leaving HEAD readable by accident. But stock nginx and Apache deny nothing unless someone wrote a rule, so on a default setup treat full reconstruction as the realistic case, not the paranoid one.

config is where the money is

.git/config stores your remote URLs. If anyone ever configured a remote the lazy way, https://user:TOKEN@github.com/org/repo.git, that access token sits in the file in plain text, and the file is one URL away from anyone.

This is not a theoretical pattern. In September 2025, Sysdig's threat research team documented a campaign they named EMERALDWHALE, which harvested around 15,000 cloud credentials largely by scanning for exposed .git/config files and reading what came back. Nothing was exploited to get in. The servers simply served the file on request, the way they serve a logo.

Even without credentials in the URL, the reconstruction itself is worth a lot: hard-coded API keys, internal admin routes, and the exact library versions you have not patched yet are all easier to find with the source in hand.

How to check your site

Run the probe from outside, against every host you own, not just the main one:

for h in example.com staging.example.com; do
  for p in /.git/HEAD /.git/config /app/.git/HEAD /blog/.git/HEAD; do
    curl -sk -o /dev/null -w "%{http_code} $h$p\n" "https://$h$p"
  done
done

Two details make this check more honest than a bare status code:

Read the body, not just the code. Some servers and CDNs answer every unknown path with a custom error page and a 200 status. A real HEAD file contains exactly one line, ref: refs/heads/.... A real config opens with a [core] section. If the body is your site's "oops" page, you are fine even at 200; if the body is a ref line, you are not fine at any status.

Check subfolders. Every application deployed under a path (/app, /shop, /old) may carry its own .git. The one at the root being blocked does not say anything about the one a freelancer left inside /tools/analytics/ in 2023. On the server itself, a find settles it:

find /var/www -name .git -type d

Expect the occasional surprise. Five years of manual deploys accumulate strays.

How to close it properly

There are two layers here. Doing only the first is common and half-done.

Layer one: block it at the server. This is the seatbelt. For nginx, inside the server block:

location ~ /\.git {
    deny all;
}

For Apache, in the virtual host:

<DirectoryMatch "/\.git">
    Require all denied
</DirectoryMatch>

If you are on shared hosting and limited to .htaccess:

RedirectMatch 404 "\.git(/|$)"

The 404 is deliberate. A 403 politely confirms to the requester that the directory exists; a 404 says there is nothing here at all.

Layer two: stop shipping it. The block protects you while the directory exists, and directories exist for years on git-pulled servers. The structural fix is to not put .git in the docroot at all. Export instead of cloning:

git archive HEAD | tar -x -C /var/www/site

git archive writes the working tree with no repository metadata attached. Alternatively, keep the checkout outside the served tree and point the document root at the framework's entry folder (public/ in Laravel and Symfony). That layout gets this protection for free, since .git ends up above the docroot where the web server has no business reaching.

If it was readable, assume it was read

Blocking the path today does nothing about yesterday. Check whether anyone walked it while it was open:

sudo zgrep -h '"GET /.git' /var/log/nginx/access.log*

A burst of hits from one address is the signature: HEAD first, then config, then a long tail of object paths. That is a tool dumping your repository, and it means the whole history left the building. A quiet log is weaker comfort than it sounds, because logs rotate and the exposure may outlive them.

Either way, if .git was served for any length of time, rotate before you tidy up:

  1. Run a secret scanner such as gitleaks or TruffleHog over the entire history, not just the current tree.
  2. Rotate everything it finds: tokens in remote URLs, keys in old commits, database passwords in configs that were "removed" three commits later.
  3. Revoke any token embedded in a remote URL (git remote -v on the server shows them) and switch the server to a read-only deploy key.

The uncomfortable truth about step 3 and its neighbors: rewriting history afterwards does not un-leak anything. The copy on someone else's disk does not participate in your rewrite. Rotation is the only move that helps, so it comes first.

The short version

An exposed .git directory is the most total leak a web server can produce short of a shell, and it costs one curl command to detect. Block the path as a seatbelt, fix the deployment so the directory stops arriving, and if it was ever open to the world, treat the secrets in your history as public and rotate them. None of this takes long. The finding that takes long is the one you discover three months after someone else did.

If you want this checked continuously rather than remembered once a year, CheckWebSecurity probes .git/HEAD and .git/config on every scan and reports the finding in plain language alongside the rest of your exposure surface. Start a free check.


Related: Directory listing: what an "Index of" page leaks · WordPress security scanning without plugins