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.
git rm --cached <file-path>Step-by-Step Execution Guide
git rm --cached <file-path>Deletes the file from Git's index (staging area) while leaving the physical file untouched on disk.
git rm -r --cached <folder-path>Recursively untracks all files inside the folder while keeping them on disk.
echo "<file-path>" >> .gitignorePrevents Git from detecting the untracked file as modified in future commits.
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.
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.
Related Git Command Guides
Browse All Git RecipesUndo your most recent Git commit while keeping all your edited files staged or unstaged in your working directory.
Discard all unstaged and uncommitted file modifications and restore your working tree to the latest commit.
Rename your current local branch and update the upstream remote tracking branch on GitHub or GitLab.
Delete a branch from remote origin (GitHub, GitLab, Bitbucket) after a pull request has been merged.
Amend the commit message of your latest commit before or after pushing.
Remove files from the Git staging area (index) without losing any of your local changes.