Ignore rules primarily affect untracked files. Adding a rule does not remove an already tracked file from repository history.
Before you start
You should understand commits, branches, the working tree and the staging area. Draw the commit graph before changing history. Try commands in a disposable repository with a clean working tree so you can observe exactly which references and files each operation changes.
The practical goal is to reason through this situation: A committed generated file remains tracked after its pattern enters gitignore. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Identify tracking status
Gitignore mainly affects untracked files.
Step 2: Stop future inclusion deliberately
Changing ignore rules alone does not remove an existing tracked file.
Step 3: Address past exposure
An exposed credential requires revocation and policy-compliant history remediation.
Worked scenario
A committed generated file remains tracked after its pattern enters gitignore.
Adding .env to gitignore after committing it does not erase the old commit or revoke its key. Removing tracking addresses future snapshots only. Treat source-history cleanup and credential rotation as separate tasks, because copies may remain outside the rewritten repository.
Common mistake
Ignoring an exposed secret does not revoke it or erase previous commits.
Verify the behavior
Verify tracking and history independently without printing sensitive file contents.
Interview exercise
Handle a mistakenly committed credential.
Answer and reasoning
Rotate the credential and follow repository history-remediation policy; stop future tracking without pretending exposure has disappeared.
Continue learning
Compare the scenario with the Git and CI/CD interview questions and test your understanding with the Git and CI/CD MCQs. For terminology and implementation details, consult the reference material.