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. 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. 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. 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. 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. 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. 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. 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 executinggit 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 thegit clean -fx
command. -
Combine with Git Reset for Full Cleanup: To completely reset your repository, combine
git reset
withgit clean
, wheregit reset --hard
restores tracked files to their last committed state andgit 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 thegit 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 likenode_modules/
andvenv/
. 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 executinggit 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 thangit clean
. Thegit stash -u
command stashes untracked files, excluding ignored files for potential recovery later, and thegit 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.