Clean untracked files in Git
Given I am a developer<br /> And I am working on a new branch in an existing project<br /> And during one of my commits I introduced a few files/folders<br /> And those files/folders are untracked (in .gitignore)<br /> And those files/folders are automatically generated (e.g. node_modules/ webpack_bundle.js)<br /> When I switch back to the main branch<br /> Then I see those files<br /> And I don't want to...
If you find yourself in the above situation, you may want to clean your untracked files. Git provides a command for that: git clean
.
This command comes with a way to see which files/folders are going to be deleted (DRY RUN):
git clean -n
You may notice that the above command does not show any untracked directories. To add directories to that list use the -d
switch:
git clean -dn
Alternatively you may choose to only remove files/dirs that are in .gitignore with the -X option:
git clean -X -dn
If you are ready to take action use the -f
switch and remove the -n
switch:
git clean -fd
Tweet