How to remove untracked files in git?

In Git, untracked files are files that are not currently under version control. These files have not been added to the Git index (staging area) and are not being tracked by Git. If you want to remove these untracked files, you can do so using the git clean command.

Steps to Remove Untracked Files

1. Preview Untracked Files

Before removing untracked files, it's a good idea to see which files would be affected. You can use the -n or --dry-run option with git clean to preview the files that will be removed without actually deleting them:

git clean -n

Output:

Would remove untracked_file1.txt
Would remove untracked_file2.log

2. Remove Untracked Files

To actually remove the untracked files, use the -f (force) option with git clean. Be careful with this command, as it will permanently delete the untracked files:

git clean -f

Output:

Removing untracked_file1.txt
Removing untracked_file2.log

3. Remove Untracked Directories

If you also want to remove untracked directories, you can use the -d option along with -f:

git clean -fd

Output:

Removing untracked_directory/
Removing another_untracked_directory/

4. Remove Ignored Files

If you have files that are ignored by your .gitignore file and you want to remove them as well, use the -x option. This option removes all untracked files, including those listed in .gitignore:

git clean -f -x

Output:

Removing .DS_Store
Removing build/
Removing temp/

Alternatively, if you want to remove only the ignored files and leave the untracked but not ignored files intact, use the -X option:

git clean -f -X

Output:

Removing .DS_Store
Removing build/

Summary of Options

  • -n or --dry-run: Preview the files that will be removed.
  • -f or --force: Actually remove the files.
  • -d: Remove untracked directories in addition to untracked files.
  • -x: Remove all untracked files, including those listed in .gitignore.
  • -X: Remove only ignored files.

Example Workflow

  1. Preview untracked files and directories:

    git clean -nd
  2. Remove untracked files and directories after confirmation:

    git clean -fd
  3. Remove all untracked files, including ignored files:

    git clean -f -x
  4. Remove only ignored files:

    git clean -f -X

Conclusion

Using the git clean command is a powerful way to manage and remove untracked files in your Git repository. Always use the -n or --dry-run option first to preview the files that will be affected to avoid accidentally deleting important files. Once you are sure, you can proceed with the actual clean-up using the appropriate options based on your needs.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which software tool is trending now?
Can I trust Twilio?
Cultivating resilience to handle multiple consecutive interviews
Is Dell better than IBM?
Is it hard to apply for Google?
What is coding round in an interview?
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.