This tutorial is an showcasing the material collected in the Pro Git book which is avilable for free online. I also draw on a set of excellent Scientific Python Lecture Notes maintained by Robert Johansson.
Roadmap
Definition: Version Control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
Implementations
Local Version Control System
Centralized Version Control System
Distributed Version Control Systems
Tools
In [ ]:
import os
try:
os.mkdir('me')
except OSError:
pass
os.chdir('me')
In [ ]:
%%bash
echo 'How does our directory look like?'
ls -al
git init
echo 'How does our directory look like now?'
ls -al
git status
# Let us create files for tracking.
echo 'My Project' > README
echo 'peisenha' > CONTRIBUTING.md
# Set up our identity
git config --global user.name "Philipp Eisenhauer"
git config --global user.email "eisenhauer@policy-lab.org"
In [ ]:
%%bash
git add README
git status
In [ ]:
%%bash
git add CONTRIBUTING.md
git commit -a -m'initial'
In [ ]:
%%bash
echo 'Philipp Eisenhauer' > CONTRIBUTING.md
In [ ]:
%%bash
git status
In [ ]:
%%bash
git commit -a -m'changes to contributers'
In [ ]:
%%bash
# Remove files
git rm README
git status
In [ ]:
%%bash
# Move files
git mv CONTRIBUTING.md CONTRIBUTORS.md
git status
In [ ]:
%%bash
git commit -m'reorganizing files'
In [ ]:
%%bash
git log
Let us create a repository called try_git in our GitHub accounts
In [ ]:
%%bash
git remote add origin git@github.com:peisenha/try_git.git
git push -u origin master
In [ ]:
%%bash
git clone git@github.com:peisenha/try_git.git ../other
While we are at it, let us check out the class repository.
In [ ]:
%%bash
git clone git@github.com:softEcon/course.git /home/vagrant/course_material
From now on, you will find all class material in this repository.
Types:
I use annotated tags to release different versions of my research software. You can check it out an example here.
In [ ]:
%%bash
# Create tag
git tag -a v1.0 -m'ouf first tag annotated tag'
In [ ]:
%%bash
# List tag
git tag -l
In [ ]:
%%bash
# Push to remote repository
git commit -a m'added first tag'
git push origin --tags
Let us take a look at our repository in a repository viewer. We choose gitg and can install it using the Ubuntu Software Center.
Objects
A branch is a lightweight movable pointer to one of the commits. The HEAD is an additional pointer to the local branch you are currently working on.
I ususally create a branch for each feature I would like to add to my research software. Once I satisfied with my implementation I merge the development branch back into master.
In [ ]:
%%bash
git branch testing
In [ ]:
%%bash
git branch -a
In [ ]:
%%bash
git log --oneline --decorate
In [ ]:
%%bash
git checkout testing
echo 'Some information on the project.' > README
git add README
git commit -a -m'adding project information'
Let us check how the content of the directory change as we move between the different branches.
In [ ]:
%%bash
git checkout master
In [ ]:
%%bash
git checkout testing
In [ ]:
%%bash
git checkout master
echo 'Some other information on the project.' > README
git add README
git commit -a -m'adding other project information'
In [ ]:
%%bash
git merge master testing
In [ ]:
%%bash
git add README
git commit -m'fixed merge conflict'
Let us clean up our branches by deleting testing and keep our repository in good standing.
In [ ]:
%%bash
git branch -d testing
git branch -a
The use of a version control system allows us to improve our economic research in a variety of ways. In my case, I use Git for all my software projects. The debugging process is speed up considerably as I can track back in time and pin down when I introduced a new error. GitHub allows me to easily collaborate with other researchers (or research assistants) and promote my work to a broader audience.
In [ ]:
import shutil
os.chdir('../')
for dir_ in ['me', '.ipynb_checkpoints', 'other']:
try:
shutil.rmtree(dir_)
except OSError:
pass
You find some additional resources on version control here.
Formatting
In [2]:
import urllib; from IPython.core.display import HTML
HTML(urllib.urlopen('http://bit.ly/1K5apRH').read())
Out[2]: