GIT submit - how to use an "examples" repository

Often instructors have code samples they wish to share with their students. Much like your instructor can provide you with a starting point for your assignments using the GIT submission system, she can also provide a repository containing one or more examples that you can then experiment with and build on. Typically your instructor will have a single such "examples" repository with each example occurring on a different git "branch". To work on a given example, you "check out" the branch you want to work on. If you would like to save your work for future reference, you can commit to that branch just like when you are working on an assignment. You are not, however, allowed to push to the server. If your instructor adds examples, you can "pull" them in, which will typically result in new branches being created for your use.

This tutorial demonstrates how to clone and experiment with a repository of examples provided by your instructor. It is similar to forking and cloning an assignment repository, but there is no server-side fork, as there is no need for you to push your changes. Instead, you clone the instructor's repsitory directly, and can then check out branches, each of which typically correspond to different examples.

Getting started

A sample repository is referenced throughout the tutorial below - the following transcript shows how to access it. Commands you should type are shown in bold.

$ cd ~
$ mkdir csci099
$ cd csci099
$ git clone csci:csci099/Examples
Cloning into 'Examples'...
remote: Counting objects: 16, done.        
remote: Compressing objects: 100% (13/13), done.        
remote: Total 16 (delta 3), reused 0 (delta 0)        
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (3/3), done.
$ cd Examples

Overview of repository

This repository has three C programs that might be given to students in an introductory course in C programming. They are deliberately simple so that we can focus on the use of GIT rather than the actual code being demonstrated.

Run the gitk command to get an overview of the repository and its branching structure:

$ gitk --all&

You should see a simple tree in the upper left pane that looks like this.

The green boxes containing labels (master, HelloWorld, GoodbyeWorld and Loop) are branches in this repository.

Notice that the master branch has a yellow dot - this is the current branch, as shown by the command below:

$ git branch
* master
$

This is the main starting point of all the examples the instructor has provided. Its only content is README, .gitignore and .git repository:

$ ls -al
total 20
drwxr-xr-x 3 juhl juhl 4096 Oct 15 10:47 .
drwxr-xr-x 3 juhl juhl 4096 Oct 15 10:18 ..
drwxr-xr-x 8 juhl juhl 4096 Oct 15 10:47 .git
-rw-r--r-- 1 juhl juhl   43 Oct 15 10:18 .gitignore
-rw-r--r-- 1 juhl juhl   36 Oct 15 10:18 README
$ 

HelloWorld

Working upward in the tree along the right path, the next green box is labelled HelloWorld - this is a branch containing the standard "hello world" program in C. To see this version of the program, try the following:

$ git checkout HelloWorld
Branch HelloWorld set up to track remote branch HelloWorld from origin.
Switched to a new branch 'HelloWorld'
$ ls
README  t.c
$ cat t.c
#include <stdio.h>
int main() { printf("hello world\n"); return 0; }
$ make t
cc     t.c   -o t
$ ./t
hello world
$ 

As an experiemnt, edit t.c and change the output message to have initial capital letters on each word, then add and commit your changes:

$ edit t.c
$ make t
$ ./t
Hello World
$ git add t.c
$ git commit -m "Capitalized message in hello world program."
[HelloWorld cfaba89] Capitalized message in hello world program.
 1 file changed, 1 insertion(+), 1 deletion(-)
$ 

If you Update your gitk window (hit F5), you should see that HelloWorld now has a new commit whose parent is the remote HelloWorld branch.

GoodbyeWorld

If you look closely at the gitk window, you should see a GoodbyeWorld remote branch. Check it out now:

$ git checkout GoodbyeWorld
Branch GoodbyeWorld set up to track remote branch GoodbyeWorld from origin.
Switched to a new branch 'GoodbyeWorld'
$ ls
README  t  t.c
$ cat t.c
#include <stdio.h>
int main() { printf("hello world\ngoodbye world\n"); return 0; }
$ ./t
Hello World
$

So, there is a new version of t.c, but the executable t is unchanged(!) - this is because GIT ignores files it is not tracking. If you want to try the new code, you need to compile and run it:

$ make t
cc     t.c   -o t
$ ./t
hello world
goodbye world
$ 

Updating the gitk window you should now see a new local GoodbyeWorld branch that is currently the same as the remote (instructor) version of this code. As above, feel free to modify this program and make one or more commits - update gitk after each commit to see what has changed.

If you make a change to t.c that you would like to discard, run the following to get back to the most recent committed version on the current branch:

$ git checkout HEAD t.c
$ cat t.c
#include <stdio.h>
int main() { printf("hello world\ngoodbye world\n"); return 0; }
$

On changing branches

When changing from one branch to another, GIT complains if there are uncommitted changes to files being tracked. To see what GIT thinks the state of the current working directory is with respect to the current branch, run:

$ git status
# On branch GoodbyeWorld
nothing to commit (working directory clean)
$ 

If you want to throw away all your uncommitted changes, you can forcefully check out another branch:

$ git checkout -b HelloWorld
Switched to branch 'HelloWorld'
Your branch is ahead of 'origin/HelloWorld' by 1 commit.
$ ls
README  t  t.c
$ cat t.c
#include <stdio.h>
int main() { printf("Hello World\n"); return 0; }
$ git status
# On branch HelloWorld
# Your branch is ahead of 'origin/HelloWorld' by 1 commit.
#
nothing to commit (working directory clean)
$
If you run ./t at this point, what is the output? Why?

Loop

The last example is on the remote branch called Loop. Check it out, compile and run it. Make some changes, commit them, and then check out either of the other branches. After each GIT operation, update your gitk window to see how it changes.


juhl_AT_viu_DOT_ca
Last modified: Thu Oct 15 12:57:14 PDT 2015