Contents:

Introduction

You may be wondering why we should use version control at all. All version control systems, including git, provide the following functionality:

Git is insurance against computer crashes

Over many years of teaching courses like CSE331, we have observed that on average about one student's computer will either die or be stolen during the quarter. Therefore, you should commit and push your work to the repository often. Committing often:

How Git Works on One Computer

Let's start by discussing how Git works when there is only one user working on one computer.

Git maintains a “repository” that sits next to the files you want it to track. (On Linux, the repository is stored in a subdirectory called .git, which you will never need to—or want to—look inside.) Inside of the repository, Git keeps track of every committed version of the files that you tell it about. It will let you look through those past versions, compare the current versions to earlier ones, or even revert to a past version if you liked that better.

Git on one computer

Here are some important operations that you may want to perform with Git:

Even if you use Git by yourself on a single computer, as you can see, it already provides benefits in its ability to keep track of past versions of files. If you end up breaking something in your code and can't figure out what went wrong, it can be useful to revert to an earlier version and then try making the same changes again in small pieces until the bug shows up again. That will tell you that the problem is likely in the last small piece that you changed.

How Git Works with Multiple Computers

Git's main purpose is to manage files being changed on multiple computers. For example, in this class, you will have versions of your files on at least three different computers:

  1. your personal computer or a computer in the CSE lab
  2. the Gitlab server, which stores the version that will be graded (see below for more information)
  3. the CSE Linux server, attu, where you will validate that your code works as expected

Those different repositories are related to each other like this:

Git on multiple computers

Each of these three computers has its own Git repository. You can use Git's push command to send changes you committed in your local repository into another repository (typically, Gitlab). And you can use Git's pull command to bring changes you checked into another repository (Gitlab) into your local repository.

For example, once your assignment is working correctly on your computer, you commit your changes locally and then push them to the Gitlab repository. Then, you can log into attu and pull, which should bring those same changes into your local repository on attu.

The normal way in which to create the links between two repositories that allow you to push and pull changes between them is to create your local repository as a clone of the other repository. When you do this, your local repository will remember the location of the repository from which it was cloned (which it refers to as the master repository). When you use the push and pull commands, they default to working with the master repository. (It is possible to tell Git about other repositories besides the one from which it was cloned, but we will not need to do so in this course.)

When Git is used on multiple computers, you get backup versions of your files on multiple machines. This can be of critical importance if your computer breaks or is lost. As noted above, this seems to happen to some student every quarter, so make sure that you not only commit your changes often to your local repository but also push them to Gitlab so that you have backups on another machine. Remember: committing your changes just means that you are telling Git to remember that version — it does not mean that you are submitting those as your solution to the homework. (Instead, you indicate your final solution using a tag.) So you should feel free to commit as many versions as you want.

Git vs Gitlab

It is important to note the distinction between git (version control software), and services like Gitlab and Github (code hosting services). The former is software for working with individual repositories (including committing, pushing, and pulling). The latter are services that store master repositories.

How Git is Used in CSE 331

For CSE 331, Gitlab contains the master repository for your code. You will create the repository on your local machine by cloning a special repository created just for your use in this course. When you are done with your assignment (or any time you want to make a backup copy), you push your changes to the master repository on Gitlab. (If your computer is lost, you can restore from your most recent change that you pushed to gitlab, so push your changes often.) When the assignment is due, the staff will clone the final version of your repository and grade it.

Git Pitfalls

Some tips on avoiding common problems while using git:

Preventing merge conflicts

Merge conflicts are possible even when you are working by yourself. To avoid them:

Merge conflicts are much more common, even likely, when working with teammates. That is yet another reason why the effort in building software tends to scale super-linearly with the size of the program.