Software Design and Implementation
CS 251 (SD), Fall 2018
We will use a system called git for electronic
submissions in this
course. Git was originally designed for managing the
Linux source code
by Linus Torvalds, the creator of Linux. Git has since become
extremely popular for sharing and collaboratively developing code,
often through the free GitHub service. Instead of GitHub, we will use
a local git server named stogit, in order to
make the work you submit
available to graders and professors for the course but otherwise
private.
Contents:
Appendix - setting up a
gitworking directory manually.
Overview
The git system operates as a web service: you do work
on a desktop or laptop computer, then store or retrieve your work using a
git server via the Internet. We will use the
git server named stogit, located at the web URL
.
stogit.cs.stolaf.edu
Your desktop or laptop is called your local computer, and
the stogit computer is called a remote computer.
The CS program creates a
stogitrepository located on thestogitserver for each student in a course. Yourstogitrepository is the (remote) saved version of your code, and is accessible only by you and by your TAs and professors in your course. For team projects, a separatestogitrepository will be created with shared access by the team members.Gitis designed for teamwork: team members can do software development work separately, then integrate their (working) code with previous working code.Your
stogitrepository for this course named
wheresd-f19/usernameusernameis your username (e.g.,smith99).For homework, labs, projects, etc., you will typically do your work on a Link computer (RNS 202 or 203) in a subdirectory of your
directory. Your~/SD~/SDdirectory on your local computer is called your working directory.From time to time as you work, you can make a "snapshot" of your files in your (local) working directory, called a
commitingit. Thesecommitsnapshots record of your progress at a certain point of code development.It's possible to retrieve versions of your code files from prior
commits if you find you need to return to a previous state.When you have working code for some stage of your program, you can synchronize a
commitsnapshot with your remotestogitrepository across the network, using thepull/pushprocess:first
pullfrom yourstogitrepository, to check for any (remote) repository changes relative to the files in your (local) working directory; thenpushyourcommitsnapshot to your (remote)stogitrepository
Thereafter, you could retrieve the changes you
pushed tostogit, even if you accidentally delete or corrupt the code in your (local) working directory.
Key terms for talking about
git
We use these terms (introduced above) when talking about
git and our stogit server.
local - ______
remote - ______
repository - ______
working directory - ______
commit(snapshot) - ______pull/push- ______
Setting up a local client computer for stogit
Setting up a local "client" computer to use stogit for
your course requires two steps:
creating an
sshkey for safe passwordless connection to thestogitserver; andestablishing a working directory for your
stogitrepository.
Notes:
Step 1. only needs to be performed once per client computer; also, since all Link computers share your same home directory
~, you only need to carry out these steps once on one Link computer in order to usegiton any of the Link computers. But if you want to accessstogitfrom another computer that has git software installed (e.g., maybe your laptop), you need to perform Step 1 on that computer.Step 2. needs to be performed once per
stogitrepository. For example, if you have both a personal course repository and a team project repository, you will need to perform step 2. for both of those repositories.
Step 1. Creating and entering an
ssh key for stogit
When you connect from your local computer to
stogit, you need to authenticate (prove who you
are, e.g., with a password). It can get very tedious to have to enter
your password over and over for frequent stogit interactions, so
stogit provides a convenient way to authenticate that is actually
more secure than using passwords directly (because the authentication data is
far harder guess than a password is). The system is called
passwordless ssh, and you can set it up using
these steps:
Create an
sshkey. You may already have ansshkey on the Link computers: if you already have a file~/.ssh/id_rsa.pub, you can skip to step b. below. Otherwise, generate a newsshkey as follows.link% ssh-keygen -t rsa
Note: Thessh-keygencommand will prompt for an optional "passphrase" - you can enter an empty line for your passphrase.Note: The string
link%indicates that this command should be entered in a terminal window on a Link computer. Don't enter the characterslink%.Enter your
sshkey into yourstogitsettings. Visitin a browser, and log in using your St. Olaf username (e.g.,stogit.cs.stolaf.edusmith99) and password. Open yourSettings(look for a drop-down menu in the upper right corner of thestogitweb page, then chooseSettings), then select "SSH Keys" from the menu bar. Give a name to your key (such as "Link machines"), then copy and paste the public key (contents of the.pubfile above) into the larger text box. Finally, click the submit button for your key.In order to copy your public key, you can print it at a terminal window on a link machine using, e.g.,
link% cat ~/.ssh/id_rsa.pub
Note: Your
stogitprofile can hold multiplesshkeys for access from various client computers, but you need only enter onesshkey from a single Link computer in order to accessstogitfrom any Link computers. This is because all Link computers share your same home-directory files.First, make some git configuration settings on the computer, if you haven't already (e.g., for a prior course).
Notes:
These commands are illustrated here for Link computers; you only have to configure one Link computer, and all others will share that configuration.
If you are using a new non-Link computer, e.g., a Raspberry Pi, these commands must be entered on that new computer, whether or not you have entered them on a Link computer.
git config --global user.name "Your Name" git config --global user.email "username@stolaf.edu" git config --global core.editor "emacs"
If you already have files in a subdirectory
~/SD, e.g., from prior assignments, rename that directory in order to protect that work.link% mv
~/SD~/SD.buNow create your new working directory.
link% git clone git@stogit.cs.stolaf.edu:
.gitsd-f19/username~/SDlink% ls~/SDNote: This creates a new directory
. If~/SDis a never-used repository, your working directory should now contain a single filesd-f19/usernamethat contains some simple identifying information (check this).~/SD/READMEFinally, copy your prior work (if any) to your new working directory
.~/SDlink% cp -r
~/SD.bu/*~/SDlink% ls~/SDThe
lscommand should showREADME(from your repository) and also all your prior work (from.~/SD.bu- Create or modify some code files in your (local) working directory.
Use
git add filename ...to stage files intended for acommitsnapshot.Use
git commit ...to create acommitsnapshot.Use
git pull ...to check for any (remote) repository changes.Use
git push ...to upload yourcommitto thestogitserver.Steps 1 and 2: Create a
commit(snapshot) of files in your working directory.This means to record the current state of those files, stored on the local system (not yet on
stogit).Commits can bepushed to your repository onstogit(which explains why we are making thiscommit), and can also be used to retrieve previous versions of your work.Staging the files to
commit. First, usegit addto indicate which files to include in thiscommit. For example, if you want tocommitfiles namedhw1.txtandhw1.outcontained in a directory, you can enterenter~/SD/hw1link% cd
You could accomplish the same thing using these commands.~/SD/hw1 link% git add hw1.txt hw1.outlink% cd
Alternatively, you could use~/SDlink% git add hw1/hw1.txt hw1/hw1.outgit add hw1instead ofgit add hw1/hw1.txt hw1/hw1.outabove, but that would submit all files in thesubdirectory, which may be too much.~/SD/hw1For example, in C or C++ programming, only
.cor.cppfiles should be committed (unless instructed otherwise), not executables or other files that can be generated from the source files ending in.cor.cpp.
The
git addcommand records current versions of the files you indicated, in preparation for acommit. Those versions are referred to as staged files.-
Note: Git commands such as
git add,git commit,git pull, andgit pushmay be entered within a subdirectory of your working directly. Enter them wherever it's convenient; for example,cd ~/SD/hw1 git add hw1.txt hw1.out ...
may be more convenient thancd ~/SD git add hw1/hw1.txt hw1/hw1.out ...
To view the files that are currently staged (and unstaged) enter
link% git status
Note: If you stage a file withgit add, then make more changes in that file in your working directory,git statuswill list that file as being both staged (the version of that file when youadded it) and unstaged (the current version after your subsequent changes).
Creating a
commit. Now you can create thecommit"snapshot" consisting of your staged files usinglink% git commit -m "message"wheremessagedescribes what thiscommitcontains. For example, if you are submittinghw2, a good message might be "submit hw2 complete" or "hw2, part A only", e.g.,link% git commit -m "submit hw2 complete"
The
messageis the easiest way to label yourcommits, so you (and your graders!) can know whichcommits contain which files.
Steps 3 and 4: Uploading a
committo your remote repository onstogitusingpush.Since
git commitonly creates local snapshots of your work (on your computer only), you need to perform an operation calledgit pushoperation to upload your work to the remotestogitrepository.pullthe current version of your remote repository. This step retrieves any current versions of files stored in yourstogitrepository. Since we're assuming it's new, your remote repositoryinitially contains only a single file calledsd-f19/usernameREADME.link% git pull origin master
retrieves thatREADMEfile. Here,originrefers to yourremote, andmasterindicates the main (and only)branchof that remote repository. (Git branches help with team project development and other advanced tasks; using the one main branchmasteris enough for now.)Get in the habit of always pulling before you push.
The
git pullcommand firstfetches the files from the remote repository to your local.gitstructure, then attempts tomergeyourcommitwith thosefetched files. This could result in a conflict; for example,gitwould detect a conflict if your working directory has a fileREADMEthat differs substantially from a fileREADMEthat wasfetched from the remote repository.If such a merging conflict arises, the output from
git pullwill inform you about it. Anymergeconflicts must be resolved before you can proceed.Performing the
push. Now,pushyourcommitto your remote repository onstogit.link% git push origin master
You should always
pulljust before youpushtostogit. Thegit pullcommand retrieves any changes in the remote repository, thus helping to avoidpushing out of date code to the remotestogitrepository.Get in the habit of
pulling before everypushfrom the beginning. You won't notice a difference if you're the only person using a repository, but you need to have this habit in place before you share a repository in a team project.
Now, enter the following
gitconfiguration commands.link% git config --global user.name "Your Name" link% git config --global user.email "username@stolaf.edu" link% git config --global core.editor "emacs"
for your personal name and St. Olaf username.Initialize the working directory for
git.link% cd
The~/SDlink% git initgit initcommand creates agitstructure in your current directory, implemented in a new subdirectory.git. (If you ever need to remove thegitstructure from a directory, you can delete the.gitsubdirectory.)Note: We will assume that your default directory is your working directory
~/SD(the one containing the.gitsubdirectory) for allgitcommands below.Assign a
gitname to your remote repository.link% git remote add origin git@stogit.cs.stolaf.edu:
This command assigns the name.gitsd-f19/usernameoriginto your repository onstogit. Such names are referred to asremotes ingit. You can choose a name other thanorigin, butoriginis the typical name for a singleremotewhen getting started, and we will use that name in our examples.Be sure to enter the URL accurately. For example, be sure to use a colon instead of a slash just after
stogit.cs.stolaf.edu. Also, use your username, not the eight characters "username." You can check your current remote (if any) usinglink% git remote -v
If you need to correct your remote, delete it and add again.link% git remote remove origin link% git remote add origin git@stogit.cs.stolaf.edu:
.gitsd-f19/username
Note: It is also possible to interact with
stogit using a
different method, called HTTP (instead of SSH), but this requires
you to enter a
password every time you interact with the remote stogit server
from the command line. We recommend using the SSH approach
described above, since it is more convenient (not needing to enter
your password frequently) and more secure (your password isn't
transmitted over the network).
Step 2. Establish a working directory for your
stogit repository.
The basic git work cycle
Once you have successfully set up your working directory for your
stogit repository (in Part
B), you will follow a standard git work cycle to
create and modify your code, package it in commits as
you go, and pull/push working commits to
stogit, as indicated in the Overview section above. The
basic work cycle has five steps, including four git commands:
The following items elaborate on this four-part work cycle.
Appendix
Here are the steps of the stogit_wd command found
on Link computers. You can perform these manually on a non-Link
computer in order to set that computer up as a client for
stogit.
The instructions for initializing a working directory vary
depending on circumstances such as whether that directory already
exists and whether it contains files you want to add to your
git repository. We will start with the typical
circumstances for a first submission of work in a course, explaining
the steps of the procedure as we go, then
describe variant procedures for initializing a git working
directory in some other circumstances.
Case 1: A non-empty directory of your work already exists, and
you have a new stogit repository for that work.
We will assume that the directory is named ~/SD, and that your stogit repository is named . sd-f19/username
Case 2: To create a new (local) working directory and load it with a copy of your (remote) repository. ______
______Case 3: A non-empty directory of your work already exists, and
you want to add that work to a non-empty stogit repository.
______