Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I preview what Git clean would remove before deleting untracked files?

Quick answer

Use git clean -n to preview what Git would remove without removing anything. If you also want the preview to include untracked directories, use git clean -nd. Review the listed paths carefully before considering cleanup: options such as -x and -X change whether ignored files are included, and a pathspec can limit the scope. A preview applies to its options, exclusions, and pathspec, so match those to the cleanup you are considering. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want to clear out untracked files, but I do not want to delete something important by mistake. What should I do first?

Geez

Start with a dry run: git clean -n. The -n option reports what Git clean would remove without removing anything. [S1] Read the output as a list to inspect, not as confirmation that every listed item is disposable.

If your goal includes untracked directories, preview those too with git clean -nd. Git clean can recurse into untracked directories with -d; combining it with the dry-run option lets you see that broader set without deleting anything. [S1]

Before considering cleanup, check each listed path against work you might want to keep. If you cannot confidently identify an item, do not treat the preview as permission to remove it.

Follow-up question

How can I tell whether the preview includes directories and ignored files?

Geez

Check the options in the exact preview command. With -d, Git clean can include untracked directories; without it, do not assume the preview covers those directories. [S1]

Pay special attention to ignored files: -x includes ignored files, while -X selects ignored files only. Those options change what the command considers for removal, so an ordinary git clean -n preview is not a preview of a command that adds -x or -X. [S1]

For example, if you are considering a cleanup that includes ignored files, preview the corresponding scope without removing anything, such as git clean -ndx. Review its output separately. This example illustrates the option combination; it does not mean those paths are safe to delete.

Follow-up question

What exactly should I check in the output before I go any further?

Geez

Compare the preview with what you intend to include. Check that the command has the directory and ignored-file options you mean to use, and check whether it has exclusions or a pathspec that narrows the set. A preview’s scope depends on its options, exclusions, and any supplied pathspec. [S1]

Then read the listed paths one by one. Ask whether each is disposable, whether it belongs to a local experiment, and whether you need to preserve it. Those are review questions, not something Git can answer for you.

A hypothetical example: suppose the preview lists scratch.txt and build-output/. If scratch.txt holds notes you still need, stop and preserve those notes before considering cleanup. If you cannot tell what a listed directory contains or why it is there, do not proceed on the basis of a guess.

Follow-up question

Can you give me a cautious checklist I can reuse for a disposable repository?

Geez

Use this sequence:

  1. Decide whether you mean untracked files only, or untracked files and directories.
  2. Run a dry-run preview; use -d if the intended scope includes untracked directories. [S1]
  3. If ignored files are in scope, account for -x or -X in the preview and review that output separately. [S1]
  4. Check any exclusions and pathspecs so the preview matches the scope you have in mind. [S1]
  5. Read every listed path and preserve anything you might still need.
  6. If the preview is unclear or includes something unexpected, stop and investigate rather than continuing.

Example preview for untracked files and directories, without removing anything: git clean -nd. Treat its output as a report of what that particular option set would remove—not as a universal preview for other options or pathspecs. [S1]

A practical checklist

  • Choose whether untracked directories belong in the intended scope.

  • Use -n for a non-removing preview; add -d if previewing untracked directories.

  • Check whether -x or -X changes the treatment of ignored files.

  • Review exclusions and any pathspec against the scope you intend.

  • Inspect every listed path and preserve anything you may still need.

  • Stop if an item is unfamiliar, unexpected, or unclear.

A few more useful answers

Does `git clean -n` delete anything?

No. The -n/--dry-run option reports what would be removed without removing anything. [S1]

Will a preview without `-d` show untracked directories?

Do not assume it will. Git clean can recurse into untracked directories with -d, so include that option when directories are part of the scope you want to preview. [S1]

Does a preview cover ignored files automatically?

The options matter: -x includes ignored files, and -X selects ignored files only. Review a preview that uses the same intended options, exclusions, and pathspec as the scope you are considering. [S1]

Sources

References used for this guide.

  1. [S1] Git - git-clean Documentation
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work