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 0:00 a heading announcing a 100% off sale. 0:05 We decided that it was a bad idea, so we unstaged the change. 0:09 But the medals.html file saved in our working directory still has that 100% off 0:13 heading. 0:17 Now, this change is small enough that it would be easy to go in and 0:19 remove manually. 0:22 But in a more complex project, the change could effect dozens of lines. 0:23 That's why git offers a command to discard changes to a file automatically. 0:28 By the way, 0:33 remember how in an earlier video I said git can undo almost any mistake you make? 0:33 We're about to show you something git can't undo. 0:39 The changes in the medals.html have never been committed, so 0:42 git doesn't have them stored anywhere. 0:46 And we're about to erase those changes. 0:49 When you're discarding changes to a discarded file, make 0:51 absolutely sure that you're not discarding changes you actually wanted to keep. 0:54 Okay, with that warning out of the way, let's get rid of this 100% off heading. 1:00 Let's run the git status command, and we'll see another helpful hint here. 1:05 Use git checkout with the file name to discard changes in the working directory. 1:11 The git checkout command has a couple different uses. 1:16 If you don't specify any file names, git checkout switches between branches. 1:20 We won't be talking about branches in this course, so 1:25 we won't be covering that usage here. 1:27 If you do specify file names, the contents of those files will be overwritten 1:30 with their last committed versions. 1:35 Let's try this command out on the medals.html file. 1:37 We type the git checkout command. 1:41 Then we use some special syntax that is mainly used together with git checkout, 1:43 a space, a pair of dashes, and another space. 1:48 This is not a command line option. 1:51 An option would be followed by an option name rather than a space. 1:54 Instead, it's a separator that makes it clear to Git 1:58 that any arguments that follow will be file names. 2:01 Finally, we type the name of one or more files we want to discard changes for. 2:05 In this case, it will be just medals.html. 2:09 Hit Return to run the command. 2:13 There won't be any output, which as usual, just means that it was successful. 2:15 Now we can run git diff again. 2:20 Since the changes have been discarded, it no longer shows any output. 2:22 We can also run get status again, and 2:26 we'll see that the working directory is clean. 2:29 There are no files that have been modified since the last commit. 2:32 Or rather, 2:35 our medals.html file has been changed back its state as of the last commit. 2:36 By undoing the modifications to this file, 2:41 we have made another backwards transition here. 2:44 We've taken a modified file in our working directory and 2:46 updated its contents to look like they did in the previous commit. 2:50 We rewound the file from its modified state back to its last committed state. 2:54
You need to sign up for Treehouse in order to download course files.Sign up