Introduction

Please open notebook rsepython-s1r1.ipynb

Some of the outputs shown in this reading will differ to those generated when you run the notebook.

You will also need to customise some of the commands to match your own details.

What’s version control?

Version control is a tool for managing changes to a set of files.

There are many different version control systems:

Why use version control?

Git is also a collaborative tool:

Git != GitHub

How do we use version control?

Do some programming, then commit our work:

my_vcs commit

Program some more.

Spot a mistake:

my_vcs rollback

Mistake is undone.

What is version control? (Team version)

When working with others, the above example may take the form:

Suleman Jane
`my_vcs commit` ...
... Join the team
... `my_vcs checkout`
... Do some programming
... `my_vcs commit`
`my_vcs update` ...
Do some programmingDo some programming
`my_vcs commit` ...
`my_vcs update` ...
`my_vcs merge` ...
`my_vcs commit` ...

Scope

This course will use the git version control system, but much of what you learn will be valid with other version control tools you may encounter, including subversion (svn) and mercurial (hg).

Practising with Git

Example Exercise

In this course, we will use, as an example, the development of a few text files containing a description of a topic of your choice.

This could be your research, a hobby, or something else. In the end, we will show you how to display the content of these files as a very simple website.

Programming and documents

The purpose of this exercise is to learn how to use Git to manage program code you write, not simple text website content, but we’ll just use these text files instead of code for now, so as not to confuse matters with trying to learn version control while thinking about programming too.

In later parts of the course, you will use the version control tools you learn in this section with actual Python code.

Markdown

The text files we create will use a simple “wiki” markup style called markdown, http://daringfireball.net/projects/markdown/basics, to show formatting. This is the convention used in this file, too.

You can view the content of this file in the way Markdown renders it by looking on the web: https://github.com/UCL/ucl_software_carpentry/blob/master/git/git_instructions.md, and compare the raw text: https://raw.github.com/UCL/ucl_software_carpentry/master/git/git_instructions.md.

Displaying Text in this Tutorial

This tutorial is based on use of the Git command line. So you’ll be typing commands in the shell.

To make it easy for me to edit, I’ve built it using an IPython notebook.

Commands you can type will look like this, using the %%bash “magic” for the notebook (enables the execution of bash commands).

%%bash
echo some output

some output

In this document, we will show the new content of an edited document like this:

%%writefile somefile.md
Some content here

Overwriting somefile.md

But if you are following along, you should edit the file using a text editor.

On Windows, we recommend ‘Notepad++’: https://notepad-plus-plus.org.

On Mac, we recommend ‘Atom’: https://atom.io

Setting up somewhere to work

Create directories for the example:

%%bash
mkdir -p learning_git/git_example
cd learning_git/git_example

We are going to add the directories we have just created, to the same directory as the IPython notebook we are currently using.

The file paths returned by these commands will vary for your system.

import os
top_dir = os.getcwd()
top_dir

‘/Users/uclrits/rsd-python/sec1git’

git_dir = os.path.join(top_dir, 'learning_git')
git_dir

‘/Users/uclrits/rsd-python/sec1git/learning_git’

working_dir=os.path.join(git_dir, 'git_example')
os.chdir(working_dir)

Solo work

Configuring Git with your name and email

First, we should configure Git to know our name and email address. Please use your own details.

%%bash
git config --global user.name "UCL RITS Example"
git config --global user.email "uclrits@gmail.com"

Initialising the repository

Now, we will tell Git to track the content of this folder, git_example, as a git “repository”.

%%bash
pwd # Note where we are standing-- MAKE SURE YOU INITIALISE THE RIGHT FOLDER
git init

/Users/uclrits/rsd-python/sec1git/learning_git/git_example Initialized empty Git repository in /Users/uclrits/rsd-python/sec1git/learning_git/git_example/.git/

As yet, this repository contains no files:

%%bash
ls

We can check the status of Git with the following:

%%bash
git status

On branch master

Initial commit

nothing to commit

It is possible to initiate repositories in sub-directories:

%%bash
mkdir git-sub
git init

/Users/uclrits/rsd-python/sec1git/learning_git/git_example/git-sub Initialized empty Git repository in /Users/uclrits/rsd-python/sec1git/learning_git/git_example/git-sub/.git/

but any such repositories would be redundant.

If you accidentally do this, it is possible to undo the mistake by removing the .git folder from the sub-directory, but this should be done with great care.

%%bash
cd ../ #this will return us to the git_example directory
rm -rf git-sub/.git

Running this command in the wrong directory, will remove the entire Git history of a project you might want to keep.

Therefore, always check your current directory using the command ‘pwd’.

Next: Reading - Solo