Using git and Github

Post date: Apr 27, 2014 8:09:14 AM

From my perspective, I think the best git or even github tutorial for an absolute beginner is from git-scm online book period. It explains very well about the concept, motivation and how to use the git tool to fulfill our needs.

I found this Github tutorial very useful:

  • Create a repository -- a short and followable tutorial. This is by no mean a perfect document, but at least get you started on Github right away.
  • How the heck do I use Github - This website is a more extended explanation for the short tutorial above.
  • Get started Git Basic -- A basic idea of how git works in general, containing nice report and video. So far I think this is the best resource for an absolute beginner to understand the git concepts from fundamentals to very advanced topics seamlessly. There are some specific pages I want to point to:

There are also great examples using git for version control on IPython Notebook:

http://nbviewer.ipython.org/github/fperez/reprosw/blob/master/Version%20Control.ipynb

http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-7-Revision-Control-Software.ipynb

There are two different ways to start using Github:

Upload your existing project directory to Github

In this case you start create project repo on your own local machine (laptop, desktop) first, and you want to upload the repo to Github later.

  1. Let's assume you have your project directory called "myproj", containing some files.
  2. Go to the directory and start git on the directory using the command below:
    1. $ git init
  1. After this point the hidden file .git will be create in this myproj directory.
  2. Say, you are working on a file called 'readme.txt', and you can create a "checkpoint" using the following commands:
      1. $ git add readme.txt
      2. $ git commit -m 'This is my first checkpoint'
  3. Now, you will need to create a repo on Github--just go there and create one. This is a one-time process. Say, its URL is
    1. https://github.com/kittipatkampa/myproj
  4. Come back to your terminal on your local computer. In this step you will need to tell git on local machine to know which repo on Github should be linked to.
      1. $ git remote add origin https://github.com/kittipatkampa/myproj.git
  5. You want to push your revision to Github using command:
      1. $ git push origin master
    1. If you got an error message saying that "fatal: remote origin already exists.", which usually happens in the first time, then you might want to force it using
      1. $ git push --force origin master
    1. At this point readme.txt will be uploaded to Github
  1. Now you come back to work on your main job readme.txt and you feel it deserves a check point, so you will just do the following:
    1. $ git add readme.txt
    2. $ git commit -m 'some additional comments are added to the end of the file'
    3. $ git push origin master
  2. The error message should not present at this point. Note that you don't need to do remote add or init anymore.
  3. If you want to know the repo connection, just do
    1. $ git remote -v

Clone project from Github to your local machine

This use case is much easier and less problematic to the previous one.

  1. First you create repo on Github, say
      1. https://github.com/kittipatkampa/myproj2
  1. clone the Github repo on your local machine using
      1. $ git clone https://github.com/kittipatkampa/myproj2
    1. After this you can update the file on the repo and push it to Github.
    2. Note that you don't need to do git init or git remote add anymore because when the project is cloned, hence those are initialized for you already.

Summary

  • git init -- initialize git repo on your current folder
  • git status -- check the status of the files
  • git add <files> -- add the files to the stage area
  • git commit -m 'description' -- commit the change with description
  • Go to github and click "Create New...", name the new repo and get the repo URL.
  • git remote add origin <Github repo URL> -- add the remote repo (e.g. from Github) to your local folder
  • git push -u origin master -- push all the commits in the master branch to remote repo origin.

Real example

I want to build a git repo for a directory of my python examples and I also want to push it to Github. Here is what I do:

