How to Remove Untracked Files in Git

While working with Git, you might accumulate untracked files—files that are not yet added to Git’s tracking system. These could be temporary files, build artifacts, or other generated content cluttering your working directory.

This guide will show you how to safely identify and remove untracked files to keep your workspace clean.


🔍 What Are Untracked Files?

Untracked files are files present in your working directory that Git hasn’t added to version control. They do not show up in commits unless you explicitly add them.


🛠️ How to View Untracked Files

To see all untracked files, run:

git status

Untracked files will be listed under:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

🚮 How to Remove Untracked Files

1. Preview What Will Be Removed

Before deleting, you can preview the files that will be cleaned with:

git clean -n

or

git clean --dry-run

This shows which untracked files and directories would be deleted without actually removing them.


2. Remove Untracked Files

To remove all untracked files (but keep untracked directories):

git clean -f
  • -f or --force is required to actually delete files for safety reasons.

3. Remove Untracked Files and Directories

To remove both untracked files and untracked directories:

git clean -fd
  • -d allows removal of untracked directories.

4. Remove Ignored Files Too (Use With Caution)

To remove untracked files, directories, and files ignored by .gitignore:

git clean -fdx
  • -x includes ignored files in the cleanup.

⚠️ Important Notes

  • Always run git clean -n or git clean --dry-run before actually deleting files.
  • git clean cannot be undone, so double-check what you’re deleting.
  • Removing ignored files (-x) is rarely needed and should be done carefully.

🧠 Summary of Commands

TaskCommand
Preview files to be removedgit clean -n or git clean --dry-run
Remove untracked files onlygit clean -f
Remove untracked files & directoriesgit clean -fd
Remove untracked files, directories & ignored filesgit clean -fdx

🏁 Conclusion

Keeping your Git working directory clean from untracked files helps avoid clutter and accidental commits of unwanted files. Use git clean carefully to remove these files and maintain a tidy repository.

Sharing Is Caring:

Leave a Comment