Skip to main content

Git Clean: Remove Untracked Files and Keep Repos Tidy

Master git clean to safely remove untracked files and directories. Learn key options, interactive mode, and best practices to prevent data loss.
Mar 7, 2025  · 8 min read

When working on a Git repository, it’s common to accumulate untracked files and directories that are not part of the repository’s version control system. These can include temporary files, build artifacts, or other locally created files that are not committed. Over time, these files can clutter the working directory, making it harder to maintain a clean and organized workspace.

To address this problem, Git provides the git clean command, which removes untracked files and directories. In this guide, I will show you how to safely use git clean, its different options, and best practices to avoid unintended file deletion while keeping your Git repository tidy. For further learning, I recommend taking our Foundations of Git and Introduction to GitHub Concepts courses to learn about version control and the difference between Git and GitHub.

Understanding git clean

The git clean command is a useful tool in Git that removes untracked files from the working directory. The git clean command is useful when you need to reset your working directory to a pristine state, such as before switching branches or after running build processes that generate temporary files. However, the git clean command should be used with caution since it permanently deletes untracked files and directories without moving them to a trash or recycle bin.

Unlike git reset or git checkout, which modify tracked files, git clean strictly deals with files and directories that are not under version control. This includes:

  • Temporary files created by builds

  • Log files or cached data

  • Files listed in .gitignore (if explicitly specified with -x)

The git clean command is useful for maintaining a tidy working directory, complementing other Git commands like git reset and git checkout, which manage changes in tracked files.

I find the Complete Git Cheat Sheet a handy reference guide for common Git commands.

Common Uses for git clean

The git clean command is a useful tool for maintaining a clean working directory. Below are some common use cases where git clean can safely remove unnecessary files and directories.

Removing untracked files

When working in a Git repository, untracked files can accumulate over time. The following options help in removing them:

  • git clean -f: Forces the removal of untracked files.

  • git clean -fd: Removes untracked files and untracked directories.

  • git clean -fx: Removes untracked and ignored files such as those listed in .gitignore.

However, you should be careful when using the -fx option because it also removes ignored files. Always verify using a dry run (git clean -n) before executing the command.

Interactive mode

The git clean -i command enables an interactive mode, allowing users to selectively remove untracked files. This mode is useful when you want more control over which files or directories are deleted.

Assume you are working on a project where you have already saved the changes in the Olympics folder. However, you have added new data sources named, 2024_Data, and 2025_Data. If you run the git clean -i command, you will get the prompt allowing you to choose from the following options to delete the untracked changes.

  • clean:  Remove selected files

  • filter: Specify a pattern for files to clean

  • select: Choose individual files by number

  • quit: Exit without deleting anything

git clean -i interactive mode.

git clean -i interactive mode. Image by Author.

Dry run for safety

Using the git clean -n option is a useful safety measure since it performs a "dry run," displaying a list of untracked files and directories that would be removed without actually deleting them. This allows you to review the changes before executing them.

In the example below, the git clean -n command lists the files that should be deleted but does not delete them.

git clean -n for dry run.

git clean -n for dry run. Image by Author.

Combining with git reset

To fully reset a Git working directory, you can combine git reset and git clean where:

  • git reset --hard: Resets tracked files to the last committed state, discarding any local changes.

  • git clean -fd: Removes untracked files and directories, leaving only committed files.

This combination ensures a completely clean working directory, making it useful when switching branches, undoing experimental changes, or preparing for a fresh start.

Check out or Git Pull Force: How to Overwrite a Local Branch With Remote tutorial to learn the best practices for overwriting local changes.

Step-by-Step Guide to Using git clean

Follow these steps to safely clean your Git repository and remove unwanted untracked files and directories.

Step 1: Check the current state

Before cleaning, run git status to check the current state of your repository to see which files are untracked.

git status

git status to check the current state of the repository.

git status to check the current state of the repository. Image by Author.

Step 2: Preview what will be removed

Before executing any removal command, use git clean -n to preview what will be removed.

git clean -n

git clean -n to preview what to delete.

git clean -n to preview what to delete. Image by Author.

Step 3: Remove untracked files

Run git clean -f to remove untracked files if the preview looks correct.

git clean -f

Remove untracked changes using git clean -f.

Remove untracked changes using git clean -f. Image by Author.

Step 4: Remove untracked directories

Use git clean -fd to remove untracked directories.

git clean -fd

Remove untracked directories using git clean -fd.

Remove untracked directories using git clean -fd. Image by Author.

Step 5: Remove ignored files

To remove both untracked and ignored files, use the git clean -fx command.

git clean -fx

Step 6: Use interactive mode for selective cleaning

For more control, consider using git clean -i for interactive removal. This mode allows you to select which files to delete individually.

Git clean -i

git clean -i interactive mode for selective cleaning.

git clean -i interactive mode for selective cleaning. Image by Author.

git clean vs. Other Git Commands

The git clean command is often compared with other Git commands that modify or reset files. The table below highlights the key differences:

