git-keeper Introduction
This activity is designed to introduce you to git-keeper, and to show you an example C program.
You should have gotten an email from git-keeper with a Git clone URL for this assignment.
This shows you how to use the terminal to clone an assignment, and then how to commit and push your submission when you have completed the assignment.
Navigating Directories on the Command Line
Open Terminal in macOS or Cygwin in Windows.
Once you have a terminal open you can run commands. When you are working on the command line you are always inside a working directory. If you type the command pwd
it will print your current working directory.
The files and directories you work with on the command line are also files and directories you see in Finder on macOS or in the file explorer on windows, it is just a different way to access them.
Create a directory to store assignments and in-class activities for this class. The command mkdir
creates directories. You can create a directory called cs210
which is inside your current working directory like this:
mkdir cs210
Now enter this directory with cd
(which stands for change directory):
cd cs210
Now that you have changed directories, run pwd
again and you will see that you are in a different directory.
Configuring Git
When you commit things to a Git repository, Git needs to know your name and your email address.
Set your email address like so:
git config --global user.email youremail@wooster.edu
And set your name like so (note that the quotes are required):
git config --global user.name "Your Name"
Cloning with Git
Now that you have Git configured, you can clone the in-class activity. Replace the URL in the command below with the one you got in the email:
git clone nsommer@gitkeeper.wooster.edu:/home/nsommer/nsommer/cs210/gitkeeper_intro.git
You will be prompted for a password. Enter the password you received in the email from git-keeper. 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 'gitkeeper_intro'...
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 gitkeeper_intro
.
Navigating into the Cloned Directory
Run the following command to change your working directory to the directory you just cloned:
cd gitkeeper_intro
Now run ls
and you should see a single file called command_line_args.c
.
Editing the File
Open up command_line_args.c
in a text editor.
If you are on Windows and you installed Cygwin at C:\Cygwin
, you should look in C:\Cygwin\home\<your user>\cs210
. Note that in Windows directory names in a path are separated by backslashes (\
) whereas in the Unix-style terminal (such as in Cygwin) they are separated by forward slashes (/
).
On macOS you need to look in the home directory (the directory that is named the same as your username) for the cs210
folder. Your home directory might not show up in the sidebar in Finder. To enable this, open up Finder, go to the Finder menu, select Preferences, go to the Sidebar tab, and check the box next to the house icon which is labeled with your username.
Note that command_line_args.c
is an empty file. Past the following program into the editor and then save the file:
/**
* This is a simple example C program that prints information about the command
* line arguments that were passed to it.
*/
// stdio.h gives you I/O functions such as printf() and scanf()
#include <stdio.h>
// string.h gives us strlen()
#include <string.h>
// If main is written this way with these two parameters, argc is the number of
// command line arguments, and argv is an array of strings which contains the
// command line arguments.
int main(int argc, char ** argv) {
for(int i = 0; i < argc; i++) {
// strlen() gives you the length of a string, not counting the '\0'.
// size_t is an unsigned integer type used for sizes.
size_t length = strlen(argv[i]);
// printf() takes a string to print, and values to substitute for the
// conversion strings. %i (or %d) is the conversion string for an int,
// %s for a string, %zu for a size_t
printf("argv[%i] is \"%s\", which contains %zu characters\n", i,
argv[i], length);
}
// returning 0 indicates success
return 0;
}
Compiling the Program
Compile the program using gcc
like so:
gcc command_line_args.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 compiled programs a.out
on macOS and a.exe
on Windows. Type ls
and you should now see 2 files: a.out
(or a.exe
) and command_line_args.c
.
You can run a.out
like this. For a.exe
just substitute exe
for out
:
./a.out
You should see the following output:
argv[0] is "./a.out", which contains 7 characters
When you ran this program there was a single command line argument, "./a.out"
. You can pass additional arguments to the program:
./a.out some more arguments
This produces the following output:
argv[0] is "./a.out", which contains 7 characters
argv[1] is "some", which contains 4 characters
argv[2] is "more", which contains 4 characters
argv[3] is "arguments", which contains 9 characters
Take a look at the program to see what is happening.
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, and then followed by the file you want to compile. In other words:
gcc -o command_line_args command_line_args.c
This compiles command_line_args.c
and names the compiled executable file command_line_args
. You can now run this program by running ./command_line_args
.
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.
Comitting your Changes
You have edited the file, 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: command_line_args.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
command_line_args
no changes added to commit (use "git add" and/or "git commit -a")
Git noticies 2 things: the file command_line_args.c
has changed since the last commit, and there is a new file called command_line_args
that is not tracked by Git.
We want the change to the source file command_line_args.c
to be committed to the repository but we do not want to commit the executable file command_line_args
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 command_line_args.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: command_line_args.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
command_line_args
where the modification is now green. We are now ready to commit!
Run the following to commit:
git commit -m "Modified command_line_args.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 escape, type :q!
and press enter. That will quit vi
without saving your changes.
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 nsommer@gitkeeper.wooster.edu/home/nsommer/nsommer/cs210/gitkeeper_intro.git
a811a69..ef01028 master -> master
You may see a warning about the default push behavior not being specified, which you can ignore.
Email From Git-keeper
Now check your email. You should have an email from git-keeper saying your submission was received.