Using Git and GitHub

Like, effectively

Curtis Rueden, Mark Hiner & Johannes Koenig-Schindelin

Prelude:
Why use a version control system?

?

Common scientific development workflow

  • Email
  • Hack some code
  • Meeting
  • Hack some code
  • Student needs something
  • Hack some code
  • It is broken?
  • WTF?
  • ???
  • Boohooooo!

Common scientific development workflow, version II

  • Coffee
  • Make a change in the code
  • Test
  • Make another change in the code
  • Test
  • Send the code to a collaborator
  • It no longer works!
  • WTF?
  • ???
  • Boohooooo!

Less common scientific development workflow

  • Make changes in the code
  • It works, great!
  • Collaborator made different changes in the code!
  • How to reconcile?
  • ???
  • Boohooooo!

Most common development workflow

  • Provide code to collaborator
  • Six months later...
  • Get mail saying: your code is broken
  • Test verifies: is broken
  • Used to work!
  • Where is the working version?
  • Aargh!
  • Boohooooo!

Overview

  • Getting started
  • Diffs
  • Commits
  • Commit history
  • Merges
  • Some theory
  • Topic branches
  • Bisecting
  • Forking
  • Pull Requests
  • GitHub tricks

Getting started

http://git-scm.com/

Cloning the loci-scripts repository

						
$ git clone https://github.com/uw-loci/loci-scripts
Cloning into 'loci-scripts'...
remote: Reusing existing pack: 27, done.
remote: Total 27 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (27/27), done.
Checking connectivity... done.
						
						

Diffs

						
diff --git a/anthem.txt b/anthem.txt
new file mode 100644
index 0000000..46ea613
--- /dev/null
+++ b/anthem.txt
@@ -0,0 +1,9 @@
+Lord, grant that Marshal Wade
+May, by thy mighty aid,
+Victory bring!
+
+May he sedition hush
+And, like a torrent, rush
+Rebellious Scots to crush.
+
+God save the King!
						
						

Diffs, part II

						
diff --git a/anthem.txt b/anthem.txt
index 46ea613..d920528 100644
--- a/anthem.txt
+++ b/anthem.txt
@@ -7,3 +7,5 @@ And, like a torrent, rush
 Rebellious Scots to crush.
 
 God save the King!
+
+(melody: My country 'tis of thee)
						
						

Commits

						
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	hello.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add hello.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hello.txt
						
						

Commit messages

Short summary (50 characters or less)

More detailed explanatory text ("commit message body") after an empty
line, with additional information that cannot be inferred easily from
the diff, e.g. what bug this solves, who reported the issue.

Wrap it to 72 characters per line.

Write your commit message in the imperative: "Fix bug" and not "Fixed
bug" or "Fixes bug."  This convention matches up with commit messages
generated by commands like git merge and git revert.

Separate paragraphs with blank lines.

- Bullet points are okay, too

- Typically a hyphen or asterisk is used for the bullet, preceded by a
  single space, with blank lines in between, but conventions vary here

- Use a hanging indent
						
Source: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html

A commit reflects a revision

... that is, a working revision

... i.e. not, never ever Many changes...!!! Good example for a bad example: https://github.com/imagej/imagej/commit/ad125915d

Commit history

"git log"

  • git log
  • git log -p
  • git log origin/master
  • git log bin/
  • git log -SXmx
  • git log --author=Curtis
  • git log -3
  • git log HEAD^..HEAD^2

Merges

						
$ git merge origin/tutorial
Auto-merging bin/prairie-backup.sh
CONFLICT (content): Merge conflict in bin/prairie-backup.sh
Recorded preimage for 'bin/prairie-backup.sh'
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   bin/prairie-backup.sh

no changes added to commit (use "git add" and/or "git commit -a")
						
						

Merge conflicts

  # compress Prairie dataset to ZIP file
  echo
  echo "Compressing '$datasetFullName' to ZIP..."
  if [ -e "$zipPath" ];
  then
    # ZIP file already exists; skip this dataset
    echo "ZIP file already exists. Moving on..."
    continue
  fi
<<<<<<< HEAD
  # make the directory
  mkdir -p "${zipDir:-1}"
=======
  mkdir -p "$zipDir"
>>>>>>> master
  (cd "$datasetBase" && zip -r9 "$zipPath" "$datasetName" > /dev/null)

  # delete uncompressed Prairie dataset
  echo
  echo "Deleting '$datasetFullName'..."
						

Some theory

"content-addressable database"

Mahna mahna
do dooh bee doo doo
=> a2a100025d1516bab79163fe862475d7bd010ef8
Everything is a content-addressable object, identified by a hash of itself

Everything  is an object

  • a file is an object
  • a directory is a list of files, i.e. an object
  • a commit is a top-level directory and a list of parent commits, i.e. an object

Surprising fact: commits do not store changes!

Everything?

Not quite: HEAD is mutable. Mutable things cannot be objects.

The commit graph

Topic branches

						
$ git checkout -b my-cool-topic origin/master
Switched to a new branch 'my-cool-topic'

...
$ git push origin HEAD
Counting objects: 14, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 913 bytes | 0 bytes/s, done.
Total 8 (delta 2), reused 0 (delta 0)
To origin
 * [new branch]      HEAD -> my-cool-topic
						
						

Rewriting branches

						
$ git rebase -i origin/master
Successfully rebased and updated refs/heads/master.

$ git push origin +HEAD
Counting objects: 17, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (17/17), 12.75 KiB | 0 bytes/s, done.
Total 17 (delta 3), reused 0 (delta 0)
To origin
 + 597967a...deadbeef HEAD^ -> master (forced update)
						
						

"Interactive rebase"

pick c5ad950 Insert a comment
pick 1253d09 Fix typo
pick dbf553c Add support for XYZ

# Rebase 152ebdc..dbf553c onto 152ebdc
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
						

Fixup commits

						
$ git add bin/

$ git commit --fixup HEAD^^
[master b6c875c] fixup! Insert a comment
 1 file changed, 1 insertion(+)
						
						

Bisecting

					
$ git bisect start

$ git bisect bad HEAD

$ git bisect good HEAD@{1.day.ago}

... test ...
$ git bisect good

... test ...
$ git bisect bad

... rinse and repeat ...
... find out bad commit ...
$ git bisect reset
					
					

Forking

Network

Pull Requests

GitHub tricks

README.md

https://help.github.com/articles/github-flavored-markdown

gh-pages

See e.g. http://scijava.github.io/, backed by https://github.com/scijava/scijava.github.com.

Thank you!

ImageJ2 team: