準備工做:
下載安裝git客戶端 http://code.google.com/p/git-osx-installer/downloads/list?can=3
註冊github帳號 https://github.com/ -->Pricing and Signup -->Create a free account
建立ssh:
在local打開terminal:
$cd ~/.ssh 檢查是否已經存在ssh
若是存在,先將已有的ssh備份,或者將新建的ssh生成到另外的目錄下
若是不存在,經過默認的參數直接生成ssh
生成過程以下:
$ssh-keygen -t rsa -C xxxxx@gmail.com(註冊github時的email)
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/twer/.ssh/id_rsa):
Created directory '/Users/twer/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/twer/.ssh/id_rsa.
Your public key has been saved in /Users/twer/.ssh/id_rsa.pub.
The key fingerprint is:
18:16:11:c9:01:6c:48:09:7f:27:c6:43:0d:7f:3f:84 xxxxx@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
|.o.++=== |
|.ooo.+. . |
| ..* = E . |
| o = + o |
| . S o |
| . |
| |
| |
| |
+-----------------+
若是要修改ssh生成目錄,在藍色位置處輸入要生成的路徑,選擇默認的話,會生成在 ~/.ssh下
在github中添加ssh:
登錄github,選擇Account Settings-->SSH Public Keys 添加ssh
Title:xxxxx@gmail.com
Key:打開你生成的id_rsa.pub文件,將其中內容拷貝至此
測試SSH:
$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 xianfuying! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
在藍色位置處輸入 yes
設置本地git我的信息:
$git config --global user.name "your real name"
$git config --global user.email "xxxxx@gmail.com"
至此,git和github的設置就完成了,下面就是如何將本地代碼push到github上,以及如何從github上pull代碼了:
在github中建立Repository:
https://github.com/ --> New Repository 輸入Repository信息 projectName
在本地建立代碼庫:
建立一個文件夾做爲local repository
$mkdir test
建立一個文件
$cd test
$vi test.txt
將文件添加至local repository
$git add test.txt
初始化local repository
$git init
commit文件
$git commit -a
定義遠程服務器別名
$git remote add alias git@github.com:xxxxx/projectName.git
將本地數據push到github上
$git push alias master
這樣就能夠將本地的代碼push到github的repository中了
從github中pull代碼:
在github中搜到你想要pull的代碼,如https://github.com/edgecase/ruby_koans
選擇fork,將此repository fock到你的repository下
在本地建立local repository並初始化
使用命令:
$git pull git@github.com:xxxxx/ruby_koans.git
將github上的代碼pull到local repository中
參考:
http://apps.hi.baidu.com/share/detail/16068052
http://blog.csdn.net/banxi1988/article/details/6555293
http://apps.hi.baidu.com/share/detail/31697631