How to delete untracked files in Git
Deleting untracked files in Git helps clean your working directory by removing temporary files, build artifacts, and other unwanted content not tracked by Git.
As the creator of CoreUI with over 25 years of development experience, I regularly clean untracked files to maintain organized repositories and prevent accidental commits.
The most reliable approach uses git clean with appropriate flags to remove untracked files and directories safely.
This command provides precise control over what gets deleted while protecting important untracked content.
Use git clean -fd to remove untracked files and directories from your working directory.
git clean -fd
This command removes all untracked files (-f for force) and directories (-d for directories) from your working directory. Git requires the force flag as a safety measure since this operation is irreversible. The command only affects untracked files - those not added to Git’s index or committed to the repository. Files listed in .gitignore are also removed unless you use additional flags to preserve them.
Best Practice Note:
This is the cleanup method we use in CoreUI development for maintaining clean working directories.
Always run git clean -n (dry run) first to preview what will be deleted, ensuring you don’t lose important untracked files accidentally.



