To finish up our discussion of how we can undo changes that we've made, I want
to look at a relatively simple one which is how we can remove untracked files
from our working directory.
The idea is that if we have a lot of files that have been added to our working
directory that are not tracked that we don't want, we just want to get rid of
them, is there an easy way that we can do that?
We certainly could go through and just delete them one by one,
but Git gives us a quick and easy way to just tell all those files that should be thrown away.
So specially if you have things like log files, zipped directories, compiled
code, things like that that you don't want in your Repo, and you'd like to get
them out of your working directory too.
We can get rid all of those by using the git clean command.
Let's start by just creating some simple files.
So we'll go into TextMate, and let's just make a new file, put Junk in it, and
we'll save it, and we'll call it junk1, we'll put on the Desktop and then let's
do a Save As, and we're going to call this one junk2, and then let's do Save As
one more time, make this one junk3. So we'll close that up.
We've now got these three junk files, we're going to add all of these to
our working directory.
So now when I do git status you'll see that they show up there as untracked files.
What we want to do is get rid of them.
And the way that we just say throw them away is with git clean, and git clean on
its own won't actually do anything.
They will come and say you know what I need either a -n or a -f option.
-n is a test run, so let's try that first, git clean -n, and it comes up and it
says would not remove the folder explorers.
Don't worry about that, we'll talk more about directories later.
It says that it would remove junk1.txt, junk2, and junk3.txt, so it tells us the
files that it would remove. That's what the -n option does.
git clean -f forces it to run, and essentially we have to add this extra flag to
it because it is going to be destructive, it is going to throw away anything
that is not in our repository.
However, if we have something in our staging directory, junk1.txt like that,
now if we run git clean -f, it did remove junk2 and junk3, but it did not remove junk1.
Junk1 is in our staging index, not in our working directory, so it didn't get thrown out.
git reset HEAD on junk1.txt, now if we do git clean with the -f option, it
throws it away as well, git status is empty.
And we take a look, and we can see that all three of those files have been thrown away.
So git clean is pretty simple, especially compared with all the work that we were
doing with different kinds of resets.
It has one simple purpose, which is just to throw away the untracked files.
Now be careful when using it, because of course it does destructively throw
away these files. They're not stored in our Repo, they're not stored in our staging index.
If we throw them out of the working directory, they're not stored there
either, and they're not stored in your trashcan, they're just going to be permanently deleted.