Undo & History

How to Stop Tracking a File in Git Without Deleting It

Remove a committed file (such as .env or local config) from Git tracking while preserving the file on your local disk.

Instant 1-Line Solution
Safe CommandBeginner
git rm --cached <file-path>
When to use this scenario:You accidentally committed a credentials file or IDE config (.vscode, .idea) and want to remove it from Git and add it to .gitignore.

Step-by-Step Execution Guide

1Remove file from Git index only
git rm --cached <file-path>

Deletes the file from Git's index (staging area) while leaving the physical file untouched on disk.

2Remove entire folder from Git index
git rm -r --cached <folder-path>

Recursively untracks all files inside the folder while keeping them on disk.

3Add pattern to .gitignore
echo "<file-path>" >> .gitignore

Prevents Git from detecting the untracked file as modified in future commits.

4Commit the removal
git commit -m "chore: stop tracking <file-path>"

Finalizes the untracking in Git history.

Critical Pitfalls & Precautions

  • β€’Never omit "--cached"! Running "git rm <file>" will delete the physical file from your hard drive.
Frequently Asked Questions

Untrack File (Keep Local) - Frequently Asked Questions

Common questions about safe rollback, remote history implications, and troubleshooting.

Yes! When teammates pull this commit, Git will remove the file from their working directory. Make sure to share a .env.example template.