How to remove untracked files in Git?
Use the git clean command. It deletes untracked files, so first do a dry run with -n.
Terminal output
$ git clean -n
Would remove temp1.txt
Would remove temp2.txt
$ git clean -f
Removing temp1.txt
Removing temp2.txt
$ git status -s
?? cache/
The first command shows what will be deleted. The second deletes the files. The folder cache is left, because folders need the option -d:
Terminal output
$ git clean -fd
Removing cache/
$ git status -s
| Option | Meaning |
|---|---|
-n | Dry run, only show |
-f | Force, delete the files |
-d | Also delete untracked folders |
-x | Also delete ignored files |
Warning: deleted files do not go to the recycle bin and cannot be recovered by Git.
Learn more about the similar topics:
| Tutorials |
|---|
| Git Init |
| Git Status |
| Git Add |
| Git Commit |
| Git Log |
| Git Diff |
| Git Ignore (.gitignore) |