{
"metadata": {
"name": "",
"signature": "sha256:8d775a81f9310587be7426a6c0adb8064c0177ce50b189be2cc683f7680f2dd9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Version control for fun and profit: the tool you didn't know you needed. From personal workflows to open collaboration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note:** this tutorial is based (sometimes blantently copied), and therefore owes a lot, to the excellent materials offered in:\n",
"\n",
"- [Fernando Perez's original notebook](https://github.com/profjsb/python-bootcamp/blob/master/Lectures/10_GitDevelopment/VersionControl.ipynb)\n",
"\n",
"That notbook owed a lot to the following (including these notes for completeness):\n",
"\n",
"***\n",
"- [\"Git for Scientists: A Tutorial\"](http://nyuccl.org/pages/GitTutorial) by John McDonnell \n",
"- Emanuele Olivetti's lecture notes and exercises from the G-Node summer school on [Advanced Scientific Programming in Python](https://python.g-node.org/wiki/schedule).\n",
"\n",
"In particular I've reused the excellent images from the [Pro Git book](http://git-scm.com/book) that John had already selected and downloaded, as well as some of his outline. But this version of the tutorial aims to be 100% reproducible by being executed directly as an IPython notebook and is hosted itself on github so that others can more easily make improvements to it by collaborating on Github. Many thanks to John and Emanuele for making their materials available online.\n",
"\n",
"After writing this document, I discovered [J.R. Johansson](https://github.com/jrjohansson)'s [tutorial on version control](http://nbviewer.ipython.org/urls/raw.github.com/jrjohansson/scientific-python-lectures/master/Lecture-7-Revision-Control-Software.ipynb) that is also written as a fully reproducible notebook and is also aimed at a scientific audience. It has a similar spirit to this one, and is part of his excellent series [Lectures on Scientific Computing with Python](https://github.com/jrjohansson/scientific-python-lectures) that is entirely available as IPython Notebooks.\n",
"***"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Why should I care about Version Control?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From [What Is Version Control? Why Is It Important For Due Diligence?](http://oss-watch.ac.uk/resources/versioncontrol)\n",
"\n",
">Version control systems are essential for any form of distributed, collaborative development. Whether it is the history of a wiki page or large software development project, the ability to track each change as it was made, and to reverse changes when necessary can make all the difference between a well managed and controlled process and an uncontrolled \u2018first come, first served\u2019 system. It can also serve as a mechanism for due diligence for software projects."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From Wikipedia\n",
"\n",
">\u201cRevision control, also known as version control, source control\n",
"or software configuration management (SCM), is the\n",
"**management of changes to documents, programs, and other\n",
"information stored as computer files.**\u201d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Reproducibility?**\n",
"\n",
"* Tracking and recreating every step of your work\n",
"* In the software world: it's called *Version Control*!\n",
"\n",
"What do (good) version control tools give you?\n",
"\n",
"* Peace of mind (backups)\n",
"* Freedom (exploratory branching)\n",
"* Collaboration (synchronization)\n"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Git is an enabling technology: Use version control for everything"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Paper writing (never get `paper_v5_john_jane_final_oct22_really_final.tex` by email again!)\n",
"* Grant writing\n",
"* Everyday research\n",
"* Teaching (never accept an emailed homework assignment again!)"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Paper Writing With Git"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
""
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Annotated history of each authors's worfklow (and backup!)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
""
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Why are we using git?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I don't know, really. Here are some reasons though:\n",
"\n",
"- It's decentralized (ie. everyone has a copy of the repository).\n",
"- It has multiple, secure connection methods.\n",
"- Any more?\n",
"\n",
"I am not going to be upset if you use something else, but using a system that is decentralized is important. If you need to pick, check out [wikipedia](http://en.wikipedia.org/wiki/Comparison_of_revision_control_software).\n"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"The plan for this tutorial"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This tutorial is structured in the following way: we will begin with a brief overview of key concepts you need to understand in order for git to really make sense. We will then dive into hands-on work: after a brief interlude into necessary configuration we will discuss 5 \"stages of git\" with scenarios of increasing sophistication and complexity, introducing the necessary commands for each stage:\n",
" \n",
"1. Local, single-user, linear workflow\n",
"2. Single local user, branching\n",
"3. Using remotes as a single user\n",
"4. Remotes for collaborating in a small team\n",
"5. Full-contact github: distributed collaboration with large teams\n",
" \n",
"In reality, this tutorial only covers stages 1-4, since for #5 there are many software develoment-oriented tutorials and documents of very high quality online. But most scientists start working alone with a few files or with a small team, so I feel it's important to build first the key concepts and practices based on problems scientists encounter in their everyday life and without the jargon of the software world. Once you've become familiar with 1-4, the excellent tutorials that exist about collaborating on github on open-source projects should make sense."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Very high level picture: an overview of key concepts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **commit**: *a snapshot of work at a point in time*\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"Credit: ProGit book, by Scott Chacon, CC License."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Development with Git.pdf DevelopmentWithGit.pdf \u001b[34mfiles\u001b[m\u001b[m/\r\n",
"DevelopmentWithGit.key VersionControl.ipynb\r\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"A **repository**: a group of *linked* commits\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"Note: these form a Directed Acyclic Graph (DAG), with nodes identified by their *hash*."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"A **hash**: a fingerprint of the content of each commit *and its parent*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sha\n",
"\n",
"# Our first commit\n",
"data1 = 'This is the start of my paper2.'\n",
"meta1 = 'date: 1/1/12'\n",
"hash1 = sha.sha(data1 + meta1).hexdigest()\n",
"print 'Hash:', hash1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hash: 7bb695b77966e27cfaebfa59e27a0b91f1d33813\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Our second commit, linked to the first\n",
"data2 = 'Some more text in my paper...'\n",
"meta2 = 'date: 1/2/12'\n",
"# Note we add the parent hash here!\n",
"hash2 = sha.sha(data2 + meta2 + hash1).hexdigest()\n",
"print 'Hash:', hash2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hash: 543da8bac9f643ba5611897b192a16dea42d2ab7\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And this is pretty much the essence of Git!"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"First things first: git must be configured before first use"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The minimal amount of configuration for git to work without pestering you is to tell it who you are:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git config --global user.name \"Jeremy S. Perkins\"\n",
"git config --global user.email \"jeremyshane@gmail.com\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" And how you will edit text files (it will often ask you to edit messages and other information, and thus wants to know how you like to edit your files):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"# Put here your preferred editor. If this is not set, git will honor\n",
"# the $EDITOR environment variable\n",
"git config --global core.editor /usr/bin/emacs # Yes, I still use emacs\n",
"\n",
"# On Windows Notepad will do in a pinch, I recommend Notepad++ as a free alternative\n",
"# On the mac, you can set nano or emacs as a basic option\n",
"\n",
"# And while we're at it, we also turn on the use of color, which is very useful\n",
"git config --global color.ui \"auto\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set git to use the credential memory cache so we don't have to retype passwords too frequently. On OSX, you should run the following (note that this requires git version 1.7.10 or newer):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash \n",
"git config --global credential.helper osxkeychain\n",
"# Set the cache to timeout after 2 hours (setting is in seconds)\n",
"git config --global credential.helper 'cache --timeout=7200'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Github offers in its help pages instructions on how to configure the credentials helper for [Linux](https://help.github.com/articles/set-up-git#platform-linux) and [Windows](https://help.github.com/articles/set-up-git#platform-windows)."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Stage 1: Local, single-user, linear workflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simply type `git` to see a full list of all the 'core' commands. We'll now go through most of these via small practical exercises:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!git"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"usage: git [--version] [--help] [-C ] [-c name=value]\r\n",
" [--exec-path[=]] [--html-path] [--man-path] [--info-path]\r\n",
" [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\r\n",
" [--git-dir=] [--work-tree=] [--namespace=]\r\n",
" []\r\n",
"\r\n",
"The most commonly used git commands are:\r\n",
" add Add file contents to the index\r\n",
" bisect Find by binary search the change that introduced a bug\r\n",
" branch List, create, or delete branches\r\n",
" checkout Checkout a branch or paths to the working tree\r\n",
" clone Clone a repository into a new directory\r\n",
" commit Record changes to the repository\r\n",
" diff Show changes between commits, commit and working tree, etc\r\n",
" fetch Download objects and refs from another repository\r\n",
" grep Print lines matching a pattern\r\n",
" init Create an empty Git repository or reinitialize an existing one\r\n",
" log Show commit logs\r\n",
" merge Join two or more development histories together\r\n",
" mv Move or rename a file, a directory, or a symlink\r\n",
" pull Fetch from and integrate with another repository or a local branch\r\n",
" push Update remote refs along with associated objects\r\n",
" rebase Forward-port local commits to the updated upstream head\r\n",
" reset Reset current HEAD to the specified state\r\n",
" rm Remove files from the working tree and from the index\r\n",
" show Show various types of objects\r\n",
" status Show the working tree status\r\n",
" tag Create, list, delete or verify a tag object signed with GPG\r\n",
"\r\n",
"'git help -a' and 'git help -g' lists available subcommands and some\r\n",
"concept guides. See 'git help ' or 'git help '\r\n",
"to read about a specific subcommand or concept.\r\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"`git init`: create an empty repository"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"rm -rf test\n",
"git init test"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Initialized empty Git repository in /Users/jsperki1/src/python-bootcamp/Lectures/10_GitDevelopment/test/.git/\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note:** all these cells below are meant to be run by you in a terminal where you change *once* to the `test` directory and continue working there.\n",
"\n",
"Since we are putting all of them here in a single notebook for the purposes of the tutorial, they will all be prepended with the first two lines:\n",
"\n",
" %%bash\n",
" cd test\n",
"\n",
"that tell IPython to do that each time. But you should ignore those two lines and type the rest of each cell yourself in your terminal."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at what git did:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"ls"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"ls -la"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"total 0\n",
"drwxr-xr-x 3 jsperki1 staff 102 Jun 6 11:53 .\n",
"drwxr-xr-x 10 jsperki1 staff 340 Jun 6 11:53 ..\n",
"drwxr-xr-x 10 jsperki1 staff 340 Jun 6 11:53 .git\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"ls -l .git"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"total 24\n",
"-rw-r--r-- 1 jsperki1 staff 23 Jun 6 11:53 HEAD\n",
"drwxr-xr-x 2 jsperki1 staff 68 Jun 6 11:53 branches\n",
"-rw-r--r-- 1 jsperki1 staff 137 Jun 6 11:53 config\n",
"-rw-r--r-- 1 jsperki1 staff 73 Jun 6 11:53 description\n",
"drwxr-xr-x 11 jsperki1 staff 374 Jun 6 11:53 hooks\n",
"drwxr-xr-x 3 jsperki1 staff 102 Jun 6 11:53 info\n",
"drwxr-xr-x 4 jsperki1 staff 136 Jun 6 11:53 objects\n",
"drwxr-xr-x 4 jsperki1 staff 136 Jun 6 11:53 refs\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's edit our first file in the test directory with a text editor... I'm doing it programatically here for automation purposes, but you'd normally be editing by hand"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"echo \"My first bit of text\" > file1.txt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"`git add`: tell git about this new file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git add file1.txt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now ask git about what happened with `status`:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"On branch master\n",
"\n",
"Initial commit\n",
"\n",
"Changes to be committed:\n",
" (use \"git rm --cached ...\" to unstage)\n",
"\n",
"\tnew file: file1.txt\n",
"\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"`git commit`: permanently record our changes in git's database"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For now, we are *always* going to call `git commit` either with the `-a` option *or* with specific filenames (`git commit file1 file2...`). This delays the discussion of an aspect of git called the *index* (often referred to also as the 'staging area') that we will cover later. Most everyday work in regular scientific practice doesn't require understanding the extra moving parts that the index involves, so on a first round we'll bypass it. Later on we will discuss how to use it to achieve more fine-grained control of what and how git records our actions."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git commit -a -m\"This is our first commit\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master (root-commit) a842c78] This is our first commit\n",
" 1 file changed, 1 insertion(+)\n",
" create mode 100644 file1.txt\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the commit above, we used the `-m` flag to specify a message at the command line. If we don't do that, git will open the editor we specified in our configuration above and require that we enter a message. By default, git refuses to record changes that don't have a message to go along with them (though you can obviously 'cheat' by using an empty or meaningless string: git only tries to facilitate best practices, it's not your nanny)."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"`git log`: what has been committed so far"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git log"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"commit a842c784262c48ef862b49c073f76c5eb75d344b\n",
"Author: Jeremy S. Perkins \n",
"Date: Fri Jun 6 11:54:10 2014 -0400\n",
"\n",
" This is our first commit\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"`git diff`: what have I changed?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's do a little bit more work... Again, in practice you'll be editing the files by hand, here we do it via shell commands for the sake of automation (and therefore the reproducibility of this tutorial!)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"echo \"And now some more text...\" >> file1.txt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now we can ask git what is different:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git diff"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"diff --git a/file1.txt b/file1.txt\n",
"index ce645c7..4baa979 100644\n",
"--- a/file1.txt\n",
"+++ b/file1.txt\n",
"@@ -1 +1,2 @@\n",
" My first bit of text\n",
"+And now some more text...\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"The cycle of git virtue: work, commit, work, commit, ..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git commit -a -m\"I have made great progress on this critical matter.\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master c120964] I have made great progress on this critical matter.\n",
" 1 file changed, 1 insertion(+)\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"`git log` revisited"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's see what the log shows us now:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git log"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"commit c1209643b4e072eb35cdac35f85f5555551cd323\n",
"Author: Jeremy S. Perkins \n",
"Date: Fri Jun 6 11:54:28 2014 -0400\n",
"\n",
" I have made great progress on this critical matter.\n",
"\n",
"commit a842c784262c48ef862b49c073f76c5eb75d344b\n",
"Author: Jeremy S. Perkins \n",
"Date: Fri Jun 6 11:54:10 2014 -0400\n",
"\n",
" This is our first commit\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes it's handy to see a very summarized version of the log:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git log --oneline --topo-order --graph"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Git supports *aliases:* new names given to command combinations. Let's make this handy shortlog an alias, so we only have to type `git slog` and see this compact log:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"# We create our alias (this saves it in git's permanent configuration file):\n",
"git config --global alias.slog \"log --oneline --topo-order --graph\"\n",
"\n",
"# And now we can use it\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 25
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"`git mv` and `rm`: moving and removing files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While `git add` is used to add fils to the list git tracks, we must also tell it if we want their names to change or for it to stop tracking them. In familiar Unix fashion, the `mv` and `rm` git commands do precisely this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git mv file1.txt file-newname.txt\n",
"git status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"On branch master\n",
"Changes to be committed:\n",
" (use \"git reset HEAD ...\" to unstage)\n",
"\n",
"\trenamed: file1.txt -> file-newname.txt\n",
"\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that these changes must be committed too, to become permanent! In git's world, until something hasn't been committed, it isn't permanently recorded anywhere."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git commit -a -m\"I like this new name better\"\n",
"echo \"Let's look at the log again:\"\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master c399e9b] I like this new name better\n",
" 1 file changed, 0 insertions(+), 0 deletions(-)\n",
" rename file1.txt => file-newname.txt (100%)\n",
"Let's look at the log again:\n",
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And `git rm` works in a similar fashion."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add a new file `file2.txt`, commit it, make some changes to it, commit them again, and then remove it (and don't forget to commit this last step!)."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Local user, branching"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is a branch? Simply a *label for the 'current' commit in a sequence of ongoing commits*:\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There can be multiple branches alive at any point in time; the working directory is the state of a special pointer called HEAD. In this example there are two branches, *master* and *testing*, and *testing* is the currently active branch since it's what HEAD points to:\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once new commits are made on a branch, HEAD and the branch label move with the new commits:\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This allows the history of both branches to diverge:\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But based on this graph structure, git can compute the necessary information to merge the divergent branches back and continue with a unified line of development:\n",
" \n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now illustrate all of this with a concrete example. Let's get our bearings first:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git status\n",
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"On branch master\n",
"nothing to commit, working directory clean\n",
"file-newname.txt\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are now going to try two different routes of development: on the `master` branch we will add one file and on the `experiment` branch, which we will create, we will add a different one. We will then merge the experimental branch into `master`."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git branch experiment\n",
"git checkout experiment"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Switched to branch 'experiment'\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"echo \"Some crazy idea\" > experiment.txt\n",
"git add experiment.txt\n",
"git commit -a -m\"Trying something new\"\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[experiment 61ecd8c] Trying something new\n",
" 1 file changed, 1 insertion(+)\n",
" create mode 100644 experiment.txt\n",
"* 61ecd8c Trying something new\n",
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git checkout master\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Switched to branch 'master'\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"echo \"All the while, more work goes on in master...\" >> file-newname.txt\n",
"git commit -a -m\"The mainline keeps moving\"\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master d89964b] The mainline keeps moving\n",
" 1 file changed, 1 insertion(+)\n",
"* d89964b The mainline keeps moving\n",
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"file-newname.txt\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git merge experiment\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Merge made by the 'recursive' strategy.\n",
" experiment.txt | 1 +\n",
" 1 file changed, 1 insertion(+)\n",
" create mode 100644 experiment.txt\n",
"* 5db83a8 Merge branch 'experiment'\n",
"|\\ \n",
"| * 61ecd8c Trying something new\n",
"* | d89964b The mainline keeps moving\n",
"|/ \n",
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Using remotes as a single user"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are now going to introduce the concept of a *remote repository*: a pointer to another copy of the repository that lives on a different location. This can be simply a different path on the filesystem or a server on the internet.\n",
"\n",
"For this discussion, we'll be using remotes hosted on the [GitHub.com](http://github.com) service, but you can equally use other services like [BitBucket](http://bitbucket.org) or [Gitorious](http://gitorious.org) as well as host your own."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"ls\n",
"echo \"Let's see if we have any remote repositories here:\"\n",
"git remote -v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"experiment.txt\n",
"file-newname.txt\n",
"Let's see if we have any remote repositories here:\n"
]
}
],
"prompt_number": 35
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the above cell didn't produce any output after the `git remote -v` call, it means we have no remote repositories configured. We will now proceed to do so. Once logged into GitHub, go to the [new repository page](https://github.com/new) and make a repository called `test`. Do **not** check the box that says `Initialize this repository with a README`, since we already have an existing repository here. That option is useful when you're starting first at Github and don't have a repo made already on a local computer.\n",
"\n",
"We can now follow the instructions from the next page:"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git remote add origin https://github.com/kialio/test.git\n",
"git push -u origin master"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see the remote situation again:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git remote -v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"origin\thttps://github.com/kialio/test.git (fetch)\n",
"origin\thttps://github.com/kialio/test.git (push)\n"
]
}
],
"prompt_number": 48
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now [see this repository publicly on github](https://github.com/kialio/test).\n",
"\n",
"Let's see how this can be useful for backup and syncing work between two different computers. I'll simulate a 2nd computer by working in a different directory..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"\n",
"# Here I clone my 'test' repo but with a different name, test2, to simulate a 2nd computer\n",
"git clone https://github.com/kialio/test.git test2\n",
"cd test2\n",
"pwd\n",
"git remote -v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"/Users/jsperki1/src/python-bootcamp/Lectures/10_GitDevelopment/test2\n",
"origin\thttps://github.com/kialio/test.git (fetch)\n",
"origin\thttps://github.com/kialio/test.git (push)\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Cloning into 'test2'...\n"
]
}
],
"prompt_number": 49
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now make some changes in one 'computer' and synchronize them on the second."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test2 # working on computer #2\n",
"\n",
"echo \"More new content on my experiment\" >> experiment.txt\n",
"git commit -a -m\"More work, on machine #2\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master 3b62643] More work, on machine #2\n",
" 1 file changed, 1 insertion(+)\n"
]
}
],
"prompt_number": 50
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we put this new work up on the github server so it's available from the internet"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test2\n",
"\n",
"git push"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"To https://github.com/kialio/test.git\n",
" 5db83a8..3b62643 master -> master\n"
]
}
],
"prompt_number": 51
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's fetch that work from machine #1:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git pull"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Updating 5db83a8..3b62643\n",
"Fast-forward\n",
" experiment.txt | 1 +\n",
" 1 file changed, 1 insertion(+)\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"From https://github.com/kialio/test\n",
" 5db83a8..3b62643 master -> origin/master\n"
]
}
],
"prompt_number": 52
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"An important aside: conflict management"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While git is very good at merging, if two different branches modify the same file in the same location, it simply can't decide which change should prevail. At that point, human intervention is necessary to make the decision. Git will help you by marking the location in the file that has a problem, but it's up to you to resolve the conflict. Let's see how that works by intentionally creating a conflict.\n",
"\n",
"We start by creating a branch and making a change to our experiment file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git branch trouble\n",
"git checkout trouble\n",
"echo \"This is going to be a problem...\" >> experiment.txt\n",
"git commit -a -m\"Changes in the trouble branch\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[trouble 8e7439a] Changes in the trouble branch\n",
" 1 file changed, 1 insertion(+)\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Switched to branch 'trouble'\n"
]
}
],
"prompt_number": 53
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now we go back to the master branch, where we change the *same* file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git checkout master\n",
"echo \"More work on the master branch...\" >> experiment.txt\n",
"git commit -a -m\"Mainline work\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Your branch is up-to-date with 'origin/master'.\n",
"[master d092b1a] Mainline work\n",
" 1 file changed, 1 insertion(+)\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Switched to branch 'master'\n"
]
}
],
"prompt_number": 54
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So now let's see what happens if we try to merge the `trouble` branch into `master`:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git merge trouble"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Auto-merging experiment.txt\n",
"CONFLICT (content): Merge conflict in experiment.txt\n",
"Automatic merge failed; fix conflicts and then commit the result.\n"
]
}
],
"prompt_number": 55
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see what git has put into our file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"cat experiment.txt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Some crazy idea\n",
"More new content on my experiment\n",
"<<<<<<< HEAD\n",
"More work on the master branch...\n",
"=======\n",
"This is going to be a problem...\n",
">>>>>>> trouble\n"
]
}
],
"prompt_number": 56
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At this point, we go into the file with a text editor, decide which changes to keep, and make a new commit that records our decision. I've now made the edits, in this case I decided that both pieces of text were useful, but integrated them with some changes:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"cat experiment.txt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Some crazy idea\n",
"More new content on my experiment\n",
"More work on the master branch...\n",
"This is going to be a problem...\n"
]
}
],
"prompt_number": 57
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's then make our new commit:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd test\n",
"\n",
"git commit -a -m\"Completed merge of trouble, fixing conflicts along the way\"\n",
"git slog"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master 61a5391] Completed merge of trouble, fixing conflicts along the way\n",
"* 61a5391 Completed merge of trouble, fixing conflicts along the way\n",
"|\\ \n",
"| * 8e7439a Changes in the trouble branch\n",
"* | d092b1a Mainline work\n",
"|/ \n",
"* 3b62643 More work, on machine #2\n",
"* 5db83a8 Merge branch 'experiment'\n",
"|\\ \n",
"| * 61ecd8c Trying something new\n",
"* | d89964b The mainline keeps moving\n",
"|/ \n",
"* c399e9b I like this new name better\n",
"* c120964 I have made great progress on this critical matter.\n",
"* a842c78 This is our first commit\n"
]
}
],
"prompt_number": 58
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Note:* While it's a good idea to understand the basics of fixing merge conflicts by hand, in some cases you may find the use of an automated tool useful. Git supports multiple [merge tools](https://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html): a merge tool is a piece of software that conforms to a basic interface and knows how to merge two files into a new one. Since these are typically graphical tools, there are various to choose from for the different operating systems, and as long as they obey a basic command structure, git can work with any of them."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Collaborating on github with a small team"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Single remote with shared access: we are going to set up a shared collaboration with one partner (the person sitting next to you). This will show the basic workflow of collaborating on a project with a small team where everyone has write privileges to the same repository. \n",
"\n",
"*Note for SVN users: this is similar to the classic SVN workflow, with the distinction that commit and push are separate steps. SVN, having no local repository, commits directly to the shared central resource, so to a first approximation you can think of `svn commit` as being synonymous with `git commit; git push`.*\n",
"\n",
"We will have two people, let's call them Alice and Bob, sharing a repository. Alice will be the owner of the repo and she will give Bob write privileges. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We begin with a simple synchronization example, much like we just did above, but now between *two people* instead of one person. Otherwise it's the same:\n",
"\n",
"- Bob clones Alice's repository.\n",
"- Bob makes changes to a file and commits them locally.\n",
"- Bob pushes his changes to github.\n",
"- Alice pulls Bob's changes into her own repository."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we will have both parties make non-conflicting changes each, and commit them locally. Then both try to push their changes:\n",
"\n",
"- Alice adds a new file, `alice.txt` to the repo and commits.\n",
"- Bob adds `bob.txt` and commits.\n",
"- Alice pushes to github.\n",
"- Bob tries to push to github. What happens here?\n",
"\n",
"The problem is that Bob's changes create a commit that conflicts with Alice's, so git refuses to apply them. It forces Bob to first do the merge on his machine, so that if there is a conflict in the merge, Bob deals with the conflict manually (git could try to do the merge on the server, but in that case if there's a conflict, the server repo would be left in a conflicted state without a human to fix things up). The solution is for Bob to first pull the changes (pull in git is really fetch+merge), and then push again."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Full-contact github: distributed collaboration with large teams"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiple remotes and merging based on pull request workflow: this is beyond the scope of this brief tutorial."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Other useful commands"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [show](http://www.kernel.org/pub/software/scm/git/docs/git-show.html)\n",
"- [reflog](http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html)\n",
"- [rebase](http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html)\n",
"- [tag](http://www.kernel.org/pub/software/scm/git/docs/git-tag.html)"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Git resources"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Introductory materials"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are lots of good tutorials and introductions for Git, which you\n",
"can easily find yourself; this is just a short list of things I've found\n",
"useful. For a beginner, I would recommend the following 'core' reading list, and\n",
"below I mention a few extra resources:\n",
"\n",
"1. The smallest, and in the style of this tuorial: [git - the simple guide](http://rogerdudler.github.com/git-guide)\n",
"contains 'just the basics'. Very quick read.\n",
"\n",
"1. The concise [Git Reference](http://gitref.org): compact but with\n",
" all the key ideas. If you only read one document, make it this one.\n",
"\n",
"1. In my own experience, the most useful resource was [Understanding Git\n",
"Conceptually](http://www.sbf5.com/~cduan/technical/git).\n",
"Git has a reputation for being hard to use, but I have found that with a\n",
"clear view of what is actually a *very simple* internal design, its\n",
"behavior is remarkably consistent, simple and comprehensible.\n",
"\n",
"1. For more detail, see the start of the excellent [Pro\n",
" Git](http://progit.org/book) online book, or similarly the early\n",
" parts of the [Git community book](http://book.git-scm.com). Pro\n",
" Git's chapters are very short and well illustrated; the community\n",
" book tends to have more detail and has nice screencasts at the end\n",
" of some sections.\n",
"\n",
"If you are really impatient and just want a quick start, this [visual git tutorial](http://www.ralfebert.de/blog/tools/visual_git_tutorial_1)\n",
"may be sufficient. It is nicely illustrated with diagrams that show what happens on the filesystem.\n",
"\n",
"For windows users, [an Illustrated Guide to Git on Windows](http://nathanj.github.com/gitguide/tour.html) is useful in that\n",
"it contains also some information about handling SSH (necessary to interface with git hosted on remote servers when collaborating) as well\n",
"as screenshots of the Windows interface.\n",
"\n",
"Cheat sheets\n",
": Two different\n",
" [cheat](http://zrusin.blogspot.com/2007/09/git-cheat-sheet.html)\n",
" [sheets](http://jan-krueger.net/development/git-cheat-sheet-extended-edition)\n",
" in PDF format that can be printed for frequent reference."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Beyond the basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"At some point, it will pay off to understand how git itself is *built*. These two documents, written in a similar spirit, \n",
"are probably the most useful descriptions of the Git architecture short of diving into the actual implementation. They walk you through\n",
"how you would go about building a version control system with a little story. By the end you realize that Git's model is almost\n",
"an inevitable outcome of the proposed constraints:\n",
"\n",
"* The [Git parable](http://tom.preston-werner.com/2009/05/19/the-git-parable.html) by Tom Preston-Werner.\n",
"* [Git foundations](http://matthew-brett.github.com/pydagogue/foundation.html) by Matthew Brett.\n",
"\n",
"[Git ready](http://www.gitready.com)\n",
": A great website of posts on specific git-related topics, organized\n",
" by difficulty.\n",
"\n",
"[QGit](http://sourceforge.net/projects/qgit/): an excellent Git GUI\n",
": Git ships by default with gitk and git-gui, a pair of Tk graphical\n",
" clients to browse a repo and to operate in it. I personally have\n",
" found [qgit](http://sourceforge.net/projects/qgit/) to be nicer and\n",
" easier to use. It is available on modern linux distros, and since it\n",
" is based on Qt, it should run on OSX and Windows.\n",
"\n",
"[Git Magic](http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html)\n",
": Another book-size guide that has useful snippets.\n",
"\n",
"The [learning center](http://learn.github.com) at Github\n",
": Guides on a number of topics, some specific to github hosting but\n",
" much of it of general value.\n",
"\n",
"A [port](http://cworth.org/hgbook-git/tour) of the Hg book's beginning\n",
": The [Mercurial book](http://hgbook.red-bean.com) has a reputation\n",
" for clarity, so Carl Worth decided to\n",
" [port](http://cworth.org/hgbook-git/tour) its introductory chapter\n",
" to Git. It's a nicely written intro, which is possible in good\n",
" measure because of how similar the underlying models of Hg and Git\n",
" ultimately are.\n",
"\n",
"[Intermediate tips](http://andyjeffries.co.uk/articles/25-tips-for-intermediate-git-users)\n",
": A set of tips that contains some very valuable nuggets, once you're\n",
" past the basics."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"For SVN users"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want a bit more background on why the model of version control\n",
"used by Git and Mercurial (known as distributed version control) is such\n",
"a good idea, I encourage you to read this very well written\n",
"[post](http://www.joelonsoftware.com/items/2010/03/17.html) by Joel\n",
"Spolsky on the topic. After that post, Joel created a very nice\n",
"Mercurial tutorial, whose [first page](http://hginit.com/00.html)\n",
"applies equally well to git and is a very good 're-education' for anyone\n",
"coming from an SVN (or similar) background.\n",
"\n",
"In practice, I think you are better off following Joel's advice and\n",
"understanding git on its own merits instead of trying to bang SVN\n",
"concepts into git shapes. But for the occasional translation from SVN to\n",
"Git of a specific idiom, the [Git - SVN Crash\n",
"Course](http://git-scm.org/course/svn.html) can be handy."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"A few useful tips for common tasks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Better shell support\n",
"\n",
"Adding git branch info to your bash prompt and tab completion for git commands and branches is extremely useful. I suggest you at least copy:\n",
"\n",
"- [git-completion.bash](https://github.com/git/git/blob/master/contrib/completion/git-completion.bash)\n",
"- [git-prompt.sh](https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh)\n",
" \n",
"You can then source both of these files in your `~/.bashrc` and then set your prompt (I'll assume you named them as the originals but starting with a `.` at the front of the name):\n",
"\n",
" source $HOME/.git-completion.bash\n",
" source $HOME/.git-prompt.sh\n",
" PS1='[\\u@\\h \\W$(__git_ps1 \" (%s)\")]\\$ ' # adjust this to your prompt liking\n",
"\n",
"See the comments in both of those files for lots of extra functionality they offer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Embedding Git information in LaTeX documents\n",
"\n",
"(Sent by [Yaroslav Halchenko](http://www.onerussian.com))\n",
"su\n",
"I use a Make rule:\n",
"\n",
" # Helper if interested in providing proper version tag within the manuscript\n",
" revision.tex: ../misc/revision.tex.in ../.git/index\n",
" GITID=$$(git log -1 | grep -e '^commit' -e '^Date:' | sed -e 's/^[^ ]* *//g' | tr '\\n' ' '); \\\n",
" echo $$GITID; \\\n",
" sed -e \"s/GITID/$$GITID/g\" $< >| $@\n",
"\n",
"in the top level `Makefile.common` which is included in all\n",
"subdirectories which actually contain papers (hence all those\n",
"`../.git`). The `revision.tex.in` file is simply:\n",
"\n",
" % Embed GIT ID revision and date\n",
" \\def\\revision{GITID}\n",
"\n",
"The corresponding `paper.pdf` depends on `revision.tex` and includes the\n",
"line `\\input{revision}` to load up the actual revision mark."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### git export\n",
"\n",
"Git doesn't have a native export command, but this works just fine:\n",
"\n",
" git archive --prefix=fperez.org/ master | gzip > ~/tmp/source.tgz"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}