- One of the things that confused me a lot
when I started working with git,
is what happens when you delete a file?
A file deletion in git is another file event,
just like anything else, so when you delete a file,
it's going to ask you to add it to the staging
environment, and then push it on to commit.
So let me show you what happens.
So, I'll open up this project folder
and I'm just going to get rid of this about.html
document on a Mac I can just hit command,
and then hit the delete key, and that will
move that to the trash, and, you'll notice that
it's gone in my project folder in my text editor,
it's kind of smart, it updates every so often.
But if I do a git status now, it noticed
that a file has been deleted, so it's
expecting that file to be there,
and I can do a couple of things, here.
If I want to just get the file from the
staging environment, I can just check
it out by saying, git checkout and then
about.html, and this is sort of like
an undelete command, so if I hit that,
you'll notice that the file appears over here,
and once again, it's in this project folder.
So I just set things back to the way
that they used to be, let's go ahead
and do a git status again, and you'll see
that there's nothing going on.
So let's go ahead and delete it again,
and show you what would happen
if I really did want to delete that file.
I'll do a git status here, and notice that,
just like before, it's telling me that
the changes haven't been staged for a commit,
so it notices that this file has been deleted,
and the other option I have here is to
add the deletion into staging.
Which means that I have deleted this file
and I meant to do it, using the operating
system, and this is something that
I want to push through to staging.
So I'll just do git add and then about.html.
Remember, that I can also say git add. if I
wanted to do an entire directory.
And so now, let's go ahead and clear,
and when I do get status, now,
the deletion has been moved into staging,
and now I can push that over and commit
that deletion, so that it is stored in the commit logs.
So if I want to undo this, just like before,
I can just read up here and the directions are
git reset HEAD and about.html,
so that unstages those changes,
let's go ahead and take a look at the status.
So it now notices that I still deleted that file,
now because I rolled things back,
I can now unstage the deletion,
so I can do an undelete, just by doing
git add, and then this about.html file.
Or I can also say git rm about.html.
And what that will do is, let's clear this out,
and do a git status, and it looks
pretty much like what I did before.
Now the difference is that when you do
a git rm command, it can actually
take care of deleting the file as well as
moving everything into staging.
That's actually a quicker process.
So let's go ahead and hit git reset HEAD,
and it's a good idea to sort of practice
these commands a lot, so now let's
clear, look at the status, so it should show
that my file is still deleted, and I'll do
a git checkout to get the file out of the staging
environment, back into my work environment,
so this is like an undelete, right?
So you could see that there's nothing
going on in status, and that we have the
file back here just like we did before.
So let's say that I do want to get rid
of that file, if I do a git rm command,
and then I specify the file that I want
to get rid of, that html, right, about.html,
and if I hit return, and I do a git status,
notice that the file is gone it deleted the file,
and it's no longer here either.
It automatically removes the file as well as
pushes that deletion into the staging environment.
So, this is the same thing as going
into your desktop, deleting a file,
and then issuing the git add command.
So it does both of these at once,
now, the problem is, I am so used
to deleting files using my operating system,
or by right clicking on my sublime text editor,
that I often forget that I have to now add
those things, so I actually end up using
git rm very infrequently, and most of the time
I just sort of do the deletions, and then I go,
oh, I forgot, I was supposed to add those deletions.
And I have to issue like a git
add command or a git rm command.
Which is sort of a pain, but that's
how git handles deletions.
Now, a couple of things that I want
you to notice is, because I had this
about document opened, right before
I issued this git rm command, the text editor
actually thinks that that file should
still be somewhere, and right now
it's technically only in memory.
So it's still showing me the about.html
file, even though I deleted it,
so if you wanted to, we could save it
from here, or we could just close this out
and don't save, and now my file is gone.
So, let me show you one more way
that we can handle these deletions.
So let's go ahead and git reset,
I know this is like the long way,
but we'll just set everything back to
the way it used to be, and then we'll
do git checkout about.html, so this
is like the undelete, let's do a git status,
everything should be good, we'll go ahead
and clear this out, and say that I did
want to delete those files, and I had made
like a bunch of changes, so let's
delete this thing, delete file, ok.
And say that I'd messed around with
my project and I've added some files
and I have modified some things in here
and, you know, I go to my git status,
and I notice, oh, there's not only
maybe this change, but maybe a
whole bunch of other stuff in here.
And I want to just sort of tell git
to just do everything I could do,
git add, right, but this is going to only
move the files into staging, what I can
do is say git add --all, and it's
going to handle all the moving into staging,
any deletions, all at the same time.
So now if I do get status, notice that
everything has been moved into staging,
and it's ready to be committed, that's really cool,
so git add --all is sort of a super push everything
into staging, and just don't worry about
what's been added or deleted, just kind of
grab everything and move it, and don't
worry about having to do all the other stuff.
So, once again, I'm going to just make everything
back to the way that it used to be,
but, we'll go ahead and just
make it look like it did before.
So we'll do a git checkout about.html,
and now my about file is back,
and we'll do a git status, everything should
be clean, we'll go ahead and clear this out,
and we'll have our files just the way
they used to be at the beginning of this video.
In order to get comfortable with git,
you have to master adding the leading
and moving things from one cue to another.
The ability to delete, undelete, and move files
in and out of staging, is going to give you
something that is akin to a super undo
and undelete capabilities that you haven't
had before you had access to git.