Interview Q&A / Git & GitHub

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Why does .gitignore not work for a file that is already tracked?

SHORT ANSWER

Git ignores only untracked files. If a file was committed earlier, it stays tracked despite the rule. Run git rm --cached file to stop tracking it, then commit; the file remains on disk.

Explain it simply

A .gitignore is a 'do not admit' list at the gate, but people already inside the hall are not thrown out.

After adding .env to .gitignore, git status may still show changes to it because it was committed earlier. Tell Git to forget it: git rm --cached .env. The --cached flag removes it only from the index, not from your disk. Commit that, and from then on the ignore rule applies. Note it still exists in old commits, so a leaked secret must be rotated.

Example

bash
echo ".env" >> .gitignore
git rm --cached .env
git commit -m "Stop tracking .env"
git status    # .env no longer listed

What to say in the interview

"gitignore only affects untracked files. For one already committed I run git rm --cached file and commit, which keeps the local copy. If it held secrets, they remain in history and must be rotated."

Common mistakes

  • Believing .gitignore removes a file from earlier commits.

#git#gitignore#tracking

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.