Lessons · Git · clearing out what was never tracked
Removing files git never knew about
git clean deletes untracked files -- the ones git has never been told about. Tracked files are left alone entirely.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
restore cannot help with a file git does not know exists, and after a bad build there can be thousands of them.
How to think about it
Always -n first. It lists exactly what would go and removes nothing, and this is the one command on this road with no undo of any kind.
Worked example
git status --shortOne modified tracked file, and untracked things beside it.
git clean -nThe dry run. Nothing has been removed.
git clean -ndWith -d it looks inside untracked directories too, which is where the surprises are.
git clean -fdNow they are gone.
git status --shortThe tracked file and its edit are exactly as they were.
Your turn
List what clean would remove, without removing anything.
git clean
Try it in a real repository
The trap
Adding -x because something was still there afterwards. -x removes ignored files too, and that is where your .env and your local settings live.
Practise clearing out what was never tracked on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.