Skip to content

lab 16 Undoing Staged Changes
(before committing)

Goals

Change the file and stage the change

Modify the hello.rb file to have a bad comment

hello.rb

# This is an unwanted but staged comment
names = ARGV || ["World"]

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

And then go ahead and stage it.

Execute:

git add hello.rb

Check the Status

Check the status of your unwanted change.

Execute:

git status

Output:

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   hello.rb

The status output shows that the change has been staged and is ready to be committed.

Restore the Staged File

The restore command from the previous lab also works to unstage changes to a file.

Execute:

git restore --staged hello.rb

Output:

$ git restore --staged hello.rb

The restore command only unstaged the file. We can repeat the command to remove the unwanted changes from the working directory.

Restore the Committed Version

Execute:

git restore hello.rb
git status

Output:

$ git status
On branch main
nothing to commit, working tree clean

And our working directory is clean once again.