---
title: "I committed a password or API key: how do I remove it from Git history?"
description: "Rotate the leaked secret first, because rewriting history cannot take it back. Then remove it from every commit with git filter-repo and force push, knowing clones and forks may keep it."
url: https://www.danny.engineering/answer/git/i-committed-a-password-or-api-key-how-do-i-remove-it-from-git-history
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Git"]
---

# I committed a password or API key: how do I remove it from Git history?

## The short answer


Revoke or rotate the secret straight away. Assume it is compromised the moment it was pushed. Then, if you also want it out of the history, rewrite every commit with `git filter-repo` and force push.


```bash
# 1. Rotate the key with the provider first
# 2. In a fresh clone, replace the secret in all history
echo 'sk_live_abc123==>REMOVED' > ../replacements.txt
git filter-repo --replace-text ../replacements.txt
# 3. Re-add the remote and overwrite it
git remote add origin git@github.com:you/project.git
git push --force --all origin
git push --force --tags origin
```


## Why it happens


Every commit stores a full snapshot of your files. Deleting the secret in a new commit only removes it from the latest snapshot; every older commit still contains it, and anyone with the repository can read it with `git show` or `git log -p`.


Once a commit is pushed, you also lose control of the copies. Other developers have cloned it, CI systems have checked it out, forks may exist, and automated scanners watch public repositories for keys within minutes. Rewriting history cleans the repository you control, but it cannot reach those copies. That is why rotating the secret always comes first: a revoked key is harmless wherever it ends up.


## How to fix it


### Rotate the secret


Go to the provider (your cloud console, payment provider, database host and so on), revoke the leaked credential and issue a new one. Check the provider's logs for any use you do not recognise. Put the new value in an environment variable or secret manager, not in the code.


### If you have not pushed yet


If the commit only exists locally, you do not need to rewrite anything shared. Remove the file or value, then amend the commit.


```bash
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit --amend --no-edit
```


If the secret is in an older local commit, use `git rebase -i` to edit that commit, or use `git filter-repo` as below before pushing. Rotating is still wise if there is any chance it left your machine.


### Remove it from all history with git filter-repo


`git filter-repo` is the tool the Git project recommends in place of the older `git filter-branch`. Install it from your package manager (for example `brew install git-filter-repo` or `pip install git-filter-repo`).


Work in a fresh clone. By default filter-repo refuses to run in a repository that is not a fresh clone, so you do not destroy local work by mistake.


```bash
git clone git@github.com:you/project.git project-clean
cd project-clean
```


To replace the secret text wherever it appears, list it in a file. Each line is a literal string, and `==>` sets the replacement. Keep this file outside the repository.


```bash
printf 'sk_live_abc123==>REMOVED\n' > ../replacements.txt
git filter-repo --replace-text ../replacements.txt
```


To remove a whole file from every commit instead, use `--invert-paths`.


```bash
git filter-repo --invert-paths --path .env
```


The rewrite changes the hash of every affected commit and everything after it. filter-repo also removes the `origin` remote as a safety measure, so add it back before pushing.


### Force push and tell everyone


```bash
git remote add origin git@github.com:you/project.git
git push --force --all origin
git push --force --tags origin
```


Branch protection may block force pushes, so an admin may need to allow it briefly. Then ask everyone to delete their old clones and clone again. If someone merges or pushes from an old clone, the secret comes straight back.


On hosted platforms, old commits can still be reachable through pull request refs or caches. On GitHub, for example, you may need to contact support to purge them. Forks are separate repositories and keep the old history.


### Stop it happening again

- Add `.env` and similar files to `.gitignore` before you create them.
- Turn on secret scanning and push protection if your host offers it.
- Run a scanner such as gitleaks in a pre-commit hook or in CI.

## Common pitfalls

- **Rewriting history but not rotating.** The key is already exposed. Rewriting alone gives a false sense of safety.
- **Deleting the file in a new commit and stopping there.** The secret is still in older commits.
- **Keeping the replacements file in the repo.** It contains the secret. Store it outside and delete it afterwards.
- **Teammates pushing from old clones.** One push undoes the whole clean-up. Make sure everyone reclones.
- **Forgetting tags and other branches.** Push with `--all` and `--tags`, or the secret survives there.

## Related reading

- [git-scm.com: git filter-branch (see its warning and the recommendation to use git filter-repo)](https://git-scm.com/docs/git-filter-branch)
- [git-scm.com: gitignore](https://git-scm.com/docs/gitignore)
