Skip to content

delete untracked files with git clean

homepage-banner

TL;DR

git clean -nxdf
git checkout . && git clean -xdf

How to Use Git Clean

Git clean is a command that allows you to delete untracked files from your working directory. To use Git clean, you need to open your terminal and navigate to the root directory of your repository. Once you are in the root directory, you can run the following command to delete all untracked files:

git clean -f

This command will delete all untracked files from your working directory. If you want to preview the files that will be deleted before running the command, you can use the dry-run flag:

git clean -n

This command will show you a list of the files that will be deleted, but it will not actually delete them. Once you have reviewed the list of files, you can run the original command to delete them.

Using Git Clean with Options

Git clean also provides additional options that you can use to specify which types of files to delete. For example, if you only want to delete untracked files that are ignored by Git, you can use the following command:

git clean -fX

The -X flag tells Git to only delete ignored files. If you want to delete both untracked files and directories, you can use the -d flag:

git clean -fd

This command will delete all untracked files and directories.

Conclusion

In conclusion, Git clean is a powerful command that allows you to delete untracked files from your working directory. By default, Git clean will delete all untracked files, but you can use options to specify which types of files to delete. As with all Git commands, it is important to use Git clean with caution to avoid accidentally deleting important files.

Reference

  • https://git-scm.com/docs/git-clean

Appendix

## dryrun 检查哪些文件待删除
git clean -n
## 删除当前目录下没有被track过的文件和目录
git clean -df
## 删除当前目录下所有没有track过的文件,包含.gitignore文件里面的文件和目录
git clean -xf
Leave a message