MLH 2019: Using GitHub and Git

Links to today's information

More information for using GitHub or Swarthmore's GitHub Enterprise

Some initial configuration steps you may want to take

Before using the command line inerface for git repos hosted by GitHub or Swarthmore's GitHub Enterprise, you'll likley want to complete the following configuration steps once (run from our system):

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.

NOTE: You need to create your ssh key's on the CS system. Log into a CS lab machine (or on your laptop if you want to use git on your laptop) follow these steps:

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. In your browser load: 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.

From the unix command line, run:
git config --global user.email "username@swarthmore.edu"
git config --global user.name  "Your Name"
git config --global push.default simple

Now use GitHub or Swarthmore's GitHub

Once you have done these steps, you can create your own repos to share with collaborators

On Swarthmore's GitHub Enterprise (or on GitHub):
  1. choose New to create a new repo
    • you can choose public or private (public means anyone can see it and clone a copy).
    • choose to initialize it with a README.md (this is what is displayed with you load your repo page
    • if you are using a specific language(s), you should choose a language-specific .gitignore too
    • you can add collaborators.
  2. you and you collaboartors can clone a copy of your shared repo (grab the ssh version to clone from the command line)
  3. then you can use git commands to add, commit, push and pull to shared changes

More information and examples for using git are avialable: