Push, Pull, Log, Diff

Since this is Sally’s first time using Git on her desktop machine, she first sets up her .gitconfig file.

[user]
    name = Sally
    email = sally@futilisoft.com

Now Sally needs to set up her own repository instance.

~ sally$ git clone http://server.futilisoft.com:8000/ ./lottery
Cloning into lottery...
warning: You appear to have cloned an empty repository.

~ sally$ cd lottery

lottery sally$ ls -al
total 0
drwxr-xr-x   3 sally  staff  102 May 17 08:00 .
drwxr-xr-x  19 sally  staff  646 May 17 08:00 ..
drwxr-xr-x   8 sally  staff  272 May 17 08:00 .git

Hmmm. Harry was supposed to commit the initial code, but there’s nothing here.

But Harry did commit his changes! Why aren’t they here? Ah, he forgot to push. Sally screams at Harry loudly enough to be heard across the Atlantic.

lottery harry$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'http://server.futilisoft.com:8000/lottery'

By default, Git pushes only to matching branches: For every branch that exists on the local side, the remote side is updated if a branch of the same name already exists there. This means that you have to push the branch explicitly the first time.

lottery harry$ git push --all
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 484 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To http://server.futilisoft.com:8000/lottery
 * [new branch]      master -> master

Now Sally can pull.

lottery sally$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From http://server.futilisoft.com:8000/lottery
 * [new branch]      master     -> origin/master

Now that she has pulled, Sally has the code.

lottery sally$ ls -al
total 8
drwxr-xr-x   4 sally  staff  136 May 17 08:07 .
drwxr-xr-x  20 sally  staff  680 May 17 08:06 ..
drwxr-xr-x  12 sally  staff  408 May 17 08:07 .git
-rw-r--r--   1 sally  staff  555 May 17 08:07 lottery.c

Here’s another terminology difference with Git. My definition of pull is an operation which pulls changesets into a repository instance but does not update the working copy. git pull is equivalent to pull followed by update. git fetch is equivalent to pull.

Now that she has the initial code they had previously discussed, Sally is happy as a horsefly at the church picnic. She wants to check the log to see the details.

lottery sally$ git log
commit bcb39bee268a92a6d2930cc8a27ec3402ebecf0d
Author: Harry <harry@futilisoft.com>
Date:   Sat Jun 11 12:55:52 2011 +0200

    initial implementation

Note the way Git describes this commit: bcb39bee268a…. At the lowest level, a Git version ID is a SHA-1 hash. Git does support various forms of shorthand syntax, including unambiguously shortened SHA-1.

When Sally decides to take a look at the code, she immediately finds something that makes her nervous as a plump turkey in November. The program expects the red ball number to be the first argument, followed by the other five. But in the actual lottery, the five white numbers are always drawn and shown first. She worries this will be confusing for users so she decides to fix it. Fortunately it is only necessary to modify a few lines of main().

    if (argc != 7)
    {
        fprintf(stderr, "Usage: %s (5 white balls) power_ball\n", argv[0]);
        return -1;
    }

    int power_ball = atoi(argv[6]);

    int white_balls[5];
    for (int i=0; i<5; i++)
    {
        white_balls[i] = atoi(argv[1+i]);
    }

Now she uses the status operation to see the pending changes.

lottery sally$ git status -s
 M lottery.c

No surprise there. Git knows that lottery.c has been modified. She wants to double-check by reviewing the actual changes.

lottery sally$ git diff
diff --git a/lottery.c b/lottery.c
index e59c732..adf47a7 100644
--- a/lottery.c
+++ b/lottery.c
@@ -11,16 +11,16 @@
 {
     if (argc != 7)
     {
-        fprintf(stderr, "Usage: %s power_ball (5 white balls)\n", argv[0]);
+        fprintf(stderr, "Usage: %s (5 white balls) power_ball\n", argv[0]);
         return -1;
     }
 
-    int power_ball = atoi(argv[1]);
+    int power_ball = atoi(argv[6]);
 
     int white_balls[5];
     for (int i=0; i<5; i++)
     {
-        white_balls[i] = atoi(argv[2+i]);
+        white_balls[i] = atoi(argv[1+i]);
     }
 
     int result = calculate_result(white_balls, power_ball);

Ain’t that the bee’s knees!?!