{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 1b: Version control with Git\n",
"\n",
"Keeping track of all of the changes in your project over time is good practice. How many times have you edited something in something you were writing and then wanted to go back and see what you had in the first place? Wouldn't it be great to know what changes you made and when you made them?\n",
"\n",
"A **version control system** facilitates this process of keeping track of changes over time. Beyond that, it allows multiple people to collaborate and work on parts of the same project simultaneously. We will use version control extensively in class, both for collaboration among teammates, and for homework submission and grading.\n",
"\n",
"There are many version control systems. The four most prominent, in order of age, oldest to youngest, are [CVS](https://en.wikipedia.org/wiki/Concurrent_Versions_System), [Subversion](https://subversion.apache.org), [Git](https://git-scm.com), and [Mercurial](https://www.mercurial-scm.org) (the first version of Git was released about two weeks before Mercurial, so they are really the same age). Today, Git dominates.\n",
"\n",
"Git was developed by [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds), the person who developed the Linux operating system. He named Linux after himself, and he decided to also name Git after himself (\"git\" is British slang for a stupid person). Try typing\n",
"\n",
"```bash\n",
"man git\n",
"```\n",
" \n",
"on the command line and read what the `NAME` of the software is.\n",
"\n",
"Using Git as a version control system allows communication with remote repositories (\"repos\" for short) hosted by services such as [GitHub](http://github.com/), [GitLab](http://gitlab.com/) or [Bitbucket](http://bitbucket.org). These services provide university-affiliated people with a `.edu` email address with perks that include free private repositories. We will use GitHub for our bootcamp, and you should already have set up an account.\n",
"\n",
"Remote repositories are not only a great way for keeping your data safe and in the cloud. They are also an excellent tool for collaboration since Git allows multiple users to edit the shared files simultaneously and has a method to merge changes afterwards. Public repositories can also serve as a vehicle to distribute code (or other files).\n",
"\n",
"You can find more information about Git [here](https://git-scm.com/documentation). It is well documented. Here is an [excellent one-page (front-and-back) cheatsheet](https://education.github.com/git-cheat-sheet-education.pdf).\n",
"\n",
"Let's get started. You all should have a version of Git installed on your computers. Open the terminal and navigate into your `~/git` directory."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configuring Git\n",
"\n",
"While you already used Git in the set-up lessons, you still have some configuring to do. For this, and everything else in this notebook, we'll use the command line. We will do the configuration with `--global` flags, which means these specifications work for all of your repositories. First, we'll specify the name and email address of the person working with Git on your machine (that's you!).\n",
"\n",
"```bash\n",
"git config --global user.name \"YOUR NAME\"\n",
"git config --global user.email \"YOUR EMAIL ADDRESS\"\n",
"```\n",
"\n",
"\n",
"\n",
" \n",
"Git is very well documented and help is easily available. If you need to know more about `config`, for example, just enter:\n",
"\n",
"```bash\n",
"git help config\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cloning Repositories\n",
"\n",
"Your team's repository is already set up and hosted on GitHub. You will need to clone it from its URL; you will have access to that once the teams are finalized. To practice cloning, we will clone one of the zillions of public repositories that are hosted on GitHub. We will clone a simple package, called `insulter` that will hurl Shakespearean insults at you. Enter the following on the command line.\n",
"\n",
"```bash\n",
"git clone https://gist.github.com/3165396.git insulter\n",
"```\n",
"\n",
"Note that the insulter package is now on your local machine. You have a copy of it on your own hard drive. You do not need to be connected to the internet to use it.\n",
"\n",
"Now, `cd` to `insulter` and you can start using it, thou wayward tickle-brained flap-dragon!\n",
"\n",
"```bash\n",
"python insulter.py\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pulling in changes\n",
"\n",
"Actively developed repositories are constantly being updated. After you clone the repository, its authors (e.g., your teammates) may add or edit things in the repository. For you to get those changes, you need to **fetch** them and then **merge** them into what you have locally.\n",
"\n",
"To fetch the updated repository, you guessed it, you do:\n",
"\n",
"```bash\n",
"git fetch\n",
"```\n",
"\n",
"The result is stored in a hidden directory, `.git/FETCH_HEAD`. (Directories that begin with a `.` are hidden; you don't see them when you type `ls` without any flags.)\n",
"\n",
"Now that there are changes, you would like to update your local repository. Provided you do not have any local edits, this is seamless. You just do\n",
"\n",
"```bash\n",
"git merge FETCH_HEAD\n",
"```\n",
" \n",
"Now your repository will be up to date.\n",
"\n",
"A shortcut for the commands\n",
"\n",
"```bash\n",
"git fetch\n",
"git merge FETCH_HEAD\n",
"```\n",
" \n",
"run in succession is\n",
"\n",
"```bash\n",
"git pull\n",
"```\n",
" \n",
"In practice, you will use this a lot, but sometimes fetching and merging can be useful. Note that `git pull` is actually shorthand for \n",
"\n",
"```bash\n",
"git pull origin master\n",
"```\n",
" \n",
"which is the more verbose way of saying that you want to pull the master **branch** from the remote repository named `origin`. We will not discuss branching in this course, but it is an important concept to learn about.\n",
"\n",
"Generally it is good practice to pull before you start working each day to make sure you pull in any updates your collaborators may have made."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Relationships among your local machine and your repository\n",
"\n",
"Let us think about how we have things set up, referring to the excellent image below from [Jessica Lord](http://jlord.us/). For now, focus only on the blue portions on the right (ignore forking and the upstream repository). **Remotely**, in our case on GitHub, lives the repository that I created for your team.\n",
"\n",
"\n",
"
\n",
"\n",
"