Git & GitHub Interview Question #20
How do you stop tracking a file that is already committed but keep the local file?
Git Fundamentals, Repository, Working Tree, Staging, Commits & Branch Basics Junior Beginner
Quick Interview Answer
Adding a tracked file to .gitignore is not enough because Git already knows about it. git rm --cached removes the path from the index while leaving the working copy in place; the removal is then committed.
Detailed Explanation
Adding a tracked file to .gitignore is not enough because Git already knows about it. git rm --cached removes the path from the index while leaving the working copy in place; the removal is then committed.
Before doing this, confirm that removing the file from the repository is appropriate. If the file contained a secret, removing it from the latest commit does not erase it from earlier history, so the secret should also be rotated and history remediation considered.
Code Example
git rm --cached appsettings.Local.json
echo appsettings.Local.json >> .gitignore
git add .gitignore
git commit -m "Stop tracking local settings"