Changes, not Files

Goals

  • Learn that git works with changes, not files.

Most source control systems work with files. You add a file to source control and the system will track changes to the file from that point on.

Git focuses on the changes to a file rather than the file itself. When you say git add file, you are not telling git to add the file to the repository. Rather you are saying that git should make note of the current state of that file to be committed later.

We will attempt to explore that difference in this lab.

First Change: Allow a default name

Change the “Hello, World” program to have a default value if a command line argument is not supplied.

hello.rb

  1. name = ARGV.first || "World"
  2. puts "Hello, #{name}!"

Add this Change

Now add this change to the git’s staging area.

Execute:

  1. git add hello.rb

Second change: Add a comment

Now add a comment to the “Hello, World” program.

hello.rb

  1. # Default is "World"
  2. name = ARGV.first || "World"
  3. puts "Hello, #{name}!"

Check the current status

Execute:

  1. git status

You should see …

Output:

  1. $ git status
  2. On branch master
  3. Changes to be committed:
  4. (use "git reset HEAD <file>..." to unstage)
  5. modified: hello.rb
  6. Changes not staged for commit:
  7. (use "git add <file>..." to update what will be committed)
  8. (use "git checkout -- <file>..." to discard changes in working directory)
  9. modified: hello.rb

Notice how hello.rb is listed twice in the status. The first change (adding a default) is staged and is ready to be committed. The second change (adding a comment) is unstaged. If you were to commit right now, the comment would not be saved in the repository.

Let’s try that.

Committing

Commit the staged change (the default value), and then recheck the status.

Execute:

  1. git commit -m "Added a default value"
  2. git status

You should see …

Output:

  1. $ git commit -m "Added a default value"
  2. [master c8b3af1] Added a default value
  3. 1 file changed, 3 insertions(+), 1 deletion(-)
  4. $ git status
  5. On branch master
  6. Changes not staged for commit:
  7. (use "git add <file>..." to update what will be committed)
  8. (use "git checkout -- <file>..." to discard changes in working directory)
  9. modified: hello.rb
  10. no changes added to commit (use "git add" and/or "git commit -a")

The status command is telling you that hello.rb has unrecorded changes, but is no longer in the staging area.

Add the Second Change

Now add the second change to staging area, then run git status.

Execute:

  1. git add .
  2. git status

Note: We used the current directory (‘.’) as the file to add. This is a really convenient shortcut for adding in all the changes to the files in the current directory and below. But since it adds everything, it is a really good idea to check the status before doing an add ., just to make sure you don’t add any file that is not intended.

I wanted you to see the “add .” trick, but we will continue to add explicit files in the rest of this tutorial just to be safe.

You should see …

Output:

  1. $ git status
  2. On branch master
  3. Changes to be committed:
  4. (use "git reset HEAD <file>..." to unstage)
  5. modified: hello.rb

Now the second change has been staged and is ready to commit.

Commit the Second Change

Execute:

  1. git commit -m "Added a comment"