Day 19/30 - Git Clean -fd: How to Remove Untracked Files & Directories
Ruqaiya Beguwala

Ruqaiya Beguwala @ruqaiya_beguwala

About: 👨‍💻 | Software Developer | Open-Source Enthusiast | JavaScript & NodeJS | Think and write about code every single minute

Location:
India
Joined:
Apr 26, 2025

Day 19/30 - Git Clean -fd: How to Remove Untracked Files & Directories

Publish Date: Jun 9
1 2

Introduction

When working with Git, you might end up with untracked files and directories cluttering your workspace.
git clean removes untracked files from your working directory. The flags:

  • -f (force) → Required to actually delete files.
  • -d → Removes untracked directories too.

Example: Basic Usage

Suppose you have:

project/  
│── tracked_file.txt  
│── untracked_file.log  
│── untracked_dir/  
    │── temp_file.tmp  
Enter fullscreen mode Exit fullscreen mode

Run:

git clean -fd
Enter fullscreen mode Exit fullscreen mode

This will delete:

  • untracked_file.log
  • untracked_dir/ (and its contents)

⚠️ Warning: Deleted files cannot be recovered unless you have backups!


When & Why to Use git clean -fd

Before switching branches – Avoid conflicts with leftover untracked files.
After a build/test – Remove generated files (e.g., node_modules/, .log files).
Resetting a messy workspace – Clean up before a fresh start.


Tips & Tricks

Now that you know the basics, here are some powerful ways to use git clean effectively:

1. Interactive Cleaning (-i Flag)

Instead of blindly deleting everything, use interactive mode to selectively remove files.

git clean -fd -i
Enter fullscreen mode Exit fullscreen mode

Output:

Would remove the following items:
  temp.log
  cache/
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help
What now> 
Enter fullscreen mode Exit fullscreen mode
  • Choose 4: ask each to confirm deletions one by one.

2. Remove Only Certain File Types

Use shell wildcards to delete only specific files.

# Delete all .tmp files
git clean -f "*.tmp"

# Delete all logs except error.log
git clean -f -e "error.log" "*.log"
Enter fullscreen mode Exit fullscreen mode

3. Remove Empty vs Non-Empty Directories

# Only removes empty directories
git clean -d

# Removes non-empty untracked directories too
git clean -fd
Enter fullscreen mode Exit fullscreen mode

4. Clean Only in a Specific Subdirectory

# Clean only the tmp/ directory
git clean -fd tmp/
Enter fullscreen mode Exit fullscreen mode

5. Combining with git stash

git stash -u       # Stash tracked + untracked changes
git clean -fd      # Delete remaining untracked files
git stash pop      # Restore changes
Enter fullscreen mode Exit fullscreen mode

6. Forced Clean (Bypass Safety Checks)

# Remove even .gitignored files
git clean -fdx

# Force remove nested Git repos
git clean -fdff
Enter fullscreen mode Exit fullscreen mode

7. Scripting & Automation

# Pre-commit hook to clean temp files
#!/bin/sh
git clean -fd -e ".env"   # Keep .env but delete rest

# CI/CD full cleanup
git reset --hard
git clean -fdx
npm install
Enter fullscreen mode Exit fullscreen mode

8. Recovering Deleted Files

# Check bash history for deleted files
history | grep "git clean"

# Always preview first!
git clean -fd -n
git clean -fd
Enter fullscreen mode Exit fullscreen mode

9. Specialized Clean Operations

# Only remove ignored files (great for node_modules)
git clean -fdX

# Same as -n but more explicit
git clean -fd --dry-run

# Full workspace reset
git clean -fd && git restore .
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've now unlocked the full power of git clean -fd – from basic file cleanup to advanced Git workspace management. Whether you're a beginner removing temporary files or an expert automating CI/CD pipelines, these techniques will keep your repositories lean and efficient.


Summary: Key Takeaways

Command Effect
git clean -fd Deletes untracked files & dirs
git clean -n Dry run (shows what will be deleted)
git clean -e "*.log" Excludes .log files
git clean -fdX Removes only ignored files
git clean -i Interactive mode
git clean -fdx Includes ignored files

Final Pro Tips

  • Always check with git clean -n first!
  • Use .gitignore to prevent future clutter.
  • Combine with git stash -u to preserve work-in-progress.

Up Next in the Series: git rm --cached – Remove files from staging but keep locally


Daily advance GIT tips in your inbox---worth starting? Respond to my poll here🚀

For more useful and innovative tips and tricks, Let's connect on Medium

Comments 2 total

Add comment