Git Interview Question 6: Untrack Ignored File
Git Interview Questions · Test 1 · question 6 of 10
A config/local.env file was committed to the repository months ago. You add config/local.env to .gitignore, but every time you edit the file, git status still lists it as modified.
You want Git to stop tracking the file so that it is removed from the repository from the next commit on, while your local copy stays on disk. What should you do?
Show answer and explanation
Answer:
.gitignore only applies to untracked files. The gitignore documentation says files already tracked by Git are not affected, and it recommends git rm --cached to remove such a file from the index. --cached removes the path from the index only and leaves the working-tree file alone. After you commit, the file is no longer tracked, and because it matches .gitignore, it doesn't come back. The file is still present in older commits. If it contains secrets, rotate them.
Option B tells Git to assume the file hasn't changed, which hides your edits from git status. The file stays tracked and stays in every future commit, which the stem rules out.
Option C removes the file from the index and deletes it from your working tree. The stem says the local copy must stay on disk.
Option D only unstages pending changes to the file. The file remains tracked, so nothing about the repository changes.