日本語

How to remove untracked files with git clean

​​Git​command line

Scratch notes and build output can pile up in a working folder and make git status hard to read. git clean deletes the files that Git is not tracking (untracked files) in one go, which tidies up the working folder. It does not touch changes to files that are already committed, so unfinished edits to tracked files stay in place.

Note

I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). I used only repositories I created for testing, and I did not test on Windows or macOS.

The working folder before cleaning

I tested with a repository that has three committed files: README.md, index.html, and .gitignore. The .gitignore contains node_modules/ and *.log.

console
$ cat .gitignore
node_modules/
*.log

$ git log --format='%h %an <%ae> %s'
de16aeb Neko <neko@example.com> 最初のコミット

I then prepared four kinds of uncommitted items: one added line in index.html, a new memo.txt, a new folder drafts/ containing idea.md, and debug.log and node_modules/, which are ignored by .gitignore.

console
$ git status --short
 M index.html
?? drafts/
?? memo.txt

$ git status --short --ignored
 M index.html
?? drafts/
?? memo.txt
!! debug.log
!! node_modules/

As described in the official git status reference, items marked ?? are the files Git is not tracking. Files ignored by .gitignore appear with !! only when you add --ignored.

In the following sections, I recreated the files deleted in the previous section before each test (the change to index.html was left as it is).

Preview with -n, then delete with -f

Running git clean with no options stops without deleting anything.

console
$ git clean
fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean

According to the official git clean reference, the clean.requireForce setting defaults to true, and in that case git clean does nothing unless you pass one of -f (force), -i (interactive), or -n (dry run).

First, I used -n to display what would be deleted. With -n, nothing is actually removed.

console
$ git clean -n
Would remove memo.txt

$ git clean -n -d
Would remove drafts/
Would remove memo.txt

With -n alone, only memo.txt was listed, and drafts/ was not a target. To delete the contents of an untracked folder, you need -d. The official reference explains that, to avoid deleting too much, when no path is given, untracked directories are not entered unless -d is given.

If the displayed list looks right, I replaced -n with -f and deleted the files.

console
$ git clean -f -d
Removing drafts/
Removing memo.txt

$ git status --short --ignored
 M index.html
!! debug.log
!! node_modules/

$ cat index.html
<h1>ねこノート</h1>
<p>新しい記事の一覧</p>

memo.txt and drafts/ were deleted. The line I added to index.html remained. debug.log and node_modules/, which .gitignore ignores, were not deleted by this command.

Warning

I found that files removed by git clean could not be recovered with Git. Running git restore memo.txt after the deletion only printed error: pathspec 'memo.txt' did not match any file(s) known to git. Always check with -n before deleting.

Also delete ignored files (-x and -X)

To delete ignored files, such as build output or node_modules/, add -x or -X. The two differ in scope. I recreated memo.txt and drafts/, so there were four items in total including the ignored ones, and tested from that state.

console
$ git status --short --ignored
 M index.html
?? drafts/
?? memo.txt
!! debug.log
!! node_modules/

$ git clean -n -d -X
Would remove debug.log
Would remove node_modules/

$ git clean -n -d -x
Would remove debug.log
Would remove drafts/
Would remove memo.txt
Would remove node_modules/

The uppercase -X removes only the ignored files (debug.log and node_modules/). Files you created by hand, such as memo.txt and drafts/, remain. The lowercase -x removes both ignored and untracked files. The official reference also describes -X as useful when you want to rebuild from a clean state while keeping hand-made files, and -x as useful when you want to remove everything, including build output.

When I actually ran -X, only the ignored items were removed.

console
$ git clean -f -d -X
Removing debug.log
Removing node_modules/

$ git status --short --ignored
 M index.html
?? drafts/
?? memo.txt

Here is a summary of which items each set of options removes:

Table of the items removed by each git clean option. -n removes only memo.txt, -n -d removes memo.txt and drafts/, -n -d -X removes debug.log and node_modules/, and -n -d -x removes all four. The change to index.html is never removed (the image labels are in Japanese; the table lists the options and the items under "Would remove")
Items listed under Would remove when options are added to git clean -n

Keep files with -e

If there are files you want to keep, pass an exclusion pattern with -e. Again, I recreated the deleted files before testing.

console
$ git clean -n -d -e memo.txt
Would remove drafts/

$ git clean -n -d -x -e node_modules/
Would remove debug.log
Would remove drafts/
Would remove memo.txt

With -e memo.txt, memo.txt was excluded from the targets. The second example uses -x to clear everything but keep node_modules/. According to the official reference, -x stops using the .gitignore rules, but the patterns you pass with -e are still used.

Delete only inside a specific folder

Putting a path after -- limits the targets to items matching that path.

console
$ git clean -n -d -- drafts/
Would remove drafts/

$ git clean -f -d -- drafts/
Removing drafts/

$ git status --short
 M index.html
?? memo.txt

Only drafts/ was deleted, and memo.txt remained.

Use -i to delete one item at a time

With -i, git clean shows the candidates and then asks what to do. Choosing 4: ask each lets you confirm each deletion. I recreated drafts/ and memo.txt, then chose 4, answered y for drafts/, and n for memo.txt. Because I passed the answers through a pipe, the typed input is not shown.

console
$ git status --short
 M index.html
?? drafts/
?? memo.txt

$ printf '4\ny\nn\n' | git clean -i -d
Would remove the following items:
  drafts/   memo.txt
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help
What now> Remove drafts/ [y/N]? Remove memo.txt [y/N]? Removing drafts/

$ git status --short
 M index.html
?? memo.txt

Only drafts/, which I answered y to, was deleted. According to the official reference, choosing 1: clean removes every listed candidate, and 5: quit exits without deleting anything. 2: filter by pattern lets you enter patterns for items you do not want removed, and 3: select by numbers lets you choose items to delete by number.

For a folder containing a separate repository, pass -f twice

If an untracked folder contains another repository created with git init (one that has its own .git folder), -f -d does not delete it. I recreated drafts/ and memo.txt, ran git init in lib/, placed one file there, and then tested.

console
$ git status --short
 M index.html
?? drafts/
?? lib/
?? memo.txt

$ git clean -n -d
Would remove drafts/
Would remove memo.txt

$ git clean -f -d
Removing drafts/
Removing memo.txt

$ git status --short
 M index.html
?? lib/

lib/ appears in git status, but it was not in the -n output, and it remained without any message. As the official reference says, passing -f twice deletes it.

console
$ git clean -n -d -f -f
Would remove lib/

$ git clean -f -f -d
Removing lib/

$ git status --short
 M index.html

$ ls -A
.git
.gitignore
README.md
index.html

lib/ was removed along with its contents, including its .git folder. Check what is inside before you use -f -f.

If you don't want to type -f every time

Setting clean.requireForce to false lets git clean delete without -f. I changed the setting for one run with git -c to test it.

console
$ git -c clean.requireForce=false clean
Removing memo.txt

If you forget -n while this setting is off, the files are deleted before you can check them. It is safer to keep the default setting and use -n first, then -f.

​Posted by 86