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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github