- Hey, I'm Cameron McKenzie,
I'm the editor-in-chief over at theserverside.com.
If you wanna follow me on Twitter, it's cameronmcnz.
And, right now, I wanted to talk to you about
the git clean command.
Now, let me make it clear.
I'm not talking about cleaning git trees,
not cleaning git branches,
I've got tutorials on how to do that
with squashing commits and rebasing commits.
In this case, I'm actually specifically talking about
the git clean command.
So, as you can see, I've got an empty directory here.
There's nothing in it as that LS command would imply.
I can do LS dash A and you can see I got some
hidden files there but really nothing of import.
Now, let's imagine I issued a git clean command,
which wouldn't make much sense,
because that's supposed to clean untracked files.
But if I'd run it, you'll notice it
gives me this little nor F's given response,
which I think is most rude.
But, it basically says if you're gonna clean stuff,
that means deleting it.
And, version control is not about deleting,
it's about versioning things.
And, if you're gonna delete something that's unchecked,
unregistered, untracked, is what I mean,
there's gonna be no history of it.
So, make sure you know what you're doing.
But, in order to kinda demonstrate
how the git clean command works,
I think it should probably have some sort of file in here.
So, let me create a txt file.
So now, I've got one file here, and I can say, "git clean".
And it's still telling me that no F's are given
but I can say to it, "Look, okay,
I want a little bit more information."
I can say, "git clean dash N", and that's called a dry run.
And it says, "You know what?
If you run git clean right now, it'll remove Hello.txt"
because, basically, that's not a tracked file.
Then you can make it track by adding it to the index,
and if I do git clean again,
you notice that nothing comes up
because there's nothing that's ready to remove.
Of course, this example is not gonna be very interesting
if we don't have anything to remove.
So, let's add something that will be removed.
So, why don't we add a Hello.java file to that?
If we do a dry run on the git clean now,
you can see, yeah, I would remove that Hello.java file
because it's new and it's not tracked,
even though that there is still that Hello.txt file there.
Now, we all know when Java files get compiled,
that they turn into dot class files,
but maybe not everybody knows that,
but I know that and my Java friends know that.
So, let's add a dot class file in here.
Now, you don't version control compiled code.
And, I do believe that my dot git ignore file
ignores dot class files.
So, if I now do a dry run once again,
you'll notice that it says
it's still only going to remove Hello.java.
So, as you can see, the clean command
does not touch tracked files and it does not track
ignored files.
Here's another thing it doesn't track,
it doesn't track new directories.
(keyboard clicking)
And so, if I take a look here,
you can see I've got a new directory.
If I do a git status command, you can see that,
hey, there's a whole bunch of new stuff in there.
It actually looks like the git status command
doesn't even pay attention to that new directory,
but if I do a git clean and ask for a dry run
of the git clean,
(keyboard clicking)
it's still saying it would only remove Hello.java.
Now, you can actually force ignored files
and you can force directories to be ignored
by using the X and D switch.
And so, look at this now.
If I say, "Hey, git clean"
and use dash N, dash X, dash D, N for the dry run,
'cause it's not removing anything,
it's just saying it would remove it.
It's not only gonna remove the untracked file
but it's also gonna remove the new directory,
and it's also gonna remove the file
that normally wouldn't be touched
because it's protected by the git ignore file.
And so, that's all one, two, three
of those different switches daisy chained together.
Now, if you're real crazy, if you're really in a mood,
if you really just wanna be nasty,
you can always force this stuff to happen.
Not focre it, force it to happen
and get rid of that dash N.
And, if I do git clean force,
well guess what that's gonna do?
That's gonna force all those files to be cleaned
and when we talk clean,
what we're really talking about is being deleted.
So, let me pull the trigger on that.
Boom! (keyboard clicks)
All of a sudden, it says you need two dashes
in front of your force, and I knew that.
Boom! (keyboard clicks)
And there we go, it removed those files.
If I do an LS, you'd notice that the only file
that's there is Hello.txt and why is that?
Well, let's go to our git status command and it will tell us
that the reason that that's still there is because
that's been added to the index and it's currently tracked.
So there you go,
that's the ins and outs of working with some of these files
when you're working with the git clean command.
And, there you go, that's everything you need to know
about that git clean command.
In fact, I think there's some git clean interactive
that you can go through as well, that I didn't cover there.
But, I think that covers the basics of it.
Anyways, if you enjoyed that tutorial,
why don't you head over to theserverside.com.
I'm the editor-in-chief over there.
We got lots of great tutorials on DevOps and Git,
and GitHub, and GitLab,
and Enterprise Software Development in general.
So, come on over there, join the fun.
If you wanna follow my personal antics,
follow me on Twitter @cameronmcnz and follow on YouTube.