Basic Git

This tutorial demonstrates how to setup and use git in a solo environment. Unlike other tutorials, we ignore the ability of git to collaborate with others and instead use it to backup (with history) and maintain syncornized directories between multiple computers. Hence, one can be working on research equally well between each of their different machines.

In this tutorial we will assume the user has two Linux machines: laptop and caampc. Both have git already installed (this is not the case on all CAAM machines; ask Eric Aune for help). The computer caampc is always online and can be reached through SSH. We will place the central repository on caampc and then access this repository from laptop.

Git Setup

To keep track of who is making commits and on what machine, we tell git to store information about us applicable to all repositories on the local machine. Git wants to know a name and email address, but rather than a true email address, some prefer to use the "user@domain" convention to specify which machine we are on. So, for example, on caampc,

caampc # git config --global "Jeffrey Hokanson" caampc # git config --global <jeffreyh@caampc>

and on laptop
laptop # git config --global "Jeffrey Hokanson" laptop # git config --global <jeffreyh@laptop>
These changes are global and stored in the file ~/.gitconfig.

Setup the Repositories

Starting on caampc, we setup an empty git repository that will serve as the HEAD repository. This should be placed in the "scratch" area available on the local disk (rather than on the network mounted home directory). In our example, we will call the new repository "eigen." First we create the directory for the repository

caampc # mkdir -p /scratch/git/eigen
In this example, we leave the folder "git" available for storing other git repositories. Now we initialize the directory,
caampc # cd /scratch/git/eigen caampc # git init

Now we are ready to add our first file. While still in the directory eigen,

caampc # touch readme
This creates the blank file readme. Now we add this to the repository,
caampc # git add readme
Now we are ready to commit. Unlike SVN, committing in git tells the local copy of the repository to keep the current status. This is done through the command
caampc # git commit -a -m "First commit"
The -a tells git to record all modified files. If you only want to commit a few files, include their names after git commit. The -m records the commit log entry. Rather than doing this via the command line, git can automatically open up your favorite editor to make the commit message. Do this by setting the EDITOR environment variable. For example, since I use vi, I have the following line in my .bashrc
export EDITOR=vi

Now we have our first repository with our first commit. Git is perfectly happy to keep a version controlled directory all on its own, but our objective is to have a repository accessible from multiple machines. In this configuration, our current directory (/scratch/git/eigen) will serve as the "HEAD" to which all commits are made. Consequently, we should no longer edit files in this directory. We now tell git that this directory is going to be a "bare" repository

caampc # git config --bool core.bare true
Now can clone the repository, into our home directory where we will make edits from now on. I prefer to keep all repositories stashed in a directory labeled "GIT"
caampc # mkdir -p ~/GIT caampc # git clone /scratch/git/eigen ~/GIT/eigen
This copy of eigen will have the file readme, the only thing in the repository currently. Doing the same on the laptop is similarly easy:
laptop # mkdir -p ~/GIT laptop # git clone ssh:// ~/GIT/eigen
Here, be sure to replace caampc, with your CAAM machine name, e.g., caampc31. This will require you to enter your password every time you want to push or pull, so it is recommended to setup an SSH key (see, e.g., this).

Using the repository

The git life cycle is different from SVN's in that commits are local edits to the repository. To commit these the "HEAD" repository, we use the push command and to obtain the most recent version, we use pull. For example, let's edit the read me file:

caampc # echo "My first git repository" > readme
Now we commit this change,
caampc # git commit -a -m "my second edit"
To synchronize this version with the HEAD located at /scratch/git/eigen, we issue the command,
caampc # git push
On laptop we can retrieve this change using the command,
laptop # git pull

Further information

For futher information about git, I would recommend

Duncan Hall 2108

curriculum vitae

PDF as of November 2011




TeX Install

related projects

CAAM335 Lab

Beaded string data

External Links

Taraborelli's beautiful LaTeX

Music for Programming