How can I stop tracking a file in Git while keeping my local copy?
Quick answer
Run git rm --cached -- path/to/file. This removes the path from Git’s index while leaving the working-tree file in place; the removal is recorded for a later commit, not applied to earlier history. If you want Git to keep ignoring the local file, add a matching pattern to .gitignore: ignore rules do not untrack files that are already tracked. [S1] [S2]
The question, the details, the next step.
A practical checklist
Replace the example path with the exact file path in your repository.
Run
git rm --cached -- path/to/fileto remove the path from the index only. [S1]Confirm the local file is still present before committing.
Add a matching
.gitignorepattern if you want the local file ignored afterward. [S2]Review both the index removal and any ignore-file edit before committing.
A few more useful answers
Does adding a file to `.gitignore` untrack it automatically?
No. Ignore rules do not affect files already tracked. Remove the file from the index first, then add the ignore pattern if needed. [S2]
Will `git rm --cached` delete the file from earlier commits?
No. It records an index removal for a later commit and leaves earlier history unchanged. [S1]
Sources
References used for this guide.
- [S1] Git rm documentation: remove a path from the index only git-scm.com · Checked
- [S2] Git ignore documentation: ignored paths and already-tracked files git-scm.com · Checked
