Git in CS 277
by Anonymous, Brad Solomon, Eddie HuangOverview
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:
- Always use a good commit message which describes the changes in the commit.
- Never check in broken code. (This is more important when working in groups, but still good practice.)
- Commit regularly and frequently. For example, commit when you’re done writing a function. This allows both simpler commit messages and greater confidence in the repository.
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:
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.
Password entry On some systems, git (and other command line programs) will not display anything when you type your password. This is expected: type your password as normal, and then hit enter.
Finally, move into the directory you just cloned:
cd cs277git
Setting your active working directory If you are struggling to change your active working directory, one alternative you can do is type ‘cd’ and then drag the folder into your terminal or command prompt shell and it will autocomplete the absolute path, allowing you to move to the folder [the repo] you just made.
[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:
- “Fix the error in the data parsing function”
- “Implement the reverse helper function”
- “Completed part one of assignment”
Some examples of bad commit messages:
- “REPLACE THIS WITH YOUR COMMIT MESSAGE”
- “file submission”
- “commit”
- “HAAAAAAAAAAANDS”
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!
Oh no it didnt work!
If your attempt to git push was rejected, it usually means that your local code base is not synced up to your repo’s commit history. Course staff should be able to assist you with fixing any github commit problems, but the simple solution is to save any local files you want to keep somewhere else and then run git checkout <filename>
on the conflicted file, followed by a git pull
. This will overwrite the file locally – which is why you saved it somewhere else! You can then add any of your changes back into your code.
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