Confirm the file is tracked
Check whether Git already tracks the path:
git ls-files -- path/to/fileIf Git prints the path, the file is tracked. Add a suitable rule to your project’s .gitignore, then remove only the indexed copy:
git rm --cached -- path/to/file--cached keeps the local working copy. Check git status and the staged diff before committing. Teammates who pull this commit may see the file removed from their working tree, so coordinate when it matters.
For a directory
Check exactly which tracked paths the directory contains before using the recursive form:
git ls-files -- path/to/directory
git rm -r --cached -- path/to/directory If a secret was committed
Removing a secret from the latest commit does not erase old history. Rotate the exposed credential and follow your hosting provider’s incident guidance. A later .gitignore rule only helps prevent accidental future additions.
For exact behavior and further details, consult the official references: GitHub’s ignoring-files guide