Quick Guide to Using Git and Github

In this class the labs will be released on Github, and you will "submit" the labs by pushing your code to Github (and then filling out the Gradescope assignment to tell me you're done).

Cheat Sheet

These commands are described in more detail in the sections below.

Command Line

Command Usage
git clone <repositorylink> Get the code from Github to your personal computer. Run once per lab.
git add <filename> Indicate that you have modified filename and are ready to update it on Github.
git commit -m "message goes here" Commit the new changes to Github, with a message that tells yourself and others what you changed.
git push Push the code to Github---this is the final step to putting your changes on Github.
git status Checks the current status---will show you if you have any changed files that are not recorded on Github, or any commits that have not been followed by a push.

Cloning from Github

The first step to start working on any code from Github is to clone the repository---essentially you are creating a copy of the code on your personal computer.

Finding the Link

The clone command needs you to specify which repository you want to clone, and you do this by giving it a "link" to the repository. This link can be found on the Github webpage for that repository. In this example, I'll be using a repository in the ucsb-cs16-1-u22 organization (which is the same one you all should be added to). First, I open up the repository on Github:
Screenshot of exampleRepo in my web browser
From here, go to the green button in the upper right that says "Code" and click on it to open a small window:
Screenshot of code button opened
Here you want to make sure you are on the ssh tab of the little window, and then you can click the button on the right that looks like two boxes to copy the link.
Annotated screenshot of code button opened

Cloning using the link

From here, go to your command line and paste the link at the end of your clone command:
git clone <link>
In this case, it looks like:
git clone git@github.com:ucsb-cs16-1-u22/exampleRepo.git

If this command runs properly, you will see something like this: Clone command output
If you instead see a message about needing permissions, there are two important things to check: If you've gotten the link using the steps above, you don't have to check if you have access---you would not have been able to see the repository on Github without access.

If you check the contents of the directory right now with ls, you'll find that there is a new folder with the name of the repository (in this case, exampleRepo). You can use the cd command with the name of that folder to move into the folder and check that all of the files are there.

Updating the code on Github with your changes

Once you've made some changes to the code, you will need to run three commands (in order) to get your changes onto Github. These are add, commit, and push. The first step, however, will often be to run git status to check which files you've changed. Here is an example output from git status, after I made a small change to the main.cpp file and also created a new file in this directory called extra.cpp:
Output of git status after making changes
You can see here that main.cpp is under "Changes not staged for commit", which means this is a file that previously existed, and has been modified, and that extra.cpp is under "Untracked files", which means it is a new file that Github doesn't even know about yet. From here, the next step is to add the files:
git add main.cpp
git add extra.cpp
After this, if we run git status again, we can see that both of these files are listed in green:
Output of git status after adding changed files
From here, the next step is to commit the files, which is done by running the following command:
git commit -m "informative message here"
This allows us to give a message about the code we're adding. Your commit messages don't have to be long, but they should give some basic information about what you did to the code between the last commit and this one.
Running git status here will show us a message like this:
Output of git status after commit
Finally, the last step is to push the changes to Github. This is done by running:
git push
This command might take a moment, since it has to access the internet. It should give an output like this:
Output of successful git push
At this point, if you check on the repo in your browser, you should see the new changes (you may have to reload the page).