CSCI 544: Git basics

What is Git?

Git is a modern version control system. Amongst other benefits, it allows you to save snapshots of your project allowing you view (and revert to, if necessary) previous versions of your work. This is especially critical when working in a team as you'll often need to unwind conflicting changes or figure out precisely when/what changes were made. Version control is an absolutely essential part of any software project and a little bit of time spent learning the basics will pay off many times over.


Installing Git

From the command line, just type:
$ sudo apt-get install git


Setting up a repository

For this class, we'll be using Bitbucket for handling remote storage of repositories (another popular service is GitHub). Although you don't need to have a remote repository (Git will work just fine locally), having a remote repository both gives you a remote backup (nice if something happens to your entire machine) and makes sharing easy if you ever need it (as you will for all assignments for this class).

Getting started is easy. First, create a new repository on Bitbucket.

Then, from the command line type:

$ mkdir /path/to/your/new/project
$ cd /path/to/your/new/project
$ git init
$ git remote add origin https://YOUR_USERNAME@bitbucket.org/YOUR_USERNAME/REPOSITORY_NAME.git

In the above examples replace YOUR_USERNAME with your Bitbucket username and REPOSITORY_NAME with the name of the repository you just created on Bitbucket (Bitbucket will also prompt you with these commands after creating a new repo).


Checking in a new file

After creating a repository and linking it to Bitbucket, you'll want to get to work. Let's say you've started a new file called "test.py" and, after getting a few lines in, want to save your work. From the command line in your project directory, just type:
$ git add test.py
$ git commit -m "adding test.py (or whatever message describes your work)"
$ git push origin master

Let's look at each of those commands.

git add test.py
tells git that this is a file you want under version control (often there will be plenty of files, for example temp files or compiled files that you wouldn't want in version control).

git commit -m "some message"
is what actually commits (stores) your changes locally. Every commit is required to have a commit message. The -m flag allows you to directly enter that message on the command line (in quotes). Although it may seem like a waste, descriptive commit messages can be extremely useful when trying to remember what you did (and even more so when working in a team and trying to figure out what someone else did!).

git push origin master
is what actually pushes the changes up to your remote repository. It's very easy to forget this final step (especially if you're used to some other version control systems), so make sure to get in the habit of always pushing your changes after a commit.


Updating your code

If you are working in a team or just working on multiple computers and want to pull down the latest version of your work, there are two commands you need to know.

If you are pulling down an existing repository for the first time, use the clone command. For example:
$ git clone https://YOUR_USER@bitbucket.org/YOUR_USER/REPO_NAME.git

If you already have a copy of the repository and just want to pull down the latest changes, just use the pull command. For example (from your project directory):
$ git pull

During a pull, if there are parts of the code that need to be merged (for example, if two people have modified the same file), git will prompt you to resolve any conflicts. This is far superior to just overwriting each other's work!