Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Discarding File Modifications2:59 with Jay McGavren
Made changes to a file, but want to get the original version back? Git can help!
- 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) modified: medals.html
- 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.
In the previous video, we staged a change to medals.html that added
a heading announcing a 100% off sale.
We decided that it was a bad idea, so we unstaged the change.
But the medals.html file saved in our working directory still has that 100% off
Now, this change is small enough that it would be easy to go in and
But in a more complex project, the change could effect dozens of lines.
That's why git offers a command to discard changes to a file automatically.
By the way,
remember how in an earlier video I said git can undo almost any mistake you make?
We're about to show you something git can't undo.
The changes in the medals.html 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 discarded file, make
absolutely sure that you're not discarding changes you actually wanted to keep.
Okay, with that warning out of the way, let's get rid of this 100% off heading.
Let's run the git status command, and we'll see another helpful hint here.
Use git checkout with the file name to discard changes in the working directory.
The git checkout command has a couple different uses.
If you don't specify any file names, git checkout switches between branches.
We won't be talking about branches in this course, so
we won't be covering that usage here.
If you do specify file names, the contents of those files will be overwritten
with their last committed versions.
Let's try this command out on the medals.html file.
We type the git checkout command.
Then we use 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.
Finally, we type the name of one or more files we want to discard changes for.
In this case, it will be just medals.html.
Hit Return to run the command.
There won't be any output, which as usual, just means that it was successful.
Now we can run git diff again.
Since the changes have been discarded, it no longer shows any output.
We can also run get status again, and
we'll see that the working directory is clean.
There are no files that have been modified since the last commit.
our medals.html file has been changed back its state as of the last commit.
By undoing the modifications to this file,
we have made another backwards transition here.
We've taken a modified file in our working directory and
updated its contents to look like they did in the previous commit.
We rewound the file from its modified state back to its last committed state.
You need to sign up for Treehouse in order to download course files.Sign up