In this movie we're going to learn how to delete files and track those
deletions using Git.
To do that, let's create a couple of files that we can throw out.
We're going to keep first, second, and third file, let's make a couple of new
ones here, and I'll just call this one This is a file to delete.
We'll save that as file_to_delete1.txt, and then we'll just do a Save As,
make it file_to_delete2. So, we'll close that up.
We've now got two files here. I'm going to add those to the repository.
If I just do git status now, these show up as untracked files.
So, if I delete these, well then they just stop being tracked files, right?
When we talk about deleting files, we're talking about deleting them from the repo.
We're talking about the things that are tracked files.
So, the first thing we need to do is add these to our repo, and then we can go
about deleting them.
So, let's do git add, and we will add everything that's in our working directory.
So now, if we do git status, we'll see that those are ready to be committed, git
commit with a message Adding I'm going to call these 'red shirt' files.
That's like the red shirts in Star Trek.
We're adding them just for the sole purpose of them being able to be deleted.
So now, if we say git status, we'll see that there's nothing ready to commit,
everything here is in the repository, including these two files that we're going to delete.
Now, I wanted to have two files because there's two different ways that we're
going to look at doing deletions.
One way is that we just simply come over here, and we'll take file_to_delete1,
and we're just going to drag it down to our trash, throw it away, that's it!
Now, if we say git status, what does git tell us about it?
It looks in the repository and says, hey!
I have a file that I was tracking called file_to_delete1.txt.
I'm looking in what you've got in your working directory, and that file is not
there, and it tells you it's been deleted.
Now if we want to delete this file, we say git add file_to_delete.
It looked in the directory and saw 2, we want file_to_delete1.txt.
We're going to git add, and it comes up and doesn't tell us anything.
Let's do git status again and take a look at what it says.
Nothing changed, it's still listed as Changes not staged for commit.
It's because when we do a delete, well we're not doing an add then we do add
whenever we're adding a file or whenever we're adding changes.
With the delete, now we have to do something different, and it tells us that here.
It says add/rm, either add changes or use rm to update what will be committed.
So rm is for remove, and that's the Unix shorthand for remove as well.
So, if we want to remove this file, I have to say git rm files_to_delete, and
we can't use auto complete, because it will pick up the one that's in the directory.
We have to actually type it out, or use auto complete and then make the edit,
git remove file_to_delete1.txt. Now, we hit Return.
It says okay, I'm going to remove that. Now, we'd git status.
Now, it comes up and says okay, changes to be committed.
That's things that are in our staging area.
Now file_to_delete1.txt is in our changes to be committed.
Now, we do a regular commit, git commit with a message Deleted first red shirt file.
First, one has gone now.
Notice that it tells us Delete mode here that was deleted, one deletion was
made, git status now.
Says everything is clean, git log, we now see that, that first red shirt
file has been deleted. Now, it's the first way to use files.
We just go into the file system and delete the file by hand, and then tell
git remove that file.
Just like we were adding changes, well now, we're doing a remove change.
An easier thing though is just to do the whole remove from Git, tell Git remove
file_to_delete2.txt.
Now auto completion does work. It finds the file.
When I hit Return, it did the exact same thing, it removed the file from
the current directory.
It did have one slight difference though, which is that it completely erased it.
It used a Unix remove, not a let's put things in the trash remove.
So, in my trash, I have file_to_delete1 because I drug it in there, file2 is not
in the trash, it has just vanished, it just completely gone.
That is a difference, if you do want to keep things around, if you want to have
that sort of fallback of having things in your trash, you can do that.
Of course, it's in our repository, so we've got a saved version of it.
If we ever need the saved version, we can use that one.
Let's do git status now and see what it tells us.
It's already in our staging area.
So, now in one step, we've both eliminated the file and added it to staging,
whereas the other one was a two-step process.
The other one we took the file to the trashcan, then used remove to put it in
the staging area, and now let's add that last file with the commit, git commit
-m and "Removed second red shirt file".
So there we go, our two files to be deleted are now removed, and we learned two ways to do it.
The first is to move the file out of the working directory, in this case into
the trash, and then tell Git about it, and the second is to tell Git to do the
delete for us, and add it to the staging directory all at the same time.