Home CPSC 330

Using Git


Version control is used by virtually all professional programmers to:

It works by storing code in a repository that contains all code and all history for the project.

As changes are made, they are committed to the repository.

Git is a popular version control system created by Linus Torvalds.


Git is typically used with one repository as the "official" one. This repository is hosted online as a server. Everyone in the team sends their changes to the server, and gets others updates through the server.

A company will typically host this themselves, but we will use GitHub, which provides free hosting.

Creating an Account

To use GitHub, you first must create an account:

  1. Navigate to https://github.com/.
  2. Pick a username, email and password of your choosing.
  3. Click Sign Up.
  4. Log in to your new account.

Creating a Repository

Next you will need to create a repository for your project. Only one team member should do this step:

  1. Click the new repository button.
  2. Choose a project name & description.
  3. You must choose a public repository.
  4. Check initialize with README.
  5. Add a .gitignore for Java.
  6. The license does not matter.
  7. Hit create project.

Adding Collaborators

Next, you will need to add the other team members to the project.

  1. From your project page, click "Settings".
  2. Click the "Collaborators" tab on the left.
  3. Under "Add friend", type the user names of your team members.
  4. Add each team member.

Now each team member will be able to contribute code.

SSH Keys

To be able to use GitHub, you must set up your SSH keys:

  1. Open a terminal.
  2. Run: ssh-keygen.
  3. Hit enter three times.
  4. This will create a public/private key pair that GitHub can use.
  5. Click the account settings on GitHub in the upper right.
  6. Click the SSH Keys tab on the left.
  7. Click Add SSH Key.
  8. The title can be anything such as "Lab computer".
  9. Paste the contents of ~/.ssh/id_rsa.pub" into the key area.
  10. This key will work on any machine you log into here.
  11. You can add keys for a laptop or home computer as well.


In order to be able to work on the project, you will each need to clone the repository. A clone is your local copy of the project.

  1. Navigate to your projects page.
  2. On the lower right, there is an area that contains a "clone URL".
  3. Make sure that it is an SSH clone URL. If not, click on SSH below.
  4. In a terminal window, type git clone then copy and paste the clone URL.
  5. You should see a new folder for your project.

Telling git who you are

In order for git to know who you are, you must enter your name and email:

Adding Files

You are now ready to start developing your project.

To add a new file, create it in some way (such as with vim or Eclipse). Next enter the following commands to add the file:

  1. git add filename
  2. git commit -am "commit message"
  3. git push

You should now see your file on the project's GitHub page.

Every time you add a new file, you will need to run the "git add" command.

Every time you add or edit a file, you will need to run the "git commit" and "git push" commands.


As you are working on your project, each team member will be able to add and edit files. In order to see the changes that your team members have made, you will need to pull them in:

Running the git pull command downlads changes from the server and applies them to your local files.


The biggest challenge in using git is in conflicts. This occurs when two people make changes to the same files at the same time.

Git will not let you push conflicts into a repository. It will repond with:

> git pull
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 13 (delta 7), reused 12 (delta 6)
Unpacking objects: 100% (13/13), done.
From github.com:IanFinlayson/project
   fd5ddf6..3b17dff  master     -> origin/master
Auto-merging Example.java
CONFLICT (content): Merge conflict in Example.java
Automatic merge failed; fix conflicts and then commit the result.

For each file that has conflicts, git changes the file to include both versions, yours and theirs. For example:

// Example.java

public class Example {
  Example() {
<<<<<<<< HEAD
    int x = 7;
    int x = 10;
<<<<<<<< 3b17dfff2cab572af84d304c3611479441b00367

  private int x;

In this code, we have set x equal to 7 (HEAD is our local version). At the same time, someone else committed a change which sets x to 10.

We need to edit this file to be what we want. If we want our version, delete the other code along with the lines git inserted. Otherwise, delete the code under HEAD along with what git inserted.

Once this is done, we can commit again, and push to the repository.

This may seem like a pain, but it is a lot simpler than trying to fix conflicting code manually.

Typical Workflow

Command Summary

git cloneCreate a working copy of an existing repository.
git addAdd a new file to a project, or an existing file to a commit.
git commitCommit changes made to your local repository.
git pushPush local commits to a remote repository.
git pullPull changes from a remote repository.
git rmRemove a file from the project.
git logShow the commit log.
git statusShow the current status of the repository.
git diffShow differences between different versions of files.

Git Resources

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.