標籤:
1、準備工作:
下載安裝git用戶端 http://code.google.com/p/git-osx-installer/downloads/list?can=3(安裝了git用戶端,命令列中才有git命令)
註冊github帳號 https://github.com/ -->Pricing and Signup -->Create a free account
2、建立ssh:(建立ssh目的是將mac與github伺服器建立互信)
在local開啟terminal:
$cd ~/.ssh 檢查是否已經存在ssh(.ssh只是一個demo,它指的是mac存放公開金鑰的位置,如果第一次ssh串連,可跳過此步驟)
如果存在,先將已有的ssh備份,或者將建立的ssh產生到另外的目錄下
如果不存在,通過預設的參數直接產生ssh
產生過程如下:
$ssh-keygen -t rsa -C [email protected]([email protected]指的是在github註冊的email)
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/twer/.ssh/id_rsa): (此處可以走預設,即公開金鑰存放在/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 [email protected]
The key‘s randomart image is:
+--[ RSA 2048]----+
|.o.++=== |
|.ooo.+. . |
| ..* = E . |
| o = + o |
| . S o |
| . |
| |
| |
| |
+-----------------+
此處完成後表示在mac上已經成功產生了與github互信的公開金鑰
在github中添加ssh:
登陸github,選擇Account Settings-->SSH Keys 添加ssh
Title:[email protected](title盡量使用自己的郵箱,這個title與登入github賬戶的郵箱是不同概念,可以不相同)
Key:開啟你產生的id_rsa.pub檔案,將其中內容拷貝至此(使用cd命令進入id_rsa.pub目錄,再使用more id_rsa.pub命令查看公開金鑰的內容)
測試SSH:
$ssh [email protected]
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(此處表示第一次沒串連成功,yes代表再串連一次)
Warning: Permanently added ‘github.com,207.97.227.239‘ (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
(在上面那句話執行完之後可能會讓輸入使用者名稱和密碼,輸入在github註冊的使用者名稱和密碼即可,認證通過後會在本機存放區一個憑據,以後使用者名稱和密碼就不需要再輸入了)
Hi xianfuying! You‘ve successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.(如果出現Hi xxx,證明mac與github成功建立了互信)
設定本地git個人資訊:(向github提交資料時會顯示下面設定的值,以方便尋找誰對這個倉庫做了什麼事)
$git config --global user.name "your real name"
$git config --global user.email "[email protected]"
至此,git和github的設定就完成了
3、提交代碼
$git add *
$git commit -m "your commit‘s reason"(提交代碼到本地倉庫)
$git remote add alias [email protected]:xxxxx/projectName.git( 定義遠程伺服器別名為alias)
$git pull -u alias master(將代碼從github pull到本地倉庫)
$git pull -uf alias master(強制將代碼從github pull到本地倉庫)
$git push -u alias master(將本地倉庫的代碼push到github倉庫)
$git remote -v(查看遠程伺服器別名)
$git remote rm alias(刪除遠程伺服器別名)
mac os x10.8下如何使用git與github