🗂️
Security

Exposed .git directory: how your code and passwords leak

24.11.2026
← All articles

Many developers update their site by running git pull directly inside the project folder on the server. It is fast and convenient, but it leaves a hidden .git directory in the web root. If the web server does not block access to it, anyone on the internet can download it with a browser or a simple script, and with it your entire source code, its full history and often your most sensitive secrets.

What the .git directory actually contains

The .git directory is not a side file, it is a complete copy of the project. It holds every commit, every version of every file, the branch list, the remote repository address and a config file that sometimes contains an access token. The real danger is history: a password that was committed once and deleted later is still stored there. Your current files may be clean while a commit from three months ago still holds the database password or a payment gateway key.

Attackers do not browse this folder by hand. Ready-made tools scan the internet for exposed .git directories and rebuild the whole repository from them. They read HEAD and config first, then fetch the objects one by one. Disabling directory listing does not help much, because Git's internal layout is well known and files can be requested by name.

A realistic case: an online shop in Tashkent

Imagine a small home appliance shop in Tashkent running a Laravel site on misol.uz. The developer deploys with git pull, and .env is properly listed in .gitignore. But early in the project .env was committed by accident and then removed. One day the owner learns that the customer list has appeared on another website. The investigation shows that someone downloaded the repository through misol.uz/.git/ and found the database and mail credentials in an old commit. Two small oversights, a secret in history and an unprotected folder, added up to a serious leak.

How to check whether your .git is exposed

Open https://example.uz/.git/HEAD in a browser. If you see a line like ref: refs/heads/main, the folder is public. From a terminal you get a clearer answer:

curl -s -o /dev/null -w "%{http_code}\n" https://example.uz/.git/HEAD
curl -s https://example.uz/.git/config

A 200 status means the file is reachable by everyone, while 403 or 404 means it is blocked. Repeat the check for subdomains and any extra folders that hold old versions of the site.

Blocking hidden folders in Nginx and Apache

With access to the server configuration, add this block inside the server section. It does not touch .well-known, so certificate renewal keeps working:

location ~ /\.(git|svn|hg) {
    deny all;
    return 404;
}

Test it with nginx -t and apply it with systemctl reload nginx. On shared hosting you can usually add the following to the .htaccess file in the site root instead:

RewriteEngine On
RewriteRule (^|/)\.(git|svn|hg)(/|$) - [F,L]

The better fix: keep .git out of the web root

A deny rule can disappear when the server is reconfigured or the site is moved, so it should not be your only layer. Keep the repository outside the public folder and deploy only the working files:

git --git-dir=/home/user/repo.git --work-tree=/home/user/www/example.uz checkout -f main

Alternatives include building an archive with git archive in CI or copying with rsync --exclude='.git'. The principle is the same: the public folder should contain only what the browser needs.

If the folder was already exposed

Closing access is not enough, because someone may have downloaded it already. Treat every secret that ever appeared in the history as compromised:

  1. Block access immediately and confirm with the check above.
  2. Change database, mail, FTP and SSH passwords.
  3. Regenerate API keys for payment systems, SMS gateways and other services.
  4. Revoke any token found in the repository config.
  5. Search access logs with grep "/.git/" access.log to see when and from where it was requested.

Rewriting history with git filter-repo helps for the future, but it does not make an already leaked password safe. On sayt.uz hosting you manage site files in the ISPmanager panel opened from your cabinet, and the support section can help if you get stuck.

Related articles

🏴‍☠️ Nulled themes and plugins: why free premium software costs more 🛡️ Securing a Joomla site: updates, extensions and server settings 🔑 How to Disable SSH Password Login and Switch to Keys 🛡️ Disabling Dangerous PHP Functions with disable_functions
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