How do I remove local (untracked) files from the current Git working tree?
To remove local (untracked) files from the current Git working tree, you can use the git clean command. Start with a dry run using git clean -n to see what would be deleted, then execute git clean -f to actually remove the untracked files. For removing directories as well, use git clean -df.
Contents
- Understanding Untracked Files
- The Git Clean Command
- Git Clean Options and Flags
- Safety Precautions and Best Practices
- Practical Examples
- Alternative Approaches
Understanding Untracked Files
Untracked files are files in your working directory that are not in the last snapshot and not in the staging area [source]. When you first clone a repository, all files will be tracked and unmodified. As you work, you may create new files that Git doesn’t yet know about - these become untracked files.
Untracked files are the files in your working directory that are not in the last snapshot and not in the staging area.
These files appear when you run git status with the message “Untracked files:”. While untracked files don’t affect your repository’s commit history, they can clutter your workspace and cause confusion during development [source].
The Git Clean Command
The git clean command is a convenience method for deleting untracked files in a repo’s working directory [source]. By default, git clean will only remove untracked files that are not ignored. Any file that matches a pattern in your .gitignore or other ignore files will not be removed [source].
git clean
This command removes files that are not tracked by Git - files that you’ve never added to the staging area with git add [source]. Normally, when no <pathspec> is specified, git clean will not recurse into untracked directories to avoid removing too much [source].
Git Clean Options and Flags
The git clean command supports several options that modify its behavior:
| Option | Description | Example |
|---|---|---|
-n or --dry-run |
Show what would be deleted without actually deleting | git clean -n |
-f or --force |
Actually delete the files | git clean -f |
-d |
Also remove untracked directories | git clean -df |
-x |
Remove ignored files too | git clean -fx |
-i |
Interactive mode | git clean -i |
-q or --quiet |
Don’t list deleted files | git clean -qf |
The -x option indicates git clean to also include ignored files [source]. You can combine these options for different behaviors:
git clean -df # Remove untracked files and directories
git clean -fdx # Remove untracked files, directories, and ignored files
In interactive mode, git clean will show you the following options:
- clean: Delete all selected items
- filter by pattern: Select items matching a pattern
- select by numbers: Select specific items by number
- ask each: Confirm deletion of each item
- quit: Exit without deleting anything
- help: Show this help message [source]
Safety Precautions and Best Practices
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 [source]. You need to take a backup before executing the git clean command [source].
Always do a dry run first before you remove untracked Git files [source]. The git clean command may delete important property or configuration files [source]. Here are key safety precautions:
- Always run with
-nfirst to see what will be deleted - Backup important files before running the actual delete command
- Be cautious with ignored files when using the
-xoption - Check git status before and after running git clean
- Use specific pathspec when possible to limit the scope of deletion
The git clean command may delete important property or configuration files. Always do a dry run first before you remove untracked Git files.
Practical Examples
Basic File Cleanup
To see what would be deleted without actually deleting:
git clean -n
To actually delete untracked files:
git clean -f
Removing Directories
To remove both untracked files and directories:
git clean -df
Cleaning with Ignored Files
To remove ignored files as well (use with caution):
git clean -fx
Interactive Cleaning
For more control over what gets deleted:
git clean -i
This will show you a list of files and directories that would be deleted, allowing you to select which ones to remove [source].
Cleaning Specific Paths
If you only want to clean specific directories:
git clean -f path/to/directory/
Alternative Approaches
Manual Deletion
You can also manually delete untracked files using system commands:
find . -type f -not -path './.git/*' -exec rm {} \;
find . -type d -not -path './.git/*' -exec rmdir {} \;
Using .gitignore
Before deleting, consider adding important files to your .gitignore to prevent them from being tracked in the future:
echo "temp/" >> .gitignore
echo "*.tmp" >> .gitignore
git add .gitignore
git commit -m "Add .gitignore entries"
Git Reset
For files that were previously tracked but have been modified or deleted:
git reset HEAD -- file.txt
Conclusion
The git clean command is the most efficient way to remove untracked files from your Git working tree. Remember these key points:
- Always start with a dry run using
git clean -nto preview what will be deleted - Use
git clean -fto delete untracked files andgit clean -dfto include directories - Be extremely cautious with the
-xoption as it will delete ignored files too - Files deleted with
git cleancannot be recovered through Git - always backup important files first - Consider using
.gitignoreto prevent important files from becoming untracked in the future
By following these practices, you can maintain a clean working directory while avoiding accidental data loss.
Sources
- Stack Overflow - How do I remove local (untracked) files from the current Git working tree?
- Git Documentation - git-clean
- TheServerSide - git clean: How to remove untracked files in Git
- Sentry - Remove untracked files from the working tree in Git
- GitHub - Git Tips: Reset, Clean - Remove untracked files
- GeeksforGeeks - Remove Local Untracked Files From Current Git Working Tree
- Ubuntu Manpage - git-clean
- Atlassian Git Tutorial - How to Remove Untracked Files in Git
- W3Docs - Git Clean - How To Use Git Clean
- DataCamp - Git Clean: Remove Untracked Files and Keep Repos Tidy