Getting started with stogit
CS 273 (OS), Spring 2022
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
whereos/s22/usernameusernameis your username (e.g.,smith999).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~/OS~/OSdirectory 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 this step. 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.edusmith999) and password. Open your profile in the top right corner of the screen, selectpreferences, then select "SSH Keys" from the left 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.- 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~/OS/hw1link% cd
You could accomplish the same thing using these commands.~/OS/hw1 link% git add hw1.txt hw1.outlink% cd
Alternatively, you could use~/OSlink% 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 is sometimes too much, especially for project submissions.~/OS/hw1The
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 calledos/s22/usernameREADME.link% git pull origin main
retrieves thatREADMEfile. Here,originrefers to yourremote, andmainindicates the main (and only)branchof that remote repository. (Git branches help with team project development and other advanced tasks; using the one main branchmainis enough for now.)Before summer 2021, the name of the main branch was
masterinstead ofmain, butmainis now the standard (and more inclusive) name for the main branch in git.Performing the
push. Now,pushyourcommitto your remote repository onstogit.link% git push origin main
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~/OSlink% git init link% git checkout -b main link% git branch -d mastergit 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.)The
git checkoutandgit branchcommands fix an issue with the older version ofgit initcurrently on Link machines. Here's an explanation for those who know about git branches (note that these commands work correctly only for newer repositories on stogit).This version of
git initassumes that the primary branch for a new repository is calledmaster, but stogit (also github) now calls a new repository's primary branch by the namemain.The command
git checkout -b maincreates a new branchmainand switches to that branch.The command
git branch -d masterdeletes the unwanted branch namemaster. Sincemasterand the newly created branchmainrepresent exactly the same files and commit history, no information is lost.
Note: We will assume that your default directory is your working directory
~/OS(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.gitos/s22/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:
.gitos/s22/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.
Use the following git clone command
(not mkdir) to create your
working directory . ~/OS
link% git clone git@stogit.cs.stolaf.edu:.gitos/s22/username~/OS
We have created a program called stogit_wd that simplifies the creation of a working
directory and associating it with an existing stogit
repository. This program is available for Link computers. To
create working directories on other computers, you can manually
perform the same steps that stogit_wd
does, as described in the appendix.
To run stogit_wd on a Link computer,
log in and start up a terminal window, then enter
link% source ~cs273/setup link% stogit_wdIf this is the first time you have usedos/s22/username~/OS
git on a Link
computer, stogit_wd will prompt for
your name. If something goes wrong with stogit_wd, the program will provide information and
exit so you can work with a lab assistant or peer to make
corrections and try the script again.
Note: Be sure to use lower case
for the name of
your repository , and
to substitute your username (e.g., os/s22/usernamesmith999) in
place of username.
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 ~/OS, and that your stogit repository is named . os/s22/username
Case 2: To create a new (local) working directory and load it with a copy of your (remote) repository.
link% git clone git@stogit.cs.stolaf.edu:.gitos/s22/username~/OS
Case 3: A non-empty directory of your work already exists, and
you want to add that work to a non-empty stogit repository.
______