Back to Resources

Git in CS 277

by Anonymous, Brad Solomon, Eddie Huang

Overview

Weekly lab assignments and exam coding questions will exclusively use Prairielearn Workspaces (both in notebook form and vscode emulation). However you are required to use git as the version control system for the larger project-based assignments in this class. While we do not require that you learn and use good version control practices, we cannot stress enough how useful a good version control system can be when good practices are used. The following is a brief list of good version control practices:

Again, the above practices are not hard and fast, nor complete, but they should help you complete your coding projects should you use git for them as well.

Course Setup (necessary only once for the entire semester)

The first time you’re accessing the CS 277 repository this semester, you will need to have a CS 277 repository set up for you. This process is simple:

  1. Visit https://edu.cs.illinois.edu/create-gh-repo/sp24_cs277.

This will setup a private github for you that you can use for this course. It will be viewable by the course staff but not by other students. This will allow them to help you while not sharing your private code with anyone else.

Create a clone of your repository

After you have built your university repo, the next step is cloning your repository onto your computer. This will allow you to save the changes you’ve made locally on the cloud while also maintaining a version control so if something goes wrong on your local machine, you can still recover all your work!

The URL of your repository will be based on your NetID and you will need to replace NETID with your NetID.

To clone your repository, run git clone using terminal or command prompt:

TIP: If you are using Visual Studio Code, you can also type this into your terminal there! It will make a folder inside whatever location you are actively coding in. You can then move it manually wherever you want.

git clone https://github.com/illinois-cs-coursework/sp24_cs277_NETID cs277git

…you can replace cs277git with whatever folder you want created. For example, you may want to call your folder just “cs277” or anything else.

Finally, move into the directory you just cloned:

cd cs277git

[Optional] Store your git credentials once and for all

Depending on your understanding of your own machine and your settings, it may be helpful to save your login information after entering it once. If you would like, run the following command while inside your Git Repo so that you only have to type in your git credentials once.

git config credential.helper store

Saving Assignment Progress (do this often!)

The main advantage of using git is the capacity to record your progress, ‘roll back’ your history if you encounter a problem, and get quick assistance from course staff when you have a clear problem in your code. To help facilitate this, you should add, commit, and push your work to your git repository frequently. This can always be done using the following commands on a command line while within your CS 277 directory:

git pull
git add FILENAME
git commit -m "REPLACE THIS WITH YOUR COMMIT MESSAGE"
git push

These four commands need to be run together to actually save your changes. Lets break down each of these commands:

git pull

This command does not actually do anything relevant to saving your code but will pull down the current version of your code as it is saved on github. You should get in the habit to do this first to prevent a conflict between the changes you have made locally and what is ‘up to date’ in your repo.

git add <FILENAME>

The add command specifies what files you are planning on saving (staging). The simplest git add <x> command will add whatever file you specify. However you can also use regular expressions to add multiple files (ex: git add * is all files, git add <folder>/* is all files in that folder). There are additional command options as well such as git add -u which stands for git add --update and will not add any new files but will stage every file you have already saved at least once. The last command is great when you are testing a function that produces output files (and don’t want to save every output but just your code).

git commit -m <MESSAGE>

This command is bundling every change you made with your short message. Your message should describe what you did since the commit. This will help both you and course staff understand what you did in a specific commit.

Some examples of “good” commit messages:

Some examples of bad commit messages:

git push

This command will push your bundled files to be saved and commit message and synchronizes it with the server. Ideally at this point you are done and your code has been committed!

Other Useful Commands

# Update your local repository with the remote repository
git pull

# Prints useful information about the present state of your
# local repository relative to the remote repository's state.
git status          

# Prints the commit history of the local repository
git log             

# When you want to redo the making of a new commit (all new edits are kept)
git reset           

# Permanently erases all un-committed changes
git reset --hard

# Permanently undo the latest commit
git reset --hard HEAD^

# Revert the changes specified by the fourth last commit in HEAD and create a new commit with the reverted changes.
git revert HEAD~3