An introduction to Git

Presenter Notes

About me

Nelle Varoquaux

CBIO Mines ParisTech, U900 Institut Curie, INSERM

http://cbio.ensmp.fr/~nvaroquaux/teaching/2014-scilifelab/git.html

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 http://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

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 reset --soft HEAD^: uncommits the last commit

  • git checkout: unmodifies an unstaged file

    $ git checkout -- filename1 filename2

Presenter Notes

Exercises 1 / 4

  • Initialize a git repository.

  • Configure your git environment:
    • git config user.name "Your Name Comes Here"
    • git config user.email you@yourdomain.example.com
    • git config color.ui auto
  • Check your configuration settings by using git config --list

Presenter Notes

Exercises 2 / 4

  • Create a new file AUTHORS and add it.
  • Check what the status of your repository is, by using git status.
  • Save the state of your repository with git commit
  • Now edit the AUTHORS file, and check the status of the repository.
  • Add this file and commit it.
  • Type git log in the repository. What do you see?
  • Rename the file AUTHORS to CONTRIBUTORS using git mv
  • Now delete this file (don't forget to commit your changes).
  • Use git log to see the history of the repository.

Presenter Notes

Exercises 3 / 4

  • Create a TODO file, and add it to the staging area.

  • Remove this file from the staging area.

  • Create a python script called power.py with the following code in it:
    def power(x):

    return x ** 2

  • Add this to the staging area and commit it.

Presenter Notes

Exercises 4 / 4

  • Now edit it again, and add the following function to the bottom of the file:
    def square_root(x):

    return np.sqrt(x)

  • Use git checkout to remove the changes you've made to this file. You can check what you have done using git status.

  • Commit everything.

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...