AI Hints
Home›Git & GitHub Tutorial›Project: Rescue Mission
Git & GitHub · Lesson 125 of 144

Project: Removing the Secret

Before it is pushed.

⏱ 2 min read▶ Video at 2:16:26🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

The secret key is in the last commit, but it hasn't been pushed. That's lucky. We can rewrite that commit without the file, and nobody will ever see it.

Project: Removing the Secret
From the video: Project: Removing the Secret
  • The secret is in the last commit
  • Not pushed yet
  • Rewrite the commit

Amend the commit

First, ignore dot ENV from now on, by adding it to dot git ignore. Then git r m dash dash cached, to stop tracking it, but keep it on disk. Stage the ignore file, and amend the last commit. Show dash dash stat proves the commit now has the shop page and the ignore file, but no secret.

git
echo '.env' > .gitignore
git rm --cached .env
rm '.env'
git add .gitignore
git commit --amend -m "Add shop page"
[main 373488f] Add shop page
 Date: Mon Jan 5 09:36:32 2026 +0000
 2 files changed, 2 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 shop.html
git show --stat --oneline HEAD
373488f (HEAD -> main) Add shop page
 .gitignore | 1 +
 shop.html  | 1 +
 2 files changed, 2 insertions(+)

If it had been pushed

If the secret had already been pushed, amending wouldn't be enough. The key could already be copied. You'd have to cancel and replace the key at the service that issued it, straight away. Rewriting history comes second. GitHub also scans for leaked keys, and may warn you.

Pushed a secret?assume it's leaked
Firstrevoke and replace it
Thenclean up history
GitHubsecret scanning

Summary

An unpushed secret can be removed by amending the commit. A pushed secret must be revoked first. Next, moving the commit to its own branch.

Key takeaways
  • Add to .gitignore
  • git rm --cached
  • git commit --amend
Next up: Project: Finishing the Rescue — branch, push, restore