<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9" -->

---
title: How to Fix a Leaked API Key: A Developer’s Guide to Git...
description: A step-by-step incident response guide for developers who accidentally commit an API key or other credential to a Git repository. It walks through the...
canonical: https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Fix a Leaked API Key: A Developer’s Guide to Git Security | daily.dev
og:description: A step-by-step incident response guide for developers who accidentally commit an API key or other credential to a Git repository. It walks through the...
og:url: https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9
og:image: https://api.daily.dev/og/posts/43yRnKSW9.png
og:image:alt: How to Fix a Leaked API Key: A Developer’s Guide to Git Security
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Fix a Leaked API Key: A Developer’s Guide to Git Security

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 18 min read · 5 upvotes · 1 comments

## Summary

A step-by-step incident response guide for developers who accidentally commit an API key or other credential to a Git repository. It walks through the 'Invalidate → Investigate → Remove → Replace → Prevent' workflow: revoking or rotating the leaked key first, checking provider logs and billing for abuse, removing the secret from working files using environment variables, cleaning Git history with git filter-repo when the secret was pushed, force-pushing carefully, replacing the credential everywhere it's used, and applying least-privilege restrictions to the new key. It also covers .env/.gitignore hygiene, frontend vs backend credential handling, secret managers, automated secret scanning (Gitleaks, TruffleHog), pre-commit hooks, and a list of common mistakes like assuming deletion or encoding is sufficient, or forgetting build artifacts and other branches.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/how-to-fix-a-leaked-api-key>

## Questions this post answers

### What should I do first if I accidentally committed an API key to Git?

Revoke or rotate the leaked credential immediately before doing anything else, including before cleaning Git history. Deleting the key from the latest file version does not remove it from Git history, and exposed credentials should be assumed compromised even if you delete them right away. The recommended order is invalidate, investigate, remove, replace, then prevent.

_daily.dev surfaces practical incident-response guidance for developers dealing with credential leaks._

### How do I permanently remove a secret from Git history?

Use git filter-repo to rewrite history, either removing the entire file with 'git filter-repo --path .env --invert-paths' or replacing the secret value inside files with 'git filter-repo --replace-text replacements.txt'. Back up the repository first with a mirror clone, verify removal with 'git log --all -S"secret" --oneline', then force-push the rewritten history and have collaborators reclone.

_Developers rewriting Git history to purge secrets can find step-by-step workflows like this via daily.dev._

### Does adding a file to .gitignore remove it from Git history?

No, adding a file such as .env to .gitignore only stops it from being tracked in future commits; it does not erase the file or its contents from previous commits. To stop tracking it going forward, run 'git rm --cached .env' and commit the .gitignore change, but the secret still needs to be removed from history separately if it was already committed.

_Track Git and secrets-management gotchas like this one through daily.dev before they bite in production._

## Community discussion

Top comments from developers on daily.dev.

**@markozakrajsekgeozs** · 2 upvotes

> Been there, done that! Good DEV guide 🤘

## Similar posts on daily.dev

- [\[REDACTED\] — How to keep your mobile app’s secrets, a secret\!](https://daily.dev/posts/redacted-how-to-keep-your-mobile-app-s-secrets-a-secret--605z53tt1) · ProAndroidDev · 0 upvotes · 0 comments
- [Preventing secret leakage in CI/CD environments](https://daily.dev/posts/preventing-secret-leakage-in-ci-cd-environments-tveiielvq) · Security Boulevard · 2 upvotes · 0 comments
- [The File That Hands Attackers Your Entire Server](https://daily.dev/posts/the-file-that-hands-attackers-your-entire-server-orzmzxctl) · Medium · 0 upvotes · 0 comments
- [How to Catch Security Vulnerabilities in Code Before They Reach Your Pull Requests](https://daily.dev/posts/how-to-catch-security-vulnerabilities-in-code-before-they-reach-your-pull-requests-bfxw4rcsv) · freeCodeCamp · 3 upvotes · 0 comments
- [10 Tips for Securing Your API Keys From AI](https://daily.dev/posts/10-tips-for-securing-your-api-keys-from-ai-j649i1e7u) · Nordic APIs · 0 upvotes · 0 comments

---

Tags: [#security](https://daily.dev/tags/security), [#github](https://daily.dev/tags/github), [#cicd](https://daily.dev/tags/cicd), [#git](https://daily.dev/tags/git), [#secrets-management](https://daily.dev/tags/secrets-management)

[View this post on daily.dev](https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Fix a Leaked API Key: A Developer’s Guide to Git Security","url":"https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9"},"datePublished":"2026-08-25T20:21:12.178Z","dateModified":"2026-09-14T06:19:53.267Z","description":"A step-by-step incident response guide for developers who accidentally commit an API key or other credential to a Git repository. It walks through the...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/38565b89315f6cde079d0d74af0ea2aa?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/38565b89315f6cde079d0d74af0ea2aa?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"security,github,cicd,git,secrets-management","timeRequired":"PT18M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"How to Fix a Leaked API Key: A Developer’s Guide to Git Security"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9","comment":[{"@type":"Comment","text":"Been there, done that! Good DEV guide 🤘","datePublished":"2026-08-28T05:06:20.854Z","url":"https://daily.dev/posts/43yRnKSW9#c-H0oCaBobc","author":{"@type":"Person","name":"Marko Zakrajsek","url":"https://daily.dev/markozakrajsekgeozs","image":"https://media.daily.dev/image/upload/s--slY3-STy--/f_auto/v1783585373/avatars/avatar_SbncGJHaRHf7KoebEkT1L?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-fix-a-leaked-api-key-a-developer-s-guide-to-git-security-43yrnksw9#faq","mainEntity":[{"@type":"Question","name":"What should I do first if I accidentally committed an API key to Git?","acceptedAnswer":{"@type":"Answer","text":"Revoke or rotate the leaked credential immediately before doing anything else, including before cleaning Git history. Deleting the key from the latest file version does not remove it from Git history, and exposed credentials should be assumed compromised even if you delete them right away. The recommended order is invalidate, investigate, remove, replace, then prevent. daily.dev surfaces practical incident-response guidance for developers dealing with credential leaks."}},{"@type":"Question","name":"How do I permanently remove a secret from Git history?","acceptedAnswer":{"@type":"Answer","text":"Use git filter-repo to rewrite history, either removing the entire file with 'git filter-repo --path .env --invert-paths' or replacing the secret value inside files with 'git filter-repo --replace-text replacements.txt'. Back up the repository first with a mirror clone, verify removal with 'git log --all -S\"secret\" --oneline', then force-push the rewritten history and have collaborators reclone. Developers rewriting Git history to purge secrets can find step-by-step workflows like this via daily.dev."}},{"@type":"Question","name":"Does adding a file to .gitignore remove it from Git history?","acceptedAnswer":{"@type":"Answer","text":"No, adding a file such as .env to .gitignore only stops it from being tracked in future commits; it does not erase the file or its contents from previous commits. To stop tracking it going forward, run 'git rm --cached .env' and commit the .gitignore change, but the secret still needs to be removed from history separately if it was already committed. Track Git and secrets-management gotchas like this one through daily.dev before they bite in production."}}]}
```

