Sub topic 6
Ignoring Files Git Already Tracks
This chapter assumes you know the basic Git workflow (git init, git add, git commit, git status) and that you have written a simple .gitignore file. Here you will learn why a new ignore rule sometimes does nothing, and how to fix it.
Why the rule does nothing
A .gitignore file only affects untracked files. A tracked file is one that is already in the index (Git's staging area) or in a commit. Git keeps reporting changes to a tracked file whatever the ignore rules say.
A common sequence goes like this:
- You commit
config/local.jsonby accident. - You notice, and add
config/local.jsonto.gitignore. - You edit the file, and
git statusstill shows it as modified.
The rule is correct. Git ignores it because the file is tracked. To make the rule take effect, you must first stop tracking the file.
Stop tracking a file and keep it on disk
The command git rm --cached removes a path from the index but leaves the file in your working tree. Use it together with the ignore rule:
echo "config/local.json" >> .gitignore
git rm --cached config/local.json
git commit -m "Stop tracking config/local.json"After this commit, the file is untracked, and the rule now matches it. git status no longer lists it.
For a whole directory, add -r:
git rm -r --cached build/Commit the .gitignore change in the same commit, so the rule and the removal stay together in history.
What this does to other people
The removal is a change in the repository like any other. When a teammate pulls your commit, Git deletes the file from their working tree, because the commit says the file no longer belongs to the project. If the file holds their local settings, they lose it unless they keep a copy first.
Tell your team before you push. Ask them to back up the file, pull, and then copy it back. After that, their copy is ignored on their machine too.
Common errors
- Forgetting
--cached. Plaingit rm config/local.jsondeletes the file from disk as well. If you did this by mistake and have not committed yet, rungit restore --staged --worktree config/local.json, or recover the file fromHEAD. - Removing the rule's target but not the file. If the rule pattern is wrong (for example
/local.jsonanchored to the root, when the file is inconfig/), the file becomes untracked but still appears ingit status. Check the pattern withgit check-ignore -v config/local.json. - Expecting the rule to apply retroactively. Adding a rule never changes history or the index. Only
git rm --cacheddoes. - Ignoring a file that must stay tracked in a template form. A common approach is to commit
config/local.json.exampleand ignore the realconfig/local.json.
Check the result
After the commit, confirm two things:
git ls-files config/local.json
git check-ignore -v config/local.jsonThe first command prints nothing when the file is no longer tracked. The second prints the ignore file, line number, and pattern that matches the path. If both behave as described, the file is untracked and ignored.
Keeping tracked changes out of your way (temporary option)
Sometimes you want to edit a tracked file locally without committing the edits, and without removing it from the project. Git offers git update-index --assume-unchanged and --skip-worktree for this. They are local flags, not shared with others, and they can cause confusing results when the file changes upstream. Use them sparingly. For files that should not be in the repository at all, git rm --cached is the correct fix.
Exercise
You added secrets.env to .gitignore, but git status still shows secrets.env as modified after you edit it. Which commands stop Git from tracking it without deleting your local copy, and why was the rule not working?
Show answer
The rule was not working because secrets.env was already tracked, and ignore rules only apply to untracked files. Run git rm --cached secrets.env and then git commit -m "Stop tracking secrets.env". The --cached option removes the file from the index only, so it stays on disk. Because the file remains in earlier commits, any real secrets in it should be rotated.