CS 334: Git and GitHub Tutorial

This tutorial covers the basics of using git with GitHub for CS 334 assignments.

  1. Getting your assignment repository
  2. Cloning your repository
  3. Saving and uploading your work: commit and push
  4. Updating your local copy: pull
  5. Adding a partner to your repository
  6. Troubleshooting

Getting your assignment repository

For each programming assignment, you will receive an email invitation to join the assignment on GitHub Classroom. Click the link in the email to accept the assignment. This will create a private repository for you on GitHub containing the starter code.

If you do not receive the email, check your spam folder or ask the instructor.

Cloning your repository

Once you have accepted the assignment, you will see your repository on GitHub. You need to clone it to get a local copy on your computer.

  1. Go to your repository page on GitHub and click the green "Code" button. Copy the URL shown (use HTTPS unless you have SSH keys set up).

  2. Open a terminal and navigate to where you want to put your work, then run:

    git clone <repository-url>
    

    For example:

    git clone https://github.com/williams-cs/cs334-lab1-YOURNAME.git
    
  3. cd into the new directory to see your starter files:

    cd cs334-lab1-YOURNAME
    ls
    

Saving and uploading your work: commit and push

As you work, you should periodically save snapshots of your code. Each snapshot is called a commit.

  1. After making changes, commit them with a short description:

    git commit -m "completed problem 1" -a
    

    The -a flag tells git to include all modified files. The -m flag specifies a commit message describing what you changed.

  2. Your commits are saved locally. To upload them to GitHub, push:

    git push
    

    You should push regularly so your work is backed up on GitHub.

Note

If you create a brand new file, you need to tell git to track it before committing:

git add newfile.lisp
git commit -m "added new file" -a

Updating your local copy: pull

If your partner has pushed changes (see below), you need to pull them to update your local copy:

git pull

Run git pull before you start working each time to make sure you have the latest version.

If you and your partner have both edited the same lines of the same file, git will report a merge conflict. Open the file in your editor---you will see markers like <<<<<<<, =======, and >>>>>>> showing both versions. Edit the file to resolve the conflict, remove the markers, and then commit:

git commit -m "resolved merge conflict" -a
git push

Adding a partner to your repository

For labs requiring a partner, one person accepts the assignment and then adds their partner as a collaborator:

  1. Go to your repository on GitHub (e.g., https://github.com/williams-cs/cs334-lab1-YOURNAME).

  2. Click Settings (the gear icon near the top).

  3. Click Collaborators in the left sidebar.

  4. Click Add people and search for your partner's GitHub username.

  5. Your partner will receive an email invitation. Once they accept, they can clone and push to the same repository:

    git clone https://github.com/williams-cs/cs334-lab1-YOURNAME.git
    

Both partners now work in the same repository. Remember to pull before you start working and push when you are done.

Troubleshooting

  • "Updates were rejected" when pushing: This means your partner has pushed changes you don't have yet. Run git pull first, then try git push again.

  • Merge conflict: See the instructions in the pull section above.

  • Permission denied: Make sure you have been added as a collaborator on the repository and have accepted the invitation.

If you get stuck, ask the TAs or instructor for help.