Using git (Swarthmore GitHub Enterprise)

Accessing Swarthmore GitHub

The web address is https://github.swarthmore.edu. You should be able to log in with your typical Swarthmore credentials (the same username / password you use for email). Upon logging in, you should see a welcome page that lists the repositories you currently own.

For CS 43, we'll be using a feature of GitHub called "organizations", which allows us to control everything for the course. The organization this semester is named CS43-f15. From the main GitHub page, you can select the organization you want to use, or you can jump directly to the CS43-f15 organization.

On the organization page, you should see a list of repositories that you can access. The repositories will be named according to what they contain and which users can access them. For example, suppose two teammates (Sterling Archer, sarcher1, and Lana Kane, lkane1) are working together. When Lana accesses the organization, she would expect to see:

One-time git / GitHub configuration

Before using Swarthmore's GitHub Enterprise, you'll need to complete the following configuration steps once:

1. Create an ssh key

SSH (Secure SHell) is a mechanism that allows you to interact with remote machines and issue commands to them. It typically uses a username and password, but in some cases (and this is one of them), you need something else: a cryptographic key.

If you already have an ssh key, you can skip this step. If you're not sure, you probably do not already have a key. You can check by issuing the command:

ls -l -a ~/.ssh

If you see files named id_rsa and id_rsa.pub, you already have a key. If those files aren't there, or you're told that you have no such directory named .ssh, you'll need to generate a key.

GitHub has a good comprehensive guide on the subject, but I'll give you the executive summary below:

Run the command ssh-keygen. This will give you the output:

 $ ssh-keygen
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/[username]/.ssh/id_rsa):

Press enter to confirm the default location of /home/[username]/.ssh/id_rsa. Next, it'll ask you for a passphrase:

 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:

Set a passphrase that you'll remember and then confirm it a second time. After confirming your passphrase, it'll print a key fingerprint and some strange abstract ASCII artwork that you can safely ignore.

2. Log in to Swarthmore's GitHub Enterprise and add your key.

Next, we need to let GitHub know about this key we just created. Head over to https://github.swarthmore.edu and log in using your typical Swarthmore account credentials (same account you use for email). It may ask for your name, email address, or other information. Fill that in.

When you're properly logged in, click the small gear icon at the top right of the page to get to your account settings. Choose SSH Keys from the menu on the left. Click the Add SSH key button, and two boxes will appear. Fill in the name with anything you like, this is just to help you remember where you generated the key. I would suggest a name like CS account.

After you've named your account, you need to copy in the entire contents of the id_rsa.pub file that you generated earlier. Make sure you use the file ending in .pub. You can dump the contents of the file by executing:

 cat ~/.ssh/id_rsa.pub

Copy the entire output of that file, paste it into the Key box on GitHub, and click Add Key.

3. Git client user configuration settings

Before you starting using the git command on the command line, you need to give it a basic configuration. This will tell git who you are, making it easy for you and your partner to identify who committed code to your shared repository.

Replace the email and name strings with your email address and name. If you have not run these commands, then the very first git commit will fail and tell you to run them.

git config --global user.email "username@swarthmore.edu"
git config --global user.name  "Your Name"
git confit --global push.default simple

Cloning a local copy of the repository

With git, you and your partner both create a local copy ("clone") of the shared repository, make and commit changes locally, and push the changes back to GitHub, where your partner can retrieve them.

Click on a repository, and it'll take you to a page that lists its contents (and many other useful things). One the lower right hand side, you'll see some text that says "You can clone with HTTPS, SSH, or Subversion". Click the "SSH" link. This will give you a link that you can use to clone. Let's first enter our cs43 directory and make the clone there:

    cd
    cd cs43/labs

From here, you can use git clone [URL] to locally clone the repository, where [URL] is the SSH URL given to you by GitHub. For example, if Lana were cloning, she might use: "git clone git@github.swarthmore.edu:CS43-f15/Lab1-lkane1-sarcher1.git". You can now cd into your repository directory and access the files there.

If you get authentication errors, make sure you've uploaded an SSH key to your GitHub account. (See: the "one-time configuration steps" above)

Using your shared repo

At this point, you now have a master git repository for your project that only you, your partners, and graders/instructors can access. All changes, additions, deletions, to this project can be done with git commands that will push and pull changes from this remote master copy.

You and your partner will work in your local private copies of the repo. You can add and commit changes to your private copy as you go, and choose when to push your change to the remote master repo. Once you push changes, your instructor or partner can then pull from the remote master into their copy.

An example of a sequence of common git commands:

 # check the status of your repo
 $ git status

 # see the changes to the file (compared to your last local commit):
 $ git diff blah.c

 # add a new file to the repo on the next commit:
 $ cp ~/private/foo.c .
 $ git add foo.c
 # or add new changes to an existing repo file to the next commit:
 $ vi blah.c      # edit with your favorite editor
 $ git add blah.c

 # commit your changes (adds and deletes) to your local repository:
 $ git commit

 # push committed changes from your local to the remote master repository
 #  note: the first time you push, you may need to do:
 #        git push origin master
 $ git push

 # your partner can now pull your changes from the remote into their local:
 $ git pull

Some notes about these commands:

.gitignore

It is useful to add a .gitignore file to your repo. It tells git automatically ignore adding certain files or file types to the repo, such as .o and executable files and editor backup files (like vim .swp files). This way if you do a git add git doesn't try to add all kinds of kooky files to the repo. Here is an example .gitignore file for a C program that build an executable file named myprog:
*.o
myprog
*.swp
You add a .gitignore file to a git repo just like you add any other file:
git add .gitignore
git commit
git push

Common commands:

git help    # list common git commands  git help --all lists all commands
git add     # add changes or new files to the next commit
git rm      # remove a file at the next commit
git commit  # commit your changes to your local repository
git push    # push your committed changes to the remote master repository
            # (note: the first time you may have to do: push origin master
git pull    # pull changes pushed to remote master into your local copy
            # (git will try to merge changes into your copy)
git mv      # move a repo file
git diff    # see a diff between a file(s) and latest commit (local repo)
git diff master origin/master  # diff between your copy and master's
git status  # see what has changed between your copy and the master version
git branch -a # see all the branches (likely only master)

git tools:

Troubleshooting

Links to git Resources


git documentation sections 1 and 2 in particular are very useful
git user's manual
Andy's git wiki page more complete coverage of common and advanced git features
git cheatsheet
git magic a good source of information about more advanced and obscure git functionality