Practical technology learning
Git Ignore Rules: Check Whether a File Is Already Tracked
Adding a filename to .gitignore does not make Git forget a file it already tracks. A disposable repository with two harmless text files is enough to observe the boundary between ignore matching, the index and repository history.
Create an isolated fixture
Make a new local repository used only for this exercise. Create already-tracked.txt with harmless dummy text, add it and make a local commit. Then create new-note.txt without adding it. No remote is needed, and no credentials or business files belong in the example.
This supports Git collaboration foundations in a DevOps learning plan. Jaipur learners can keep the result as a short explanation of repository state, rather than turning the exercise into a live project cleanup.
Apply the same pattern to both files
Create .gitignore with a narrow rule matching these two dummy text filenames. Edit already-tracked.txt, then inspect git status --short and git ls-files. The tracked file remains tracked and its modification is still eligible for staging. The new untracked file is normally hidden by the matching ignore rule.
Ask two separate diagnostic questions
Use git ls-files -- already-tracked.txt to ask whether the path is in the index. Use git check-ignore -v --no-index -- already-tracked.txt to inspect which ignore rule would match without the usual index check. A matching pattern and a tracked path can coexist; they are answers to different questions.
For the untracked file, git check-ignore -v -- new-note.txt can explain the applicable pattern. Keep the reported rule alongside the status output so the reason is visible.
Explain untracking without rewriting history
In the disposable fixture only, git rm --cached -- already-tracked.txt removes the path from the index while keeping the working copy. Review the staged deletion and the ignore rule before any local commit. This changes a future snapshot; earlier commits still contain the file.
On a shared project, agree ownership and the intended repository state before removing tracked paths. Do not apply a broad recursive removal command just because a copied recipe suggests it.
Keep incident response outside the dummy lab
- Never insert a real secret to demonstrate an ignore rule.
- If a real credential was exposed, adding an ignore rule is not remediation; revoke or rotate it through the approved owner process.
- History cleanup, remote copies and caches require separate coordination and do not replace credential rotation.
Save a state-based conclusion
Record the tracked file, untracked file, matching pattern and index observation. Explain why .gitignore affected only one of them in the initial test. Mango Engineers' related DevOps course provides broader Git and delivery foundations; confirm current coverage and prerequisites separately.