Interview Q&A / Git & GitHub

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What does git clean do, and how do you use it safely?

SHORT ANSWER

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

bash
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 careful

What 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.

#git#clean#untracked

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.