Yesterday I spent several hours debugging a problem with my git repo that wasn't fixed by git reset HEAD --hard because the files causing the problem were ignored by .gitignore. Is there a way to "flush" or "clean" a git repo of all files that are being ignored, so that only the files tracked by git are present?
I finally fixed my problem by deleting the repo and cloning it from github again, but in the future, I would like to immediately remove all potentially problematic files (those that are being ignored).
The git clean command also allows removing ignored files and directories.
git clean -dfX git-clean - Remove untracked files from the working tree-d for removing directories-f remove forcefully-n Don’t actually remove anything, just show what would be done.-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
If the ignored files are already added to the index/staging, you must remove the files from the tracking index before using the above clean command.
git rm -rf --cached .
Then add the files except the ones mentioned in the .gitignore file
git add .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With