AI Hints
Home›Git & GitHub Tutorial›Safety Nets
Git & GitHub · Lesson 35 of 144

Ignored Too Late

Git rm --cached.

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

Here's a common trap. You committed a file, and then added it to dot git ignore. But Git keeps tracking it anyway. Ignore rules only apply to files Git isn't already tracking.

Ignored Too Late
From the video: Ignored Too Late
  • A file was committed
  • Then added to .gitignore
  • Git still tracks it

Stop tracking, keep the file

Our debug log was committed by mistake. We add it to dot git ignore, but the change still shows up in status. The fix is git r m, dash dash cached. Cached means remove it from Git only, and keep it on disk. Commit, and from now on, Git ignores it. And l s shows the file is still here.

git
echo '*.log' >> .gitignore
git status --short
 M .gitignore
 M debug.log
git rm --cached debug.log
rm 'debug.log'
git commit -am "Stop tracking debug.log"
[main 0ede7f3] Stop tracking debug.log
 2 files changed, 1 insertion(+), 1 deletion(-)
 delete mode 100644 debug.log
git status --short
ls
about.html
debug.log
index.html
style.css

Leaked a secret?

One important warning. If you ever commit a password or secret key, removing it now doesn't remove it from history. Anyone with the repository can still find it in old commits. Treat it as leaked. Change the password or key straight away. We'll come back to this later.

Committed a secret?it's in history
rm --cacheddoesn't erase history
Treat it as leakedassume it's seen
Rotate itchange the key now

Summary

git r m dash dash cached stops tracking a file but keeps it on disk. And a committed secret must be changed. Next, cleaning junk out of your folder.

Key takeaways
  • git rm --cached <file>
  • Then commit
  • Leaked secrets: rotate them
Next up: Cleaning Up the Folder — git clean