---
title: "How do I keep secrets such as API keys out of my repository?"  
description: "How do I keep secrets such as API keys out of my repository?"  
author: "Federica Bianchi"  
published: 2026-11-16  
updated: 2026-11-16  
canonical: https://www.mindstick.com/forum/163049/how-do-i-keep-secrets-such-as-api-keys-out-of-my-repository  
category: "Git and DevOps"  
tags: ["Secrets Management", "API keys", "security", "git", "devops"]  
reading_time: 1 minute  

---

# How do I keep secrets such as API keys out of my repository?

I accidentally pushed an API key to GitHub. How do I fix it, and how do I avoid it next time?

## Replies

### Reply by Shakil Siddique

First, **treat the key as stolen.** Bots scan public repositories within minutes. Revoke or rotate the key at the provider right away. Deleting the file in a new commit is not enough, because the key stays in the Git history. Cleaning history (with git filter-repo or BFG) is optional after rotation.

To avoid it next time:

- **Do not put secrets in code or committed config files.** Keep them in environment variables or a separate file that is in .gitignore.
- In .NET development, use **User Secrets** (`dotnet user-secrets`).
- In production, use a secret store: Azure Key Vault, AWS Secrets Manager, HashiCorp Vault, or your CI system's secret variables.
- Turn on **secret scanning** (GitHub has it) and consider a pre-commit hook such as gitleaks.
- Give keys the **least permissions** and an expiry, so a leak does limited damage.

Commit a template such as `.env.example` with fake values, so teammates know what to configure.


---

Original Source: https://www.mindstick.com/forum/163049/how-do-i-keep-secrets-such-as-api-keys-out-of-my-repository

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