On my local computer:

  1. Go to your directory for which you want to build a repo. In my case,
      1. /Users/kittipat/Dropbox/research/python_dev
    1. Initialize -- you can check whether or not the folder is initialized already by using command git status. Since my folder has not been initialized, the output is empty:
      1. kittipat python_dev $ git status
      2. fatal: Not a git repository (or any of the parent directories): .git
    2. Therefore, I initialize the repo using command git init.
      1. kittipat python_dev $ git init
      2. Initialized empty Git repository in /Users/kittipat/Dropbox/research/python_dev/.git/
    3. I then check the status of the repo and found nothing in the "stage" area:
      1. kittipat (master #) python_dev $ git status
      2. On branch master
      3. Initial commit
      4. Untracked files:
      5. (use "git add <file>..." to include in what will be committed)
      6. .DS_Store
      7. .ipynb_checkpoints/
      8. date_time_conversion.ipynb
      9. demo_decision_tree_v1.ipynb
      10. demo_nice_pivot_table.ipynb
      11. get_leaf_node_id.ipynb
      12. iris.pdf
      13. iris.png
      14. lib_order_segmentation_python3.zip
      15. lib_order_segmentation_python3/
      16. my_tutorial/
      17. programming_problems/
      18. tsne_python/
      19. nothing added to commit but untracked files present (use "git add" to track)
    4. Staging -- In this stage, I will add all my IPython notebook files (with extension .ipynb) to the stage area using command git add. After that, I checked the repo status again and found 4 files are displayed on the stage area awaiting for commit.
      1. kittipat (master #) python_dev $ git add *.ipynb
      2. kittipat (master #) python_dev $ git status
      3. On branch master
      4. Initial commit
      5. Changes to be committed:
      6. (use "git rm --cached <file>..." to unstage)
      7. new file: date_time_conversion.ipynb
      8. new file: demo_decision_tree_v1.ipynb
      9. new file: demo_nice_pivot_table.ipynb
      10. new file: get_leaf_node_id.ipynb
      11. Untracked files:
      12. (use "git add <file>..." to include in what will be committed)
      13. .DS_Store
      14. .ipynb_checkpoints/
      15. iris.pdf
      16. iris.png
      17. lib_order_segmentation_python3.zip
      18. lib_order_segmentation_python3/
      19. my_tutorial/
      20. programming_problems/
      21. tsne_python/
    5. Commit -- commit the snapshot with some description using commit git commit -m 'your message'.
      1. kittipat (master #) python_dev $ git commit -m 'all ipynb files are added and first-committed'
      2. [master (root-commit) a2df0d9] all ipynb files are added and first-committed
      3. 4 files changed, 2875 insertions(+)
      4. create mode 100644 date_time_conversion.ipynb
      5. create mode 100644 demo_decision_tree_v1.ipynb
      6. create mode 100644 demo_nice_pivot_table.ipynb
      7. create mode 100644 get_leaf_node_id.ipynb
      8. kittipat (master) python_dev $ git status
      9. On branch master
      10. Untracked files:
      11. (use "git add <file>..." to include in what will be committed)
      12. .DS_Store
      13. .ipynb_checkpoints/
      14. iris.pdf
      15. iris.png
      16. lib_order_segmentation_python3.zip
      17. lib_order_segmentation_python3/
      18. my_tutorial/
      19. programming_problems/
      20. tsne_python/
      21. nothing added to commit but untracked files present (use "git add" to track)
      22. Right now we have the repo ready on our local machine. Next step we will setup repo on Github (remote) and push all the commit from local machine to remote.

On Github:

  1. Login to your account on Github and name your repo. Here I named it python_example.
  2. Github will provide the url to this repo,
      1. https://github.com/kittipatkampa/python_example.git
    1. so keep it for the next step.

Back to my local computer:

  1. Check whether or not the folder has remote repo already using command git remote. If output is empty, the folder does not have any remote repo yet.
  2. Add remote repo -- Add the remote repo using command git remote add origin <git URL>.
      1. kittipat (master) python_dev $ git remote add origin https://github.com/kittipatkampa/python_example.git
  3. Push to remote repo -- Now we will push all that is committed to Github (remote repo) using command git push -u origin master.
      1. kittipat (master) python_dev $ git push -u origin master
      2. Username for 'https://github.com': kittipatkampa
      3. Password for 'https://kittipatkampa@github.com':
      4. Counting objects: 6, done.
      5. Delta compression using up to 4 threads.
      6. Compressing objects: 100% (6/6), done.
      7. Writing objects: 100% (6/6), 72.91 KiB | 0 bytes/s, done.
      8. Total 6 (delta 0), reused 0 (delta 0)
      9. To https://github.com/kittipatkampa/python_example.git
      10. * [new branch] master -> master
      11. Branch master set up to track remote branch master from origin.

Further resources:

  1. How to upload a project to Github
  2. Fatal: remote origin already exists
  3. Command cheat sheet