How Do I Remove Local (Untracked) Files from the Current Git Working Tree?

Better Stack Team
Updated on June 21, 2024

To remove local (untracked) files from the current Git working tree, you can use the git clean command. Here's how you can do it:

Before executing the clean command, you can use the -n or --dry-run option to see which files would be removed without actually removing them:

 
git clean -n

This will show you a list of untracked files and directories that would be removed.

Step 2: Remove Untracked Files

To actually remove the untracked files and directories, you can run the git clean command:

 
git clean -f

The -f or --force option is used to forcefully remove untracked files and directories.

Step 3: Confirm Removal (If Necessary)

Git will prompt you to confirm the removal of untracked files and directories unless you specify the -f option. If you're sure you want to remove them, you can confirm the removal.

Note:

  • Be cautious when using git clean -f, as it will permanently delete untracked files and directories from your working tree. Once deleted, the files cannot be recovered unless you have a backup.
  • Use git clean -n to preview which files will be removed before executing the clean command.
  • You can also use additional options with git clean to selectively remove files based on patterns or directories.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.