Skip to content

lab 15 Undoing Local Changes
(before staging)

Goals

Checkout Main

Make sure you are on the latest commit in main before proceeding.

Execute:

git checkout main

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.
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"

Check the Status

First, check the status of the working directory.

Execute:

git status

Output:

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <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 restore command to checkout the repository’s version of the hello.rb file.

Execute:

git restore hello.rb
git status
cat hello.rb

Output:

$ git restore hello.rb
$ git status
On branch main
nothing to commit, working tree clean
$ cat hello.rb
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"
puts "You have #{names.length} names!"

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.