1. 程式人生 > >用git與github建立連線

用git與github建立連線

1.註冊賬戶以及建立倉庫

要想使用github第一步當然是註冊github賬號了。之後就可以建立倉庫了(免費使用者只能建公共倉庫),Create a New Repository,填好名稱後Create

2.安裝客戶端Git

github是服務端,要想在自己電腦上使用git我們還需要一個git客戶端, 在本地倉庫選擇git init here 會多出一個 .git資料夾,右鍵Git Bash進入git命令列,為了把本地的倉庫傳到github,還需要配置ssh key

3.配置Git

(1) 首先在本地建立ssh key;

$ ssh-keygen -t rsa -C “

[email protected]” 注意ssh-keygen命令中間沒有空格,如果在ssh後面加上空格, 會得到Bad escape character ‘ygen’.的錯誤。 後面的[email protected]改為你的郵箱,之後會要求確認路徑和輸入密碼, 我們這使用預設的一路回車就行。 成功的話會在~/下生成.ssh資料夾,進去,開啟id_rsa.pub,複製裡面的key。 回到github,進入Account Settings,左邊選擇SSH Keys,Add SSH Key,title隨便填,貼上key。

(2) 為了驗證是否成功,在git bash下輸入: $ ssh -T

[email protected]
如果是第一次的會提示是否continue,輸入yes就會看到:You’ve successfully authenticated, but GitHub does not provide shell access 。這就表示已成功連上github。
(3) 接下來我們要做的就是把本地倉庫傳到github上去,在此之前還需要設定username和email,因為github每次commit都會記錄他們。
gitconfig−−globaluser.name”yourname” git config –global user.email “[email protected]

4. 提交上傳

(1) 接下來在本地倉庫裡新增一些檔案,比如README,
gitaddREADME git add README g i t c o m m i t m f i r s t c o m m i t ( 2 ) g i t h u b : git push origin master
git push命令會將本地倉庫推送到遠端伺服器。
git pull命令則相反。
修改完程式碼後,使用git status可以檢視檔案的差別,使用git add 新增要commit的檔案,也可以用git add -i來智慧新增檔案。之後git commit提交本次修改,git push上傳到github。

5.一些常見錯誤

我用git add file新增檔案時出現這樣錯誤:
fatal: Not a git repository (or any of the parent directories): .git
提示說沒有.git這樣一個目錄,解決辦法如下:
git init就可以了!
推送本地庫到遠端庫的命令:
git push origin master origin/mast