How do Git ignore rules differ from files already tracked by Git?
Quick answer
Git ignore patterns apply to files Git is not already tracking; adding a matching pattern does not untrack a file that is already in Git’s index. To keep the file on your computer but stop tracking it, add an appropriate ignore pattern and remove the file from the index with git rm --cached <path>. [S1]
The question, the details, the next step.
A practical checklist
Write down the exact file path you intend to ignore.
Decide whether the ignore rule should be shared with the repository.
Check whether Git already tracks the file; ignore rules do not untrack it. [S1]
Choose a pattern whose scope matches your intent.
For an already tracked file you want to keep locally, use
git rm --cached <path>. [S1]Review the path and pattern before applying the change.
A few more useful answers
Will adding a `.gitignore` rule remove a tracked file from Git?
No. Ignore patterns do not affect files Git already tracks. Remove the file from the index with git rm --cached <path> if you want to keep the local file but stop tracking it. [S1]
Does `git rm --cached` delete my local file?
The documented use is to remove the file from the index while retaining the local copy. Use the file’s path in place of <path>. [S1]
Should the ignore pattern go in the repository’s `.gitignore`?
Use a repository .gitignore when the pattern should be shared; Git also provides other exclude sources for rules with a different scope. [S1]
Sources
References used for this guide.
- [S1] Git documentation: gitignore git-scm.com · Checked
