What does git clean do, and how do you use it safely?
git clean deletes untracked files from the working directory. Because they were never committed, they cannot be recovered, so always run git clean -n first to preview what would be removed.
Explain it simply
After a build or experiment, your folder is littered with files Git does not track. git reset --hard will not remove them. git clean will.
The flags matter: -n is a dry run that only lists, -f actually deletes (required by default), -d includes untracked directories, and -x also removes ignored files such as node_modules or .env. Because Git never saved these files, there is no undo. Always dry-run first and read the list.
Example
git clean -n # preview: Would remove notes.txt
git clean -f # delete untracked files
git clean -fd # also untracked folders
git clean -fdx # also ignored files: be carefulWhat to say in the interview
"git clean removes untracked files. I always run git clean -n first because deleted untracked files cannot be recovered, then -f to delete, -d for folders, and -x only if I want ignored files gone too."
Common mistakes
- Using -x and wiping a local .env file.