Q: You added `config.yaml` to `.gitignore` but Git is still tracking it and showing it as modified. Why, and how do you fix it?
gitignore only prevents *new* files from being tracked. It does not untrack files that are already in the repo. Git is still watching config.yaml because it was committed before the ignore rule existed.
.gitignore only affects untracked files.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "When an engineer accidentally creates this branch or commit divergence, I walk them through safe recovery without data loss. The interviewer is testing: Understanding that .gitignore only affects untracked files.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Production Solution & Architecture
.gitignore only prevents *new* files from being tracked. It does not untrack files that are already in the repo. Git is still watching config.yaml because it was committed before the ignore rule existed. Fix it by removing the file from the index while keeping it on disk: The --cached flag is critical — without it, git rm deletes the local file too. After the commit, .gitignore will keep it out going forward. For a whole directory: git rm -r --cached path/to/dir. To re-check what Git is currently tracking that *should* be ignored: git ls-files -ci --exclude-standard.
git rm --cached config.yaml
git commit -m "Stop tracking config.yaml"
- Immediate Triage: .gitignore only prevents new files from being tracked. It does not untrack files that are alrea
- Run targeted verification commands before modifying configuration.
- Automate permanent guardrails (CI check, alerts, IaC policy) to prevent recurrence.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring added configyaml gitignore
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring added configyaml gitignore Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring added configyaml gitignore
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring added configyaml gitignore Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.