How to delete all untracked files at once in git?

1

Running git status in a folder:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   b.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   a.txt

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

        c.txt
        d.txt
        e.txt

What is the best way to delete the files c.txt , d.txt and e.txt of this folder without touching the files a.txt and b.txt ?

    
asked by anonymous 30.12.2017 / 16:00

2 answers

2

You can use the command git clean -f .

If you want to check which files would be deleted before executing the deletion, you can use the -n option, eg:

git clean -n

The output would be:

Would remove c.txt
Would remove d.txt
Would remove e.txt

When executing the command git clean -f , the output is:

Removing c.txt
Removing d.txt
Removing e.txt

Documentation

    
30.12.2017 / 16:21
2

When I want to return with the repository to the same state after a clone, for example, it is not enough to have no files to be "committed", since it is possible to have files that are ignored by .gitignore.

During the development process, when we run the application to debug, the compiler creates intermediate files, to link the code, or the EXE file itself or a dll, or any other compilation result.

git clean -fxd
  • f or --force, to force removal of files
  • x, to remove ignored files, this option is interesting, because it removes files created in a build, for example, (obj, exe, ...)
  • d, remove in all directories
03.01.2018 / 00:35