Table of Contents

Using git

You may need to use git as a version control system in some of your CSCI or DSCI classes. It is also heavily used in industry, in open-source software development, and in Data Science.

If you have a particular task you need to complete for a class, you might check out the section below titled "I need to..."

Overview: What is git?

Git is a version control system. It maintains copies of files during development and allows people to share various versions of files. Git does keeping “snapshots” of files in a repository, and allowing these snapshots to be shared, updated, and merged.

When working with git, our local files can be in three states:

(Note that there may also be a remote repository, on a service like GitLab or GitHub, which keeps a copy of all of our commits.)

To use git to track changes to a project, we do the following:

  1. Initialize a git repository in a directory (or clone a repository from a server)
  2. Create files or make changes to our files in the directory. These files are now considered untracked changes by git.
  3. After we have made changes to a file, we copy the file to git's staging area. This is a temporary location before git permanently makes a copy of our changes.
  4. When we are ready, we commit the changes in our files. This moves all files in the staging area into the git archive. This creates a permanent snapshot of what the project looked like at that moment.
  5. (Optional) If we have a connection to an archive on a remote repository (on a service like GitLab or GitHub), we can push those changes to remote repository. This stage is needed to share the project with others, if we wish.

As a simple example, we will look at the process of setting up a git repository for a simple “Hello, World!” program. In the following, we will be using git on the command line. This is easy if you are logged in to cslab103, for example, where git is already installed. (There are also GUI interfaces to git; we won't cover those.)

$ mkdir HelloTest
$ cd HelloTest
$ ls
$
$ git init
Initialized empty Git repository in ~/HelloTest/.git/
$ ls -a
.  ..  .git 
#include <iostream>

int main() {

  std::cout << "Hello, World!" << std::endl;

  return 0;
}
$ ls
hello  hello.cpp 
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	hello
#	hello.cpp
nothing added to commit but untracked files present (use "git add" to track)
$ git add hello.cpp
[19:13] jhoggard@cslab100:~/cs130/HelloTest $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   hello.cpp
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	hello
$ git commit -m "Initial commit: Hello world program."
[master (root-commit) bbf762b] Initial commit: Hello world program.
 1 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100644 hello.cpp

Server: GitLab, GitHub, etc

We can also work with a remote repository, which holds a copy of all our work on a server. Common servers include GitHub and GitLab.

The department maintains its own GitLab server at codestore.cs.edinboro.edu. See your professor if you need to set up an account.

Create and clone

If you want to set up a remote repository on codestore, you can click the “New” button in the upper-left after you log in:

You can select “Create a blank project”, then fill in the name of your project, a description, set the privacy level, and create the project.

Afterwards, you will see instructions for cloning your repository to your local machine, but it is not hard: On your project's main page, click the “Clone” button and select the https address to copy. (There is a button that will copy it.)

Then on your machine (possibly cslab103), type the following in the directory where you want to clone the repository:

git clone <address>

where <address> is the https address you copied from codestore. (The repository will be copied into a directory named for the repo.)

Now you have a local copy of the repository, and by default, it is connected to the remote repository. (The remote repository gets the nickname origin.) You can work on your local copy, committing files as usual.

However, origin doesn't know about the commits on your local machine, and your local repository won't know about any updates to origin until you push or pull them.

Push and Pull

To send your commits up to origin (the remote repo) you need to push the files:

git push origin master

(The above sends your master branch commits to origin. In our case, origin is the remote repository at codestore.)

To update your local repository with any changes added to the remote repository, you need to pull the files. (Or fetch them.) The easiest way to update your local repository is to type

git pull

This will fetch any commits made on your current branch (probably master) and merge them into your current directory.

You can also separate this into two steps. The first retrieves any new snapshots on the remote repository, and the second merges the changes into your current files.

git fetch
git merge

Fork Another Repository

You may sometimes want to copy someone else's repository on codestore into your very own version of the repository. This is called a fork. (For example, your professor might provide you a basic repository for an assignment, and ask you to fork it so you can work on your own copy.)

In GitLab (for example, at codestore), you can click on the “Fork” button at the top of a repository to make your own copy:

Then you can clone your fork of the project to your own computer, and proceed as usual.

Setting Options

Common Commands

The following are common tasks to complete using git. Where we refer to GitLab, the same task can be completed with other git servers, such as GitHub, usually with very little or no change.

The .gitignore File

Often there are files we do not wish to include in the git repository. Usually we don't include object files and executables, for example, since these are generated automatically by compiling your source files. (So we don't actually need copies of these.)

If you want to automatically exclude these files so that git will always ignore them, create a file called .gitignore in the same directory as the project. In the file, enter a list of all the files you want git to ignore, one per line.

Once you have done this, you will see that (for example) a git status command will no longer list those files as untracked, and git add -A will not add those files, and so on.

"I need to..."

Some common tasks you might need to know how to complete for a class assignment: