Git Clean

Updated · 9 min read
easyGit 2.53gitgit-cleanuntrackedinterview

After a long day of cooking, the kitchen has the dishes you meant to make, and also peelings, empty packets and a test batch that didn't work out. Cleaning up means throwing out what isn't part of the meal, without touching the meal.

In a project, that mess is untracked files: build output, test files, notes, the leftovers of an experiment. git restore and git reset don't touch them, because Git doesn't track them. git clean is the command that removes them. Because Git never had a copy of these files, a deleted one can't be brought back by Git, so git clean is built to make you look before you delete.

In this lesson, you will preview a clean, remove untracked files and folders, choose what happens to ignored files, and keep the files you want.

The Starting Point

Navika's project has two committed files and a .gitignore that ignores log files. After an afternoon of experiments, there are untracked files everywhere:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo "*.log" > .gitignore
git add .
git commit --quiet -m "Add README and ignore logs"
echo "try a discount" > experiment.txt
echo "call the office" > notes.txt
mkdir build
echo "compiled" > build/App.class
echo "started" > app.log
git status -s
git status -s --ignored
Output
?? build/
?? experiment.txt
?? notes.txt
?? build/
?? experiment.txt
?? notes.txt
!! app.log

experiment.txt, notes.txt and the build/ folder are untracked. app.log is ignored, so it shows only with --ignored, as !!.

Preview First: -n

-n (short for --dry-run) lists what git clean would remove, and removes nothing:

Terminal
git clean -n
Output
Would remove experiment.txt
Would remove notes.txt

Only the two untracked files are listed. Folders are left alone unless you add -d, and ignored files unless you add -x. Make -n a habit: run it first every time, read the list, then run the real command with the same options.

Git Refuses Without -f

Without -f (force), git clean doesn't delete anything:

Terminal
git clean
Output
fatal: clean.requireForce is true and -f not given: refusing to clean

This is a safety catch. The setting clean.requireForce is true by default, so a mistyped command can't wipe out files. You have to say -f (or -n to preview, or -i to choose interactively).

Remove Untracked Files: -f

Navika wants to keep notes.txt, but remove the experiment. Naming a path limits the clean to it:

Terminal
git clean -f experiment.txt
git status -s
Output
Removing experiment.txt
?? build/
?? notes.txt

-e (exclude) does the opposite: everything except a pattern. Here, the preview with -d includes folders, and -e notes.txt protects the notes:

Terminal
git clean -nd -e notes.txt
git clean -fd -e notes.txt
git status -s
Output
Would remove build/
Removing build/
?? notes.txt

build/ is gone with everything in it, and notes.txt is still there. The ignored app.log was not touched.

Ignored Files: -x and -X

By default, git clean leaves ignored files alone, because they are usually things you want, such as your local settings or downloaded dependencies. Two options change that:

  • -x removes ignored files as well as untracked ones.
  • -X (capital) removes only ignored files, and keeps the other untracked ones.
Terminal
echo "debug" > error.log
git clean -nX
git clean -nx
Output
Would remove app.log
Would remove error.log
Would remove app.log
Would remove error.log
Would remove notes.txt

-X lists only the two log files, which is handy for deleting build output and logs while keeping new files you haven't added yet. -x lists them together with notes.txt.

Syntax

Terminal
git clean [-n | -f | -i] [-d] [-x | -X] [-e <pattern>] [<path>...]
OptionIt is used to
-nShow what would be removed, remove nothing
-fActually remove (required by default)
-iChoose interactively what to remove
-dInclude untracked folders
-xInclude ignored files too
-XRemove only ignored files
-e <pattern>Keep files that match the pattern
<path>Clean only inside this file or folder
Which files each git clean option removes

In Practice

The most common use is getting back to a completely fresh copy of the last commit, for example before a release build, or when your folder is full of leftovers you don't understand. Two commands together do it:

Terminal
echo "half-finished edit" >> README.md
git reset --hard
git clean -fdx
git status -s --ignored
Output
HEAD is now at 7e3314e Add README and ignore logs
Removing app.log
Removing error.log
Removing notes.txt

git reset --hard throws away changes to tracked files, and git clean -fdx removes every untracked and ignored file and folder. Nothing is left but the last commit. Be very sure before you run this pair: it removes local settings, downloaded dependencies and anything else that is not committed.

Common Mistakes

Running git clean -f without -n first. You will delete files you forgot about. Preview, read, then run.

Forgetting -d and wondering why folders remain. Untracked folders need -d.

Using -x and losing local settings. -x removes ignored files too, such as .env files and IDE settings. Use -X or leave both out unless you mean it.

Expecting git clean to undo edits to tracked files. It only removes untracked files. Use git restore or git reset --hard for tracked ones.

Setting clean.requireForce to false to save typing. The safety catch is cheap, and a mistyped git clean without it can't be undone.

Under the Hood

git clean reads the same information as git status. It walks the working tree, compares every path with the staging area, and checks the ignore rules from .gitignore, .git/info/exclude and your global ignore file. A path that is not in the staging area is untracked, and one that also matches an ignore rule is ignored. With -n, Git prints the list. With -f, it deletes the files directly from the disk, the same way rm would.

Git also protects nested repositories: a folder with its own .git inside is not removed even with -fd. It takes -ff (force twice) to remove one.

Interview Corner

What does git clean do?

It removes untracked files from the working tree. -n shows what would be removed, -f removes them (Git requires it by default), -d includes untracked folders, and -x or -X include ignored files. Removed files can't be recovered by Git.

What is the difference between git clean -x and git clean -X?

-x removes ignored files in addition to the other untracked files. -X removes only ignored files and keeps the other untracked ones.

How do you make the working tree exactly match the last commit?

git reset --hard to undo changes to tracked files, then git clean -fdx to remove all untracked and ignored files and folders. Preview the clean with -ndx first.

Why does git clean refuse to run without -f?

Because clean.requireForce is true by default. Deleting untracked files can't be undone, so Git makes you say -f, -n or -i explicitly.

Practice

Challenge: preview a full clean. In the same repository, create a file draft.txt, a folder tmp with a file tmp/data.txt, and a file debug.log. Then preview three cleans: git clean -n, git clean -nd and git clean -ndX.

What does Git print? Before you open the solution, write down which of the three new paths each preview lists.

Show solution
Terminal
echo "x" > draft.txt
mkdir tmp
echo "x" > tmp/data.txt
echo "x" > debug.log
git clean -n
git clean -nd
git clean -ndX
Output
Would remove draft.txt
Would remove draft.txt
Would remove tmp/
Would remove debug.log

-n lists only draft.txt: the untracked folder needs -d, and the ignored log needs -x or -X. -nd adds tmp/. -ndX lists only the ignored debug.log, because capital -X means "ignored files only".

Checkpoint. In a practice repository, create untracked files, an untracked folder and an ignored file. Use -n with each combination of -d, -x and -X to predict, then clean them one group at a time with -f.

Quick check: Git clean

4 questions
1Which command shows what git clean would delete, without deleting anything?
2Which option makes git clean include untracked folders?
3You want to remove only ignored files, such as logs and build output. Which option?
4Can Git bring back a file removed with git clean -f?

Key Takeaways

Quick reference

CommandIt is used to
git clean -nPreview removing untracked files
git clean -fRemove untracked files
git clean -fdRemove untracked files and folders
git clean -fXRemove only ignored files
git clean -fdxRemove all untracked and ignored files and folders
git clean -f -e <pattern>Remove untracked files except matching ones
git clean -iChoose interactively

Was this tutorial helpful?

Lesson 26 of 106