DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Safely Remove Untracked Files in Git

Use git clean safely: preview untracked files first, then choose whether to remove files, directories, ignored files, or only selected paths.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use git clean -n to preview untracked files Git would remove. After checking the list, run git clean -f to delete those files. Add -d if you also want untracked directories included. The preview is essential: git clean deletes files, and its ignored-file options can broaden the scope.

Check what Git considers untracked

Start with git status to see the state of your working tree. git clean removes files that are not under version control; it does not discard edits to tracked files. If your goal is to revert changes to tracked files, do not use this command.

As an Amazon Associate I earn from qualifying purchases.

The Git project describes git clean as recursively removing files that are not under version control, starting from the current directory. Its official manual is updated through Git 2.56.0: git-clean.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Preview before deleting

Run a dry run first:

git clean -n

The -n option, also written --dry-run, shows what Git would remove without deleting anything. This preview does not include untracked directories by default. To preview those too, run:

git clean -n -d

Read the output and confirm every listed path is safe to discard. If it includes work you want to keep, stop and narrow the scope before running a deletion command.

Remove the files shown in the preview

Once the dry run shows the intended files, run the matching command with -f:

git clean -f

To remove untracked directories as well, include -d:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
git clean -f -d

Git normally requires the force option before deleting anything. If clean.requireForce is disabled in your configuration, that safeguard may not apply; check the preview regardless. A second -f permits Git to modify an untracked nested Git repository, such as another repository inside a directory. Use that only when you specifically intend to remove that nested repository.

Choose a narrower or interactive cleanup

Limit cleanup to a path

When only particular paths should be cleaned, supply a pathspec, for example:

git clean -n -- path/to/folder

If the preview is correct, run the same pathspec with force:

git clean -f -- path/to/folder

A pathspec scopes the affected paths; with a pathspec, -d is irrelevant. Always preview again with the same pathspec and cleanup options you plan to use for deletion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Select candidates interactively

Use interactive mode when you want to choose among the candidates rather than remove everything in scope:

git clean -i

Git presents cleanup choices so you can select what to remove. Review the candidates carefully before confirming a selection.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Understand ignored files: -x and -X

By default, git clean respects Git’s ignore rules. The two options that change this behavior have different scopes:

  • -x disables the standard ignore rules, so ignored files as well as other untracked files may be removed. Command-line exclusions supplied with -e are still retained.
  • -X removes only files ignored by Git, leaving other untracked files alone. This can help clear ignored build output while preserving manually created untracked files.

Preview either mode before deleting. For example, inspect ignored files only with:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
git clean -n -X

Use -x only if you intend to include ignored files in the cleanup. Those files may contain local settings, generated output, or other work you have not committed.

Git clean options at a glance

Option Effect
-n / --dry-run Show what would be removed without deleting it.
-i / --interactive Present candidates and let you make cleanup choices.
-d Include untracked directories when no pathspec is supplied.
-f / --force Permit deletion when the force requirement is enabled; normally required for actual cleanup.
-x Disable standard ignore rules so ignored and other untracked files may be removed; command-line -e exclusions remain.
-X Remove only files ignored by Git.
-e <pattern> Add an exclusion pattern to preserve matching paths during cleanup.
<pathspec> Limit cleanup to matching paths; -d is then irrelevant.
Second -f Allow modification of an untracked nested Git repository.

These behaviors are documented in the official Git clean manual.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.