Archive

Archive for January 7, 2013

Git / GitHub – Starter Guide

January 7, 2013 2 comments

Git is one of the most common source version control nowadays, and the most popular public server is GitHub.com

For newbie, it’s certainly difficult to start using Git at the beginning, so I decide to write this article in order to help any beginners to walk through this first start.

The environment used in this tutorial is: Windows (any version) and Cygwin command-line.

First, you need to get Cygwin from here. When you launch Cygwin to install packages, please type “git” in the package search to to check it.

Cygwin - Select Git package

Cygwin – Select Git package

Then wait for installation to be done.

Next is to visit: GitHub.com , and register an account.

Now, launch Cygwin command-line and start to work with GitHub repositories.

There are two methods to work with Github, one is over HTTPS, the other is over SSH.

Personally, I prefer to work over SSH so the following guide will be it.

Step 1: Create a SSH key

$ ssh-keygen -t rsa -C "pete.houston.17187@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xjaphx/.ssh/id_rsa):
Created directory '/home/xjaphx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/xjaphx/.ssh/id_rsa.
Your public key has been saved in /home/xjaphx/.ssh/id_rsa.pub.
The key fingerprint is:
cf:da:fb:49:67:20:ed:89:b5:df:ad:17:66:27:aa:8c pete.houston.17187@gmail.com
The key's randomart image is:

The above command will create a RSA-implemented key for email “pete.houston.17187@gmail.com“. So you’ve can just replace by your own email address for your key.

Step 2: Add newly-created SSH key to GitHub account

In order for GitHub account to acknowledge your work, you need to add your key to your account.

Go to GitHub, go to Account Settings -> SSH Keys, and click the button Add SSH Key.

Title: any name you want to recognize your device, well, your Git environment.

Key: simple, paste the content of your created SSH key, open file “id_rsa.pub” above.

Step 3: Execute ssh-agent for handle SSH work

You need to run SSH-agent always when work over SSH protocol.

$ exec ssh-agent bash

If you don’t run it, you will get this error message, when typing the following command:

$ ssh-add -l
Could not open a connection to your authentication agent.

Step 4: Config Git environment (optional)

This step is optional, but it is very helpful, when you are using Git on this environment frequently.

$ git config --global user.name "your_github_account"
$ git config --global user.email "your_github_email"
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.branch auto

The first two commands is to save your global config of your GitHub account, so you don’t need to input username and email everytime accessing your repo.
The following three commands will identify to support color, it’s pretty handy for command users to see the diffs.

Step 5: Connect to GitHub through SSH

Try to issue this command to connect to GitHub

$ ssh git@github.com
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi xjaphx! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Remember to type exactly the above command!!!
If you get the following message like above output, you’re set for GitHub environment.

Step 6: Create a repo and test

Logging into Github.com in browser and follow this URL to create new repo: Create new repo

For sample, I create a new repo named “GitDemo“, so the Git URI to work on this repo is: git@github.com:xjaphx/GitDemo.git , whereas “xjaphx” is my username, replace it by yours.

Now on Cygwin cmd-line, I will get this repo to local.

$ git clone git@github.com:xjaphx/GitDemo.git
Cloning into 'GitDemo'...
warning: You appear to have cloned an empty repository.

Ok, well, navigate to this GitDemo directory and add/remove some file for testing purposes.

$ cd GitDemo

$ touch sample.c

$ git add sample.c

$ git commit -m "add new source file"
[master (root-commit) 28a2f82] add new source file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 sample.c

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:xjaphx/GitDemo.git
 * [new branch]      master -> master

Ok, so now in browser of your repo, you will see a new file “sample.c” in Files tab.
That’s how to add file.

Do the following step to remove a file

$ git rm sample.c
rm 'sample.c'

$ git commit -m "remove unecessary file"
[master 1fe4824] remove unecessary file
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 sample.c

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (2/2), 205 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@github.com:xjaphx/GitDemo.git
   28a2f82..1fe4824  master -> master

That’s it for the very very easy steps to learn Git and use GitHub!

You can google to find more resources to learn more about Git.

These are some references, I think you should read to kick a start learning.

TutsPlus – Eay Version Control with Git / GitHub

Git-scm

GitHub Learning Center

GitHub Help

GitHub – Code School

If you have any problem working with Git, feel free to post it here, you’re all welcomed!

Cheers,

Pete Houston

Categories: Git / GitHub.com Tags: ,