CSC 313 — Software Engineering

Introduction to Git and Github

Overview

In this assignment you'll learn how version control is used in software engineering, how to use the git version control system, and how to use GitHub for collaborative software development.

Introduction

Version control systems (VCS) allow developers to maintain a record of how their code has changed over time. When used properly, a VCS can help a developer track down the exact point in time when a bug was introduced or fixed, easily undo changes, and collaborate with other developers.

There are many types of version control systems. Some of the more popular ones include CVS, subversion, mercurial, and git. In recent years, git has quickly become the most popular of the group.

Git

Git stores files in a type of database called a repository. Most software teams that work with git keep a central repository on a server somewhere that everyone on the team can access. This repository not only stores the files, but also the history of every change made to each file, including who made the changes and when those changes were made.

Even when working alone, professional developers use version control because it easily allows them to see what's changed in a file over time, as well as to restore a file to any point in its history.

Git works with groups of changes called commits. A single commit might have many changes associated with it. Those changes might include updates to existing files, the addition of new files, or the removal of files.

Imagine a developer named Sally who has started a new job for US Robotics. She's told that her first assignment is to fix a bug in the positronic brain code that is causing all of the robots to randomly attack ice cream shops. She takes the following steps:

Step One: First, Sally will clone the central repository. This creates a copy of the repository on her computer.

git clone https://github.com/us-robotics/brain.git

Step Two: Next, Sally finds the bug in the PositronicBrain.java file that is causing the odd behavior. She quickly fixes the bug and saves her changes.

Step Three: Sally's next step is to add the PositronicBrain.java file to the list of changed files to commit.

git add PositronicBrain.java

Step Four: When Sally is done adding files, she will commit those changes, adding a brief message to describe her changes.

git commit -m "Fixed the bug that made robots attack ice cream shops."

Step Five: Now that her own changes are finished, before she can share them, she must pull any changes her teammates have made from the central repository into her local copy.

git pull

Step Six: After making sure that there isn't a conflict between her teammates' changes and her own, she is ready to push her changes up to the central repository.

Most of your interactions with a git repository will follow the same six steps that Sally followed. Note the sequence of the pull and push commands.

This is critical when working with git: Always pull before you push.

GitHub

One of the reasons that git is so popular is because of GitHub. GitHub is a site that provides repository hosting. You can sign up for a free account and host public repositories that anyone can have access to, or you can sign up for a paid account, which allows you to have private repositories that only you and your team have access to.

By using Github as the central repository, teams can collaborate all over the world, keeping their code in sync easily.

Assignment Part 1

In order to interact with a git repository, you need to have the git software tools installed on your computer. In addition, you will need to sign up for an account on GitHub. While anyone can sign up for a free account, as a student you can get a student account for free, which allows you to also have private repositories.

  1. Sign up for a GitHub Student Pack.

  2. Follow the steps outlined here to download and configure git on your computer.

  3. Follow steps 1 - 6 of this guide to create your repository.

Assignment Part 2

  1. Open the command line and navigate to a directory where you'd like to work. Use the git clone command to create a local copy of the repository you created on GitHub during the preparation assignment.

    You can find the URL used to clone your repository on GitHub. For example, if your GitHub name is l33tcoder, and your repository name is _pacman_, the command to clone your repository would be:

    git clone https://github.com/l33tcoder/pacman.git
    

    You will now have a new local directory named after your repository. This directory is linked to the repository on GitHub.

  2. Using your favorite text editor, create a text file called sample.txt write a short message in it, and save it to the repository directory.

  3. Next, change directories to your git repository directory and use the git status command to view the current status of your repository. The sample.txt file will appear in the list of untracked files. This means it hasn't bee added to the git repository yet.

    cd __your repository name__
    git status
    
  4. Next, use the git add command to add this file to the next commit:

    git add sample.txt
    
  5. If you rerun the git status command, you'll see that sample.txt now appears in the list of changes to be committed.

  6. Now, to push your changes up to the copy of the repository on GitHub, execute the git push command.

    git push
    
  7. Reload your repository page on GitHub, and verify that your new file is listed there.

  8. On the repository page, click the link showing your commit history.

    This will show you a list of all the commits you have pushed to the repository.

Congratulations! You now know how to add, commit, and push changes from your local repository to the central repository on GitHub.

Submit the url of your GitHub repository you created. (This should be something like https://USERNAME.github.io/REPOSITORYNAME).