An introduction to Git

Presenter Notes

About me

Nelle Varoquaux

CBIO Mines ParisTech, U900 Institut Curie, INSERM

http://cbio.ensmp.fr/~nvaroquaux/

Presenter Notes

Why do I need version control ?

As researchers, you want:

  • reproducible research
  • fast and efficient research
images/Journal-of-Irrproducibe-Research.jpg

Presenter Notes

A story told by filenames

images/version_control.gif

"Piled Higher and Deeper" by Jorge Cham: www.phdcomics.com

Presenter Notes

Version control

Version control system:

  • keeps all the historical versions for easy tracking.
  • benefits team collaboration.
  • improves our efficiency.

Presenter Notes

Setting up Git

Presenter Notes

Creating a project

  • Initializing a new project::

    $ git init

    Initialized empty Git repository in /home/nelle/Projets/SCBC/git-test/.git/

  • Initializing from an existing project::

    $ git clone git://github.com/schacon/grit.git

Presenter Notes

Configuring Git

  • Locally: only the git repository you are working on will be affected::

    git config [options]

  • User-wide: the user's default configuration will be modified in ~/.git/config::

    git config --global [options]

  • Globally: system-wide configuration: all users are going to be affected::

    git config --system [options]

Presenter Notes

Configuring git

  • Your identity::

    $ git config --global user.name "Your Name Comes Here"

    $ git config --global user.email you@yourdomain.com

  • Setting your editor::

    $ git config --global core.editor vim

  • Checking your settings::

    $ git config --list

Presenter Notes

Saving snapshots

Presenter Notes

A few commands

  • git add: adds a file to the snapshot we want to take::

    $ git add README

  • git commit: save all the files we added to the snapshots::

    $ git commit -m "My commit message"

  • git status: show the status of the files of the repository

  • git log: show commit log

Presenter Notes

File status

  • Tracked files: files that were in the last snapshots. They can be in 3 different states:
    • unmodified
    • modified
    • staged
  • Untracked files: all the other files

Presenter Notes

File status lifecycle

images/git_file_status_lifecycle.png

Pro Git Boot, by Scott Chacon: http://git-scm.com/book

Presenter Notes

Exercises

Exercises 01_configuring_and_committing.rst

Presenter Notes

Deleting and moving files

  • git rm: Removes files from the working tree::

    git rm FILENAME

  • git mv: Move or rename a file or a directory::

    git mv FILENAME TARGET

Presenter Notes

Canceling operations

  • git reset HEAD: unstages filename::

    $ git reset HEAD filename

  • git checkout: unmodifies an unstaged file

    $ git checkout -- filename1 filename2

Presenter Notes

Exercises

Exercises 02_deleting_canceling.rst

Presenter Notes

Branching

Presenter Notes

Commits are repository snapshots.

images/git_0-300dpi.png

Presenter Notes

A branch is a pointer to a commit.

images/git_1-300dpi.png

Presenter Notes

So we can have many branches !

images/git_2-300dpi.png

Presenter Notes

But how to know in which branch we are ?

images/git_3-300dpi.png

Presenter Notes

We can switch branches.

images/git_4-300dpi.png

Presenter Notes

And commit in a branch.

images/git_5-300dpi.png

Presenter Notes

Again...

images/git_6-300dpi.png

Presenter Notes

And switch branches

images/git_7-300dpi.png

Presenter Notes

The code can diverge.

images/git_9-300dpi.png

Presenter Notes

Commands

  • git branch: manages branches

    • git branch: lists the branches of the local repository
    • git branch [branch_name]: creates a branch
    • git branch -d [branch_name]: deletes a branch
  • git checkout: moves to a branch:

    • git checkout [branch_name]: moves to a branch
    • git checkout -b [branch_name]: creates and moves to the branch branch_name

Presenter Notes

Exercises

Exercises 03_branching.rst

Presenter Notes

Merging

Presenter Notes

We had two branches

images/git_10-300dpi.png

Presenter Notes

Now, let's merge branch test on master

images/git_11-300dpi.png

Presenter Notes

And we can continue working

images/git_12-300dpi.png

Presenter Notes

Commands

  • git merge - join developments history together
  • git merge [branch_name]: merges [branch_name] onto current branch

Presenter Notes

Dealing with conflicts

When a conflict emerges, you must manually edit the files:

<<<<<<< HEAD:calc.py
print 'the average is', sum(x) / float(len(x))
print 'sumsqdiffs is', sum(diffs)
=======
avg = sum(x) / float(len(x))
sumsqdiffs = sum(diffs) / float(len(x))

print 'average is', avg, 'and sumsqdiffs is', sumsqdiffs
>>>>>>> edgier:calc.py

Presenter Notes

Exercises

Exercises 04_merging.rst

Presenter Notes

Working with a server

Presenter Notes

Remotes

Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

  • git remote lists the remote servers you have configured. Tip: For more verbosity, add -v option.
  • git remote add name url: adds the url as a remote
  • git remote rm name: remove the remote name

Presenter Notes

Github

images/github.png

Presenter Notes

Updating a repository

  • git fetch [remote-name]: fetches the branches on the remote. The branches from that remote are then accessible locally in [remote-name/branch-name]
  • git push [remote-name] [branch-name]: pushed [branch-name] onto remote [branch-name]
  • git merge [branch-name]: merges [branch-name] into the current branch

Presenter Notes

Exercises

Exercises 05_remotes.rst

Presenter Notes

The Github workflow

Presenter Notes

Private git repositories

Presenter Notes

Having private git repositories

  • Github 6$/month accounts
  • Setting up a repository on a server

Presenter Notes

Setting up git on a server

In 3 commands:

  • git clone --bare my_project my_project.git
  • scp -r my_project.git user@git.example.com:/opt/git
  • git clone user@git.example.com:/opt/git/my_project.git

Presenter Notes

Thank you for your attention

Presenter Notes