Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Assuming that you want to physically remove "untracked" files and folders from the current working directory so that you can easily use git add --all. This where git clean -fd comes in handy. Remember there is no coming back from this command. Use -n or --dry-run to preview the damage you'll do. For more information, click git clean link.


Check the current status


inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: file_two.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: file_one.txt

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

dir_one/
file_six.txt

Remove only files


As you can see "file_six.txt" has been removed.


inanzzz@inanzzz:~/project$ git clean -f
Removing file_six.txt
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: file_two.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: file_one.txt

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

dir_one/

Check the current status


inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: file_two.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: file_one.txt

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

dir_one/
file_seven.txt

Remove files and directories


As you can see "file_seven.txt" and "dir_one/" have been removed.


inanzzz@inanzzz:~/project$ git clean -fd
Removing dir_one/
Removing file_seven.txt
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: file_two.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: file_one.txt