Home Schedule

Hello, World!

This activity is designed to introduce you to using git-keeper, and to compiling and running a C program.

You should have gotten two emails from git-keeper: one with a username and password for the server, and one with a Git clone URL and a link to these instructions.

Git-keeper Assignments

The workflow for most git-keeper assignments will be as follows:

  1. Receive an email with a clone URL for a Git repository on the server
  2. Clone the repository using the URL from the email
  3. Do your work in the local clone of the repository
  4. Add and commit your changes to the local repository
  5. Push your changes back to the server
  6. Check your email to make sure the submission was received and see the results of any tests that were run. If the tests did not pass, you can go back to step 3 and submit again.

Configure Git

You only need to perform the commands below once on your computer, and then Git will remember your details in the future.

  1. On windows open your WSL Ubuntu app or on Mac open the Terminal app
  2. Setup your email by running the following command replacing youremail with your Wooster username
    $ git config --global user.email youremail@wooster.edu
    
  3. Setup your name by running the following command replacing Your Name with your name
    $ git config --global user.name "Your Name"
    

Changing your git-keeper password

You can change the default password that was emailed to you by git-keeper or if you decide later to change your password.

  1. On windows open your WSL Ubuntu app or on Mac open the Terminal app
  2. Make sure you are either on campus or connected to the Campus VPN
  3. Run the following command replacing username with your Wooster username
    $ ssh username@gitkeeper.wooster.edu
    
  4. You will be prompted for a password. Enter your current git-keeper password. You will not be able to see what you are typing, just trust yourself and use the force. When you have typed your password, hit the enter.
  5. If your password was correct, you should now see a prompt that looks like this:
    git>
    
  6. Enter the command passwd and hit enter
  7. You will have to enter your current password and then your new password twice (a second time to confirm)
  8. If the terminal says you have updated your password you can now enter the command exit and hit enter to quit.

Create a CS110 directory for all your work

It is recommended to create a folder named cs110 to hold all your work. You can do this using Finder on Mac or by using the command explorer.exe . from the Windows WSL terminal window to open Windows Explorer. On Mac you might want to put your folder on your Desktop or Documents folder for convenience. On Windows adding the cs110 right to your home directory (where you might see another folder called .landscape or some files that start with .bash) will be most convenient.

Cloning a Git Repository

A Git repository stores the current version of a project's files, along with the history of changes to the repository. When you clone a repository from a server, you get a local copy of the repository on your computer.

Create a directory (also known as a folder) on your computer to store assignments for this class. This guide assumes that you will create a directory on your computer named cs110, and that your username is username. You may put the directory wherever you want, but you will have to change the commands below accordingly.

Open up a command line terminal. In macOS, do this by opening the Terminal application. If you are using the Windows Subsystem for Linux (WSL), open your WSL Ubuntu app.

Now you need to navigate to the directory that you created for the class. You can do this with the command cd (which stands for change directory). On macOS you will want to do this:


  $ cd Documents/cs110

If you are using WSL:


  $ cd cs110

If you are using Cygwin, here is the command to use:


  $ cd /cygdrive/c/Users/username/Desktop/cs110

When using the command line you are always in a working directory. You have now changed your working directory to be your class directory. You can see what your working directory is at any time by running the command pwd (print working directory). When you typed cd you told the terminal that you wanted to change directory and provided a path to the cs110 folder.

Now you can clone the repository for this activity. Copy the clone URL from the email that you received from git-keeper and use it in the command below instead of the URL that is there. In the Windows WSL command prompt you will need to right click in the Window to paste.


  $ git clone kbhowmik@gitkeeper.wooster.edu:/home/kbhowmik/kbhowmik/cs110/e01-hello_world.git

You may be asked if you are sure that you want to connect. You may type yes.

You will be prompted for a password. Enter the password you received in the email from git-keeper (or your new password if you changed it). For security reasons you will not see any characters as you type the password.

If all goes according to plan you should see output like this:


  Cloning into 'e01-hello_world'...
  remote: Counting objects: 3, done.
  remote: Compressing objects: 100% (2/2), done.
  remote: Total 3 (delta 0), reused 0 (delta 0)
  Receiving objects: 100% (3/3), done.
  Checking connectivity... done.

The command ls lists files and directories. If you run ls now you should see a single directory called e01-hello_world.

For this class you will run commands on the command line to clone assignments, compile and run programs, and submit assignments. To start, open up the command line interface (open the Terminal application in macOS or open Ubuntu in Windows).

Run the following command to change your working directory to the directory you just cloned:


  $ cd e01-hello_world

Now run ls and you should see a file named hello_world.c.

Note that you can see this file in macOS's Finder or in Windows Explorer too (for windows you will want to use the explorer.exe . to help). Open up the directory that you created and you should see the e01-hello_world folder, and inside there you should see hello_world.c.

Editing the File

Launch Visual Studio Code (or whatever text editor you installed) and open hello_world.c.

