Discarding File Modifications2:59 with Jay McGavren
Made changes to a file, but want to get the original version back? Git can help!
git restore medals.html
- In the previous video, we staged a change to
medals.htmlthat added a heading announcing a "100% off sale". We decided that was a bad idea, so we unstaged the change.
- But the
medals.htmlfile saved in our working directory still has that "100% off" heading.
Warning: We're about to show you something Git can't undo. The changes in the
medals.html file have never been committed, so Git doesn't have them stored anywhere. And we're about to erase those changes. When you're discarding changes to a modified file, make absolutely sure you're not discarding changes that you actually wanted to keep!
- If we run the
git statuscommand, we'll see another helpful hint:
use "git checkout -- <file>..." to discard changes in working directory
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
- Let's try this command out on the
git checkout -- medals.html
This uses some special syntax that is mainly used together with
git checkout - a space, a pair of dashes, and another space:
--. This is not a command line option. An option would be followed by an option name rather than a space. Instead, it's a separator that makes it clear to Git that any arguments that follow will be file names.
- Now, we can run
git diffagain. Since the changes have been discarded, it no longer shows any output.
- We can also run
git statusagain, and we'll see the working directory is clean. There are no files that have been modified since the last commit, or rather, our
medals.htmlfile has been changed back to its state as of the last commit.
You need to sign up for Treehouse in order to download course files.Sign up