Day 2 / Morning: Python scripts, and git, and github

Etherpad: http://openetherpad.org/KTUX7IJJTx

Miscellaneous notes

To upgrade packages, you can do:

pip install -U <package>

e.g.

pip install -U ipython

will upgrade your IPython installation.

Scripting

We’re going to be combining the code in two notebooks,

and

into scripts that we can run at the command line and place under version control.

Notes and tips on scripts in Python

See the scripts here:

  • ‘sys.argv’ is a list of the command-line arguments. For example, if

    you run:

    python script.py foo bar baz

    then sys.argv will contain [‘script.py’, ‘foo’, ‘bar’, ‘baz’]

  • You don’t need to name Python scripts with ‘.py’; that’s just a convention.

  • ‘#! /usr/bin/env python’ is known as the she-bang line, and it tells

    the UNIX operating system to use ‘python’ to run the text file.

  • If you add the she-bang above, and the do ‘chmod +x <filename>’, you will be able to run the script without specifying Python.

  • ‘nano’ is a good, simple text editor that will get you 40% of the way there. Learn emacs or vim or one of the many text editors on the install page. Do not, under any circumstances, use Word.

Ways to improve these scripts:

  • put everything in a function main(args), and then put:

    if __name__ == '__main__':
       main(sys.argv)
    

    at the bottom. This lets the scripts be imported and tested without running them.

  • write tests for them :)

  • put shebangs and chmod +x them.

  • put usage instructions in.

Git

Setting up:

Possible workflow:

  • Tell git who you are:

    git config –global user.name “YOUR NAME”

    git config –global user.email “YOUR EMAIL”

  • Initiate a local repository in the folder you are currently working in:

    git init

  • Add files to be tracked under version control. If you want to track file.txt :

    git add file.txt

  • Check git status to see if you have properly added the file to version control:

    git status

  • Commit file (or files). Use descriptive commit messages:

    git commit -m “Changed introductory paragraph on the manuscript to reflect new publications.”

  • Check git status to make sure that your commit was successful:

    git status

  • To see changes between 2 files:

    git diff file1 file2

  • To check the git log of commits:

    git log

  • To remove a file from staging area (ignores changes):

    git reset filename

  • To remove a file from the git repository (not tracking file anymore):

    git rm filename

Undoing things in git:

  • If you made a commit and decide that it should not have been done then use git revert. Find the commit hash using the git log command:

    git revert 4717a5c

  • If you want to take a project back in time to a certain commit and discard the history of commits that came after it use reset with that commit’s hash. You can also use this to remove a commit that you have not pushed to a repository:

    git reset 4717a5c

  • If you want to make a new branch based on the repository at a certain commit use:

    git checkout 4717a5c

Branching:

  • Use branches to create parallel copies of your repository that can be changed and commited in parallel to the original

  • To see which branch you are currently working on:

    git branch

  • To add a new branch:

    git branch March2013Experiment

  • To switch to another branch:

    git checkout March2013Experiment

  • To swithc back to your original branch:

    git checkout master

  • If you have been working on one branch (let’s say March2013Experiment) and want to merge the contents into another branch (let’s say master) use merge.

  • Make sure you are on the branch you want it to be merged into:

    git branch #checks to see what branch you are on

    git checkout master

    git merge March2013Experiment

Using a remote repository:

  • Set up your github account see here: http://github.com

  • Create new repository on github. Give it a useful name.

  • Copy the address for your repository. Should be something like: https://github.com/username/Descriptive_Repository_Name_Here.git

  • In your local git repository:

  • To check that your remote has been added properly:

    git remote -v

  • To send your local repository commits to your remote repository:

    git push origin master

  • To get commits from the remote repository (updates local repository):

    git pull origin master

  • If you want to get the changes from the remote without changing your local repository use fetch:

    git branch newbranch

    git checkout newbranch

    git fetch origin

  • If you want to integrate these changes into your local copy then you need to merge (pull is like doing fetch+merge):

    git checkout master

    git merge newbranch

comments powered by Disqus



Edit this document!

This file can be edited directly through the Web. Anyone can update and fix errors in this document with few clicks -- no downloads needed.

  1. Go to Day 2 / Morning: Python scripts, and git, and github on GitHub.
  2. Edit files using GitHub's text editor in your web browser (see the 'Edit' tab on the top right of the file)
  3. Fill in the Commit message text box at the bottom of the page describing why you made the changes. Press the Propose file change button next to it when done.
  4. Then click Send a pull request.
  5. Your changes are now queued for review under the project's Pull requests tab on GitHub!

For an introduction to the documentation format please see the reST primer.