View on GitHub

NEH Institute materials

July 2017

Home | Admin | Week 1 | Week 2 | Week 3 | Misc

Git tutorial

Git is a content management system, but not in the usual way you might expect from well-known Content Management Systems (CMS) like Wordpress or Drupal, or well-known file-sharing systems like Dropbox or Box. For this reason, Git is called a content tracker.

Git is one of the most used version control systems in the coding community. It is useful when working on coding projects, alone or in a group: it keeps track of each stage of the process, and allows you for instance to go back and restore to an earlier version.

Git is a distributed version management control system. That means that every project member has a copy of the complete history of all the files in the project on their machine. Contrast this with centralized version control systems such as Subversion (SVN) were users only have the last version of a file on their system. In this workshop, we are going to use remote repositories, which are located om a server on Github, as well as a local repository that is located on your own machine.

In this tutorial, we will work both in the browser and in a terminal window. We use the browser to administer remote Git repositories; we work in the terminal for everything regarding the local Git repository.

Git is a command line tool with which one can administer local and remote Git repositories, allowing you to version your files and share them easily with others within a project.

Github is an online service that hosts Git repositories (public as well as private) and provides social network functionality to users. Users can add issues, construct a wiki, create tasks and create pull requests on the web-site.

What you need to get started with Git:

If you don’t have a Github account, create one here: https://github.com

Preparation

General workflow

Create a new Github repository

Your repository will be located at https://github.com/username/repositoryname.

Set your identity on your local machine

When you start working with Git you need to set your identity. Git tracks who changes what in each file. Therefore you need to identify yourself before you are able to make changes and commit them. This identity will be used to track all the changes that you make to content in a repository. You only have to do this once on each machine that you use.

Open a terminal window and type the following commands, replacing John Doe with your name and the email-address with your own email address:

Cloning remote repositories

Git clone

To work with the repository we just created we need to transfer the data from the remote repository to the local machine. Copying a remote repository to the local machine is called cloning in Git. For completeness sake I also included in the table below the git command to create a new repository on the local machine.

Working with Git repositories is completely command line and file based, so the knowledge that you acquired about the command line and the file system over the past couple of days will come in very handy here.

Command Description
git clone Copy an existing repository from a remote location (for example GitHub)
git remote View and manage remote repositories
git init Create a new repository locally
$ cd ~
$ mkdir Workspace
$ cd Workspace

Now we clone the repository that we just made on GitHub to the local machine.

$ git clone https://github.com/username/repositoryname
$ cd repositoryname
$ ls -lisa

There should be your files.

Now we run the git remote command to see whether the remote repository is correctly linked with the local repository.

$ git remote -v

Sample outcome:

origin	https://github.com/username/repositoryname (fetch)
origin	https://github.com/username/repositoryname (push)

What fetch and push stand for we will get into later (under section syncing repositories)

Working directory

When you have a terminal window open, you are in what is called the working directory. The complete repository, with all the changes that people have ever made, is present, but in the background. Information other than current versions of your files is outside your working directory, but you can reach it when you need it.

Making some changes

Working with changes locally and tracking them

Git commit

Command Description
git status Show which files are modified locally or new
git diff Show changes
git add Add a file to change tracking and stage
git reset HEAD Untrack a file or unstage
git checkout Undo changes to a file (before commit)

Committing changes

Command Description
git commit Make changes permanent
git log Show history of commits
git reset HEAD~ Undo the last commit, retain changes in the working directory
git reset HEAD~ --hard Undo the last commit, remove changes from the working directory

git commit -a adds all changed files and commits the changes, that is, it combines git add with git commit. But it only adds files that have changed, and not files that are completely new. The only way to add a new file is with git add.

For example:

$ git add .
$ git commit -m "added a new feature some files changed"

If you make a mistake with a commit (forgot to add new files, or messed up your commit message)

$ git reset HEAD~

Note that there is a difference between files and commits. A commit can consist (and usually does) of multiple files. Git tracks commits and content, not single files.

vim is the default editor in Git (on all operating systems). When you type git commit, you are taken into vim to enter a commit message, where you record information about the commit. The most important vim commands are:

You type What happens
Esc enter command mode
i enter insert mode
:wq write your changes and quit
:q! cancel (quit without writing changes)

The use of the escape key in Vim, the i for insert mode, :wq and :q! to cancel

Syncing repositories

Git syncing repositories

Command Description
git pull Fetch the commits from a remote repository and merge them with the current working directory (i.e. does a fetch and a merge in one)
git push Push the commits from the local repository to a remote repository
git fetch Fetch the commits from a remote repository into the local repository
git merge Merge the commits from the local repository with commits fetched from a remote repository (actually this works on branches; this will be explained in the git tutorial part 2)

Terms learned