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:
- working with remotes -- shows how to use github with git
- recording changes to repository -- a comprehensive tutorial on how to use git to track files/directories with all necessary tips.
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
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.
- Let's assume you have your project directory called "myproj", containing some files.
- Go to the directory and start git on the directory using the command below:
$ git init
- After this point the hidden file .git will be create in this myproj directory.
- Say, you are working on a file called 'readme.txt', and you can create a "checkpoint" using the following commands:
$ git add readme.txt
$ git commit -m 'This is my first checkpoint'
- 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
https://github.com/kittipatkampa/myproj
- 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.
$ git remote add origin https://github.com/kittipatkampa/myproj.git
- You want to push your revision to Github using command:
$ git push origin master
- 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
$ git push --force origin master
- At this point readme.txt will be uploaded to Github
- 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:
$ git add readme.txt
$ git commit -m 'some additional comments are added to the end of the file'
$ git push origin master
- The error message should not present at this point. Note that you don't need to do remote add or init anymore.
- If you want to know the repo connection, just do
$ git remote -v
Clone project from Github to your local machine
This use case is much easier and less problematic to the previous one.
- First you create repo on Github, say
https://github.com/kittipatkampa/myproj2
- clone the Github repo on your local machine using
$ git clone https://github.com/kittipatkampa/myproj2
- After this you can update the file on the repo and push it to Github.
- Note that you don't need to do
git init
orgit 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:
- Go to your directory for which you want to build a repo. In my case,
/Users/kittipat/Dropbox/research/python_dev
- 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:kittipat python_dev $ git status
fatal: Not a git repository (or any of the parent directories): .git
- Therefore, I initialize the repo using command
git init
.kittipat python_dev $ git init
Initialized empty Git repository in /Users/kittipat/Dropbox/research/python_dev/.git/
- I then check the status of the repo and found nothing in the "stage" area:
kittipat (master #) python_dev $ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
.ipynb_checkpoints/
date_time_conversion.ipynb
demo_decision_tree_v1.ipynb
demo_nice_pivot_table.ipynb
get_leaf_node_id.ipynb
iris.pdf
iris.png
lib_order_segmentation_python3.zip
lib_order_segmentation_python3/
my_tutorial/
programming_problems/
tsne_python/
nothing added to commit but untracked files present (use "git add" to track)
- 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.kittipat (master #) python_dev $ git add *.ipynb
kittipat (master #) python_dev $ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: date_time_conversion.ipynb
new file: demo_decision_tree_v1.ipynb
new file: demo_nice_pivot_table.ipynb
new file: get_leaf_node_id.ipynb
Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
.ipynb_checkpoints/
iris.pdf
iris.png
lib_order_segmentation_python3.zip
lib_order_segmentation_python3/
my_tutorial/
programming_problems/
tsne_python/
- Commit -- commit the snapshot with some description using commit
git commit -m 'your message'
.kittipat (master #) python_dev $ git commit -m 'all ipynb files are added and first-committed'
[master (root-commit) a2df0d9] all ipynb files are added and first-committed
4 files changed, 2875 insertions(+)
create mode 100644 date_time_conversion.ipynb
create mode 100644 demo_decision_tree_v1.ipynb
create mode 100644 demo_nice_pivot_table.ipynb
create mode 100644 get_leaf_node_id.ipynb
kittipat (master) python_dev $ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
.ipynb_checkpoints/
iris.pdf
iris.png
lib_order_segmentation_python3.zip
lib_order_segmentation_python3/
my_tutorial/
programming_problems/
tsne_python/
nothing added to commit but untracked files present (use "git add" to track)
- 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:
- Login to your account on Github and name your repo. Here I named it
python_example
. - Github will provide the url to this repo,
https://github.com/kittipatkampa/python_example.git
- so keep it for the next step.
Back to my local computer:
- 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. - Add remote repo -- Add the remote repo using command
git remote add origin <git URL>
.kittipat (master) python_dev $ git remote add origin https://github.com/kittipatkampa/python_example.git
- Push to remote repo -- Now we will push all that is committed to Github (remote repo) using command
git push -u origin master
.kittipat (master) python_dev $ git push -u origin master
Username for 'https://github.com': kittipatkampa
Password for 'https://kittipatkampa@github.com':
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 72.91 KiB | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/kittipatkampa/python_example.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
Further resources: