Stop Tracking a File
Adding a tracked file to .gitignore does not stop Git from tracking it. You also have to remove its entry from the Index.
The following example stops tracking config.local in the repository root. Start with no unrelated changes staged and run the commands from that root.
- Add an Ignore Rule: Add this line to the root
.gitignorefile so that futuregit addcommands leave your local copy alone.
/config.local - Remove the File from the Index: The
--cachedoption keeps the working file on disk and stages its removal from the next commit.
git rm --cached -- config.local - Review the Change: Stage the ignore rule and check that the diff contains only the intended changes.
git add -- .gitignore
git diff --cached - Commit the Removal: Your local
config.localremains on disk and is now ignored.
git commit -m "Stop tracking local configuration" Stop Tracking a Directory
For a directory, add a matching ignore rule (for example, /local-config/) and use -r to remove its tracked contents from the Index. Then stage .gitignore and commit as above.
git rm -r --cached -- local-config Considerations
- Omitting
--cachedalso removes the working file from disk. - If
git rm --cachedrefuses because the staged version differs from both HEAD and your working file, preserve any staged-only changes before unstaging the file and retrying. - The commit removes the file from the shared project. When collaborators pull it, Git can remove their previously tracked copy; they should save any configuration they need beforehand.
- For local configuration, consider committing a template such as
config.examplethat new collaborators can copy to their ignoredconfig.local. - Older commits still contain the file.
- If you only want to undo
git addbefore committing, see Unstage Files.