Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Removing a file from your working directory is not the same as removing it from your Git repo. In this video, we'll learn about the "git rm" command.
Removing a file from the working directory
tin.html
:
<h1>Check out our tin medals!</h1>
<p>Medallion: $10</p>
<p>Ribbon: $50</p>
- We've added a
tin.html
file showcasing the store's new tin medals. - If we run
git status
, we'll see the file is untracked. - So let's add it:
git add tin.html
- And then we'll commit it:
git commit -m "Add tin medals"
- But suppose we later learned that customers weren't too pleased with the new tin medals, and we've decided to drop the product.
- We can delete the file from our terminal using the
rm
command, which stands for "remove":rm tin.html
Removing a file from Git
- If we run
git status
, it still shows the deleted file:
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: tin.html
#
no changes added to commit (use "git add" and/or "git commit -a")
- It shows that the
tin.html
file has been deleted, but it shows that in the "Changes not staged for commit" section. - We can make the deletion of
tin.html
part of a commit by using thegit rm
subcommand. -
git rm
is set up to work much like the plainrm
command, so it's much like taking our previous command and stickinggit
in front of it:git rm tin.html
- Let's run
git status
again... - ...and we'll see the deletion of
tin.html
is listed in the "Changes to be committed" section now.
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: tin.html
#
- Next, we can commit as usual:
git commit -m "Remove tin medals"
- Now we can run
git status
again... - ...and this time it will show the working directory is clean.
$ git status
# On branch master
nothing to commit, working directory clean
- And if we run
ls
, we'll see that thetin.html
file is still gone. - By the way, we didn't need to run
rm tin.html
as a separate step.git rm
will remove the file from the working directory for you, if it exists.
-
0:00
[MUSIC]
-
0:04
Previously, we've shown you how to set up a Git repository and commit files to it.
-
0:09
But with just the commands you know now, when you commit a file,
-
0:12
well you're committed.
-
0:14
If you decide you need to delete or
-
0:16
move a file, you can do it in your working directory.
-
0:18
But you can't do it in the Git repo.
-
0:21
If you staged the wrong file, we haven't shown you how to unstage it.
-
0:25
In these next few videos, we'll fix all that and more.
-
0:29
We'll show you how to delete or
-
0:30
move files in the get repo as well as your working directory.
-
0:34
You'll learn how to unstage files that you've accidentally staged.
-
0:37
We'll help you reset a file's contents back to the way they looked
-
0:40
after your last commit.
-
0:42
We'll show you how to bring back a file that you've accidentally deleted.
-
0:46
And if you decide you don't like the changes you made in the commit,
-
0:49
we'll show you how to undo that commit.
-
0:51
This is the good stuff.
-
0:53
In these lessons you'll begin to see the real power of Git.
-
0:57
Ready?
-
0:57
Let's get started.
-
0:59
We've added the tin.html file showcasing the store's new tin medals.
-
1:05
If we run git status, we'll see the file is untracked.
-
1:10
So let's add it.
-
1:11
Git add tin.html, and then we'll commit it,
-
1:16
git commit -m "Add tin medals".
-
1:23
And we close our editor, satisfied with a job well done.
-
1:26
But suppose we later learn that the customers weren't too pleased with the new
-
1:30
tin medals, and we've decided to drop the product.
-
1:33
If we run the ls command,
-
1:35
it will show the tin.html file here in our project directory.
-
1:39
We can delete the file from our terminal using the rm command, which stands for
-
1:44
remove, rm tin.html.
-
1:48
Just like ls, the rm command isn't part of Git, but
-
1:51
it is standard on all Unix-like systems, so it's worth learning how to use.
-
1:56
See the teacher's notes if you want more info.
-
1:59
Running ls again will show that the tin.html file is gone.
-
2:03
But the tin.html file is being tracked by Git.
-
2:07
Will deleting the file from our working directory
-
2:10
also delete it from the repository?
-
2:12
Let's try our trusty git status command to find out.
-
2:16
It shows that the tin.html file has been deleted, but
-
2:19
it shows that in the changes not staged for commit section.
-
2:23
Why is that?
-
2:25
In Git, commits don't just add or modify files, they can delete them as well.
-
2:30
This is important because repositories can be shared across multiple computers.
-
2:35
When you decide to remove a file from your project, you don't want that file to be
-
2:39
left sitting in your coworker's copy of the project.
-
2:42
You want it to be deleted from your coworker's machine, too.
-
2:45
Making file deletions part of a commit ensures that the copies of your Git
-
2:49
repository will have those deletions applied, too.
-
2:53
And by the way, in case you're worried about others using this feature to delete
-
2:57
files from your machine, there are ways to undo the deletion of a file,
-
3:01
even after you've committed the deletion.
-
3:03
We'll see one way in an upcoming video.
-
3:06
So how can we make the deletion of tin.html part of a commit?
-
3:10
The key is to use the git rm command.
-
3:14
Git rm is set up to work much like the plain rm command.
-
3:17
So it's much like taking our previous command and
-
3:21
sticking the word git in front of it, git rm tin.html.
-
3:29
Let's run git status again.
-
3:32
And we'll see the deletion of tin.html is listed in the changes to be
-
3:37
committed section now.
-
3:39
Next, we can commit as usual,
-
3:43
git commit -m, "Remove, tin medals".
-
3:52
Now we can run git status again, and
-
3:54
this time it will show that the working directory is clean.
-
3:59
And if we run ls, we'll see that the tin.html file is still gone.
-
4:03
By the way, we didn't need to run rm tin.html as a separate step.
-
4:09
Git rm will remove the file from the working directory for you, if it exists.
You need to sign up for Treehouse in order to download course files.
Sign up