Undoing Local Changes
(before staging)
Goals
- Learn how to revert changes in the working directory
Checkout Master
Make sure you are on the latest commit in master before proceeding.
Execute:
git checkout master
Change hello.rb
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.
hello.rb
# This is a bad comment. We want to revert it.
name = ARGV.first || "World"
puts "Hello, #{name}!"
Check the Status
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
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
Updated 1 path from the index
$ git status
On branch master
nothing to commit, working tree 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.
当前内容版权归 gitimmersion 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 gitimmersion .