lab 14 Undoing Local Changes
(before staging)
Goals
- Learn how to revert changes in the working directory
Checkout Master 01
Make sure you are on the latest commit in master before proceeding.
Execute:
git checkout master
Change hello.rb 02
Sometimes you have modified a file in your local working directory and you wish to just revert to what has already been committed. The checkout command will handle that.
Change hello.rb to have a bad comment.
File: hello.rb
# This is a bad comment. We want to revert it. name = ARGV.first || "World" puts "Hello, #{name}!"
Check the Status 03
First, check the status of the working directory.
Execute:
git status
Output:
$ 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: hello.rb # no changes added to commit (use "git add" and/or "git commit -a")
We see that the hello.rb
file has been modified, but hasn’t been staged yet.
Revert the changes in the working directory 04
Use the checkout
command to checkout the repository’s version of the hello.rb
file.
Execute:
git checkout hello.rb git status cat hello.rb
Output:
$ git checkout hello.rb $ git status # On branch master nothing to commit (working directory clean) $ cat hello.rb # Default is "World" name = ARGV.first || "World" puts "Hello, #{name}!"
The status command shows us that there are no outstanding changes in the working directory. And the “bad comment” is no longer part of the file contents.