Note that hello_world.c is an empty file. Enter the following into the editor. I recommend typing it out instead of copying and pasting, so that you can start to get C under your fingers:

  
    #include <stdio.h>
    
    int main() {
      printf("Hello, world!\n");
    
      return 0;
    }
  

We will talk about what everything in this program means soon, for now you just want to make sure you can edit, compile, and run this program.

Compiling the Program

C programs must be compiled. Compiling a program turns it into machine language that is not easily read by humans but can be efficiently executed by a computer.

Compilation is not needed with interpreted languages like Python, which can run human readable files directly. This is a trade-off. After making a change to a Python program it can be run again immediately. After making a change to a C program it must be re-compiled before it can be run, but it will likely run more quickly than a Python program that was written to do the same thing.

Compile the hello world program using gcc like so:


  gcc hello_world.c

If your program compiled without any errors you should see no output from gcc.

Running the Program

Now that the program is compiled, you can run it. By default gcc names the compiled program a.out. Type ls and you should now see 2 files: a.out and hello_world.c.

You can run a.out like this:


  $./a.out

You should see the text Hello, world! printed in the terminal. Congratulations, you have now compiled and run a program in C!

Naming Your Compiled Program

a.out is not a very descriptive name for a program. You can tell gcc to name the file something else by using the argument -o, followed by the name you want to give the executable. You still need to pass gcc the name of the C file to compile too. So to compile hello_world.c and name the executable file hello_world, you would run the following:


  $ gcc hello_world.c -o hello_world

The -o hello_world portion of the command can also come before the file to compile, like this:


  $ gcc -o hello_world hello_world.c

Either way, you can now run the program by running ./hello_world.

Deleting Files

Now that our program is named something better, we don't need the a.out file anymore. You can delete it with the rm command:


  $ rm a.out

BE VERY CAREFUL WITH THIS COMMAND!! rm does not send files to the trash or recycle bin, it deletes them permanently.

Re-running Commands

You can access commands from the command line history by using the up and down arrows. Try re-compiling the program by pressing the up arrow until you see the gcc command that you last used to compile the program, and then press enter. Then use the up arrow to get back to and re-run the ./hello_world command.

Committing your Changes

You have edited hello_world.c, now you need to add your changes to the Git repository. To do this you must commit your change.

You can see what the current status of your Git repository is by running git status. Your output should look something like this:


  On branch master
  Your branch is up-to-date with 'origin/master'.
  Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   hello_world.c

  Untracked files:
  (use "git add <file>..." to include in what will be committed)

  hello_world

  no changes added to commit (use "git add" and/or "git commit -a")

Git notices 2 things: the file hello_world.c has changed since the last commit, and there is a new file called hello_world that is not tracked by Git.

We want the change to the source file hello_world.c to be committed to the repository but we do not want to commit the executable file hello_world to the repository. In general you do not want to add executable files to a git repository because they will not work on every computer, they take up more space, and they can be re-compiled on a new computer if need be.

We need to stage the change so that it will be included when we commit. To stage a file to be committed, use git add with the name of the file, like this:


  git add hello_world.c

Now run git status again and you should see this:


  On branch master
  Your branch is up-to-date with 'origin/master'.
  Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

  modified:   hello_world.c

  Untracked files:
  (use "git add <file>..." to include in what will be committed)

  hello_world

where the modification is now green. We are now ready to commit!

Run the following to commit:


  $ git commit -m "Modified hello_world.c"

The commit message, which follows -m, must be in quotes. If you run git commit without the message you will be taken to the editor vi. If you know how to use vi you can write the message there and save your changes, but vi can be difficult to learn - even saving changes is not straightforward. If you find yourself in vi and you don't know what to do, press the escape key, type :q! and press the enter key. That will quit vi without saving your changes.

Be sure to practice good commit style when writing your commits and use a declarative present tense. For example:

Once you have committed your change, run git log. This will show the history of commits. You should see the initial commit that created the repository, and your commit.

Submitting Your Changes

Now you can push your changes back to git-keeper. Run the following:


  $ git push

You will be prompted for your password. If your password was accepted and your push succeeded you should see something like this:


  Counting objects: 3, done.
  Delta compression using up to 4 threads.
  Compressing objects: 100% (2/2), done.
  Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
  Total 3 (delta 1), reused 0 (delta 0)
  To kbhowmik@gitkeeper.wooster.edu/home/kbhowmik/kbhowmik/cs110/e01-hello_world.git
  a811a69..ef01028  master -> master
  

You may see a warning about the default push behavior not being specified, which you can ignore.

It is important to note that if you don't push your commits they will never get submitted to the server for grading or evaluation. Remember that once you are working on your project, the work flow is:

  1. add changed files or new files
  2. commit changed files or new files you've added
  3. push the commits to git-keeper

Email From Git-keeper

Now check your email. You should have an email from git-keeper with the results from testing your code. If there were problems with your submission, fix hello_world.c and try again by adding, committing, and pushing again.

Grading

You will earn up to 2 points for this exercise, broken down as follows: