折騰了將近兩個小時終于解決了OS下 git 上傳文件到 github的問題,從頭記錄下來,為有同樣問題的新手提供參考。
如果已經(jīng)配置過迁客,只想看常用指令,可以直接從第三部分開始看辞槐。
1、建立Github倉庫
首先在github建立一個倉庫粘室,這個教程很多榄檬,步驟說明也比較詳細(xì),跟著做就好衔统。注意勾選創(chuàng)建README文件鹿榜,不然上傳文件可能報(bào)錯。
2锦爵、在本地克隆倉庫
先從github上下載命令行工具(git)舱殿。https://git-scm.com/download/mac
注意:安裝完如果沒看到任何圖標(biāo)別擔(dān)心,因?yàn)樗皇瞧胀ǖ膽?yīng)用险掀。設(shè)置SSH密鑰
我們使用SSH實(shí)現(xiàn)客戶端跟github的加密通訊沪袭。
為了確保你創(chuàng)建了一個新的密鑰,你需要先確定一下原來是不是已經(jīng)有了一個密鑰樟氢。首先冈绊,打開終端(terminal),一般在Application-->Utilities中
a. 檢查ssh密鑰 (如果已經(jīng)有了密鑰埠啃,那可以跳到第四步)
b. 首先死宣,我們需要檢查電腦上是否已經(jīng)有了ssh密鑰:
cd ~/.ssh
如果提示“沒有該文件或文件夾”( “No such file or directory“ )跳到第三步,否則繼續(xù)
c.備份并移除原來的SSH密鑰
因?yàn)橐呀?jīng)有了ssh密鑰的相關(guān)文件夾碴开,所以你需要將他們備份一下并刪除掉
ls #顯示當(dāng)前文件夾下所有文件和文件夾config id_rsa id_rsa.pub known_hosts mkdir key_backup #在當(dāng)前目錄下創(chuàng)建一個叫"key_backup"的文件夾 cp id_rsa* key_backup #復(fù)制id_rsa和id_rsa.pub到key_backup里頭 rm id_rsa*
d.創(chuàng)建一個新的SSH密鑰
輸入以下命令創(chuàng)建一個新的SSH密鑰毅该。由于我們直接使用默認(rèn)設(shè)置博秫,所以當(dāng)問及保存密鑰的位置時,直接按回車眶掌。
ssh-keygen -t rsa -C "your_email@youremail.com" #使用你提供的郵件地址創(chuàng)建一個新的SSH密鑰Generating public/private rsa key pair.Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<press enter>
接下來輸入密碼
Enter passphrase (empty for no passphrase):<enter a passphrase>
Enter same passphrase again:<enter passphrase again>
回車台盯,正確的話應(yīng)該顯示如下:
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.The key fingerprint is:01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db user_name@username.comThe key's randomart image is:+--[ RSA 2048]----+| .+ + || = o O . || = * * || o = + || o S . || o o = || o . E || || |+-----------------+
e. 將你的SSH密鑰添加到github上
e.1 在github網(wǎng)站上點(diǎn)擊“Account Settings”* > Click “SSH Keys” > Click “Add SSH key”*
用文本編輯器打開id_rsa.pub文件(譯者注:直接在命令行中用vi打開最簡單)。這個文件是你的公鑰畏线,你需要打開“瀏覽隱藏文件”去招到它静盅,因?yàn)?ssh文件夾是隱藏的。需要小心的是寝殴,當(dāng)你從文件中拷貝密鑰時一定要注意密鑰文件是不包含任何空白或者換行符的蒿叠,這一點(diǎn)非常重要(譯者注:所以你拷貝的時候注意不要把多余的行或者空格拷貝進(jìn)去)
接下來將密鑰黏貼進(jìn)key的文本框中
點(diǎn)擊Add Key
e.2 測試一切是否順利 為了確保一切正常運(yùn)行,你需要嘗試下SSH登陸到github蚣常。注意市咽,不需要改變以下命令中"git@github.com"部分。
ssh -T git@github.com
你應(yīng)當(dāng)看到如下結(jié)果:
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"
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
接下來施绎,設(shè)置你的信息
現(xiàn)在你已經(jīng)設(shè)置好git并且將你的SSH密鑰輸入到github上了,接下來輪到設(shè)置你的個人信息了
a. 設(shè)置你的用戶名和郵箱
git會依舊用戶名和郵箱跟蹤是誰做的提交贞绳。并且谷醉,我們使用這些信息去關(guān)聯(lián)你的提交和github的賬戶。輸入以下命令冈闭,講name和email替換成你的名字和郵箱俱尼。名字需要是你的真實(shí)名字而不是你的github賬戶。
b. $ git config --global user.name "Firstname Lastname" #為你的機(jī)器上所有的git實(shí)例設(shè)置用戶名$ git config --global user.email "your_email@youremail.com" #為你的機(jī)器上所有的git實(shí)例設(shè)置用戶的郵箱
設(shè)置好SSH密鑰后萎攒,可以進(jìn)行clone庫:
git clone https://github.com/findingsea/myRepoForBlog.git
上面的地址在你的Github頁面可以找到:
clone成功如下:
Cloning into 'myRepoForBlog'...
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
3遇八、上傳文件
push文件
在終端cd打開要上傳的文件所在文件夾
git add .
git commit -m 'first_commit'
git remote add origin https://github.com/findingsea/myRepoForBlog.git
git push origin master
如果執(zhí)行g(shù)it remote add origin https://github.com/findingsea/myRepoForBlog.git,出現(xiàn)錯誤:
fatal: remote origin already exists
則執(zhí)行以下語句:
git remote rm origin
再往后執(zhí)行g(shù)it remote add origin https://github.com/findingsea/myRepoForBlog.git 即可耍休。
如果在執(zhí)行g(shù)it push origin master時刃永,報(bào)錯:
error:failed to push som refs to.......
則執(zhí)行以下語句:
git pull origin master