Skip to content

lab 5 Making Changes

Goals

Change the “Hello, World” program.

It’s time to change our hello program to take an argument from the command line. Change the file to be:

hello.rb

puts "Hello, #{ARGV.first}!"

Check the status

Now check the status of the working directory.

Execute:

git status

You should see …

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")

The first thing to notice is that git knows that the hello.rb file has been modified, but git has not yet been notified of these changes.

Also notice that the status message gives you hints about what you need to do next. If you want to add these changes to the repository, then use the git add command. Otherwise the git checkout command can be used to discard the changes.

Now check what the changes are.

Execute:

git diff

You’ll see that there is a line starting with a ‘-’ character which indicates a line that was removed. There is also a line starting with a ‘+’ character which indicates a line that was added. When you change a line of text, git treats it as a removal and an addition.

Output:

$ git diff
diff --git a/hello.rb b/hello.rb
index af0c87b..59fb3f2 100644
--- a/hello.rb
+++ b/hello.rb
@@ -1 +1 @@
-puts "Hello, World"
+puts "Hello, #{ARGV.first}!"

Up Next

Let’s stage the change.