Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Accidentally deleted a file? Git can get it back for you!
- Suppose we accidentally deleted a file from our working directory that we actually wanted to keep, like our main site page:
rm medals.html
- Git makes it pretty easy to recover the file.
- As before, let's run
git status
, and it will show 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: medals.html
#
no changes added to commit (use "git add" and/or "git commit -a")
- Also as before, there's a message saying
use "git checkout -- <file>..." to discard changes in working directory
. - This is similar to using
git checkout
to discard modifications to a file's contents. But in this case, the change we want to discard is the deletion of the file - we want to bring the file back. - If we run that command:
git checkout -- medals.html
, the file, and all its contents will be restored.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Suppose we accidentally deleted a file
from our working directory that we
0:00
actually wanted to keep,
like our main site page, rm medals.html.
0:05
We can confirm with the ls command
medals.html is now gone from the working
0:10
directory.
0:14
But there's no need to panic, this sort
of thing is the reason we're using git.
0:15
In fact it'll be pretty
easy to recover the file.
0:20
As before, let's run git status,
and it will show the deleted file.
0:22
Also as before, there's a message
saying use git checkout and
0:28
a filename to discard changes
in the working directory.
0:32
This is similar to using git checkout
to discard modifications to a file's
0:36
contents.
0:40
But in this case, the change we want
to discard is the deletion of the file,
0:41
we want to bring the file back.
0:46
Let's try that command,
git checkout -- medals.html,
0:48
as usual, there's no output,
signaling there were no errors.
0:54
If we run the ls command,
we'll see the medals.html file is back.
1:02
We can also show the sidebar,
and then edit the file.
1:07
And we'll see that all of
its contents are back too.
1:11
Git has rescued our file
from being deleted.
1:15
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up