Command Function
git clean Removes untracked files and directories permanently.
git reset Resets tracked files to a previous state, affecting staged and committed changes.
git checkout (or git restore) Discards changes in tracked files but does not remove them.
git stash Saves both tracked and untracked changes temporarily for later use.

Git Clean Best Practices

To use git clean safely and effectively, follow these best practices:

  • Use Dry Runs (-n) Before Deletion: Always use the git clean -n option to perform a dry run before executing git clean. This allows you to preview which files will be removed without actually deleting them.

  • Be Mindful of Ignored Files: When using git clean, be cautious with the -x option, which removes ignored files. This can potentially delete important configuration files listed in .gitignore. If you need to remove ignored files, use the git clean -fx command.

  • Combine with Git Reset for Full Cleanup: To completely reset your repository, combine git reset with git clean, where git reset --hard restores tracked files to their last committed state and git clean -fd removes untracked files and directories.

  • Use Interactive Mode (-i) for Safer Deletions: Instead of blindly removing files, use interactive mode (git clean -i) to review and select the files to delete.

Potential Risks and Precautions

Using git clean can be risky if not done carefully, as it permanently deletes files without storing them anywhere. Here are some key risks and precautions to consider:

  • Files Are Permanently Deleted: Unlike git stash, which temporarily saves changes, git clean permanently deletes untracked files and directories. Once removed, these files cannot be recovered through Git. If you might need the files later, consider stashing or backing them up before running the git clean command.

  • Be Cautious with the -x Option: Using git clean-fx removes ignored files, which might include important configuration files listed in .gitignore or dependency caches like node_modules/ and venv/. Accidentally removing these files can break builds or development environments, so use this option only when necessary.

  • Run a Dry Run First: Always perform a dry run using git clean -n before executing git clean -f. This step allows you to preview which files will be deleted, helping you avoid accidental removal of important files

  • Alternative Approaches: In some scenarios, using git stash might be a better option than git clean. The git stash -u command stashes untracked files, excluding ignored files for potential recovery later, and the git stash pop restores stashed changes if needed. This method is useful when you need to switch branches or clean up your workspace without permanently losing changes.

Conclusion

A well-maintained codebase shows attention to detail, efficiency, and the ability to manage digital assets responsibly. Employers look for developers who understand best practices like using git clean -n before deleting files, being cautious with ignored files using -x, and making use of safer alternatives like git stash.

Taking DataCamp courses is not only a great way to learn but it's also a great way to signal to employers that you take software development seriously. To that end, I recommend studying out Top 20 Git Interview Questions and Answers for All Levels blog post and taking our new Git Fundamentals skill track to become an expert in all things Git.

Learn Git Fundamentals Today

For beginners: Master version control using Git.
Start Learning For Free

Allan Ouko's photo
Author
Allan Ouko
LinkedIn
I create articles that simplify data science and analytics, making them easy to understand and accessible.

Git Clean FAQs

What does git clean do?

git clean removes untracked files and directories from your Git working directory.

Does git clean delete committed files?

No, git clean only removes untracked files and does not affect committed or staged files.

How can I preview files before deleting them?

Use git clean -n to perform a dry run and see which files will be removed.

What is the difference between git clean -f and git clean -fd?

git clean -f removes untracked files, while git clean -fd also removes untracked directories.

How is git clean different from git reset?

git clean removes untracked files, while git reset restores tracked files to a previous state.

Is there a way to delete files selectively?

Yes, use git clean -i for interactive mode, which allows you to choose files to delete.

Topics

Learn Git with DataCamp

course

Introduction to Git

2 hr
15.6K
Discover the fundamentals of Git for version control in your software and data projects.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

cheat-sheet

Complete Git Cheat Sheet

Git lets you manage changes made to files and directories in a project. It allows you to keep track of what you did, undo any changes you decide you don't want, and collaborate at scale with others.
Richie Cotton's photo

Richie Cotton

9 min

tutorial

Git Prune: What Is Git Pruning and How to Use Git Prune

Git prune is a Git command that removes objects from the repository that are no longer reachable from any commit or branch, helping to free up disk space.
François Aubry's photo

François Aubry

5 min

tutorial

How to Use .gitignore: A Practical Introduction with Examples

Learn how to use .gitignore to keep your Git repository clean. This tutorial covers the basics, common use cases, and practical examples to help you get started!
Kurtis Pykes 's photo

Kurtis Pykes

17 min

tutorial

Git Install Tutorial

Learn about Git initial setup, Git LFS, and user-friendly Git GUI applications in this in-depth tutorial.
Abid Ali Awan's photo

Abid Ali Awan

9 min

tutorial

Git Squash Commits: A Guide With Examples

Learn how to squash commits on a branch using interactive rebase, which helps maintain a clean and organized commit history.
François Aubry's photo

François Aubry

7 min

tutorial

Git Setup: The Definitive Guide

In this tutorial, you'll learn how to set up Git on your computer in different operating systems.

Olivia Smith

7 min

See MoreSee More