How to delete all untracked files at once in git?

Asked

Viewed 1,498 times

1

Whirling git status in a briefcase:

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 files c.txt, d.txt and e.txt of that folder without touching the files a.txt and b.txt?

2 answers

2

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

During the development process, when running the application to debug, the compiler create 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 the removal of the files
  • x, to remove ignored files, this option is interesting as it removes files created in a build, for example (obj, exe, ...)
  • d, remove in all directories

2


You can use the command git clean -f.

In case you want to check which files would be deleted before performing the deletion, you can use the option -n, example:

git clean -n

The exit would be:

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

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

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

Documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.