GitHub是一個開源的大倉庫,咱們常常從github上下載項目進行學習和研究,下面是一個完整的步驟——往GitHub上傳一個新項目。javascript
一、註冊GitHub帳號,地址:https://github.com
php
二、登陸:java
三、登陸以後的頁面,是咱們star其餘人的一些信息,相似於QQ空間的好友狀態git
一、查看系統中是否配置過SSH keys,並處理github
1.1 終端裏輸入顯示隱藏文件:shell
defaults write com.apple.finder AppleShowAllFiles -bool true
輸入命令完成以後須要重啓Finder 桌面頂部蘋果logo->強制退出->
就可顯示隱藏文件。ruby
還有一種方法,使用命令:cd ~/.ssh 檢查是否已經存在sshbash
1.2 而後前往我的文件查看有沒有 .ssh 文件夾,有的話我的建議刪除掉,重新配置app
二、在本地配置SSH keydom
建立一個 .ssh 文件夾 命令:mkdir .ssh
gonganxinxideiMac-2:.ssh gonganxinxi$ mkdir .ssh
進入剛建立的 .ssh文件夾目錄裏 命令:cd .ssh
gonganxinxideiMac-2:.ssh gonganxinxi$ cd .ssh
命令:ssh-Keygen -t rsa -C 「youEmail」,輸入完成以後一直按回車鍵 中間會提示你要輸入文件、密碼,不用管一直按回車直到出現下面這樣。
gonganxinxideiMac-2:.ssh gonganxinxi$ ssh-Keygen -t rsa -C 「385584895@qq.com」 Generating public/private rsa key pair. Enter file in which to save the key (/Users/gonganxinxi/.ssh/id_rsa): Created directory '/Users/gonganxinxi/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/gonganxinxi/.ssh/id_rsa. Your public key has been saved in /Users/gonganxinxi/.ssh/id_rsa.pub. The key fingerprint is: SHA256:2UDch+eu01e0XGA89tE89Kpnmdk+SlTLtxijRHaIX9w 「385584895@qq.com」 The key's randomart image is: +---[RSA 2048]----+ | ... . ..o.| | .. + = Bo+| | .. B * E+| | ++ + o.=| | S .+ +o++| | . +.+B+| | +.o*o.| | o oo.o | | . o. o| +----[SHA256]-----+
指令:ls -la 查看 若是輸出相似這樣的信息,就說明配置成功
gonganxinxideiMac-2:.ssh gonganxinxi$ ls -la total 0 drwxr-xr-x 2 gonganxinxi staff 68 9 1 16:50 . drwx------ 6 gonganxinxi staff 204 9 1 16:50 ..
到目前這步應該不會有什麼問題,咱們繼續。
拷貝SSH key,會在github上進行配置的時候使用
gonganxinxideiMac-2:.ssh gonganxinxi$ pbcopy < ~/.ssh/id_rsa.pub
三、在github配置SSH key
3.1 找到SSH key配置位置
3.2 填寫SSH key配置信息
此處將在終端上使用命令pbcopy < ~/.ssh/id_rsa.pub拷貝的信息粘貼到4
位置,而後Add SSH key就添加完成了。
如圖
四、回到終端,進行SSH確認鏈接
輸入命令:ssh -T Git@github.com
執行完這條指令以後會輸出 Are you sure you want to continue connecting (yes/no)? 輸入 yes 回車
回到github,刷新網頁就能夠看到鑰匙旁的灰色小圓點變綠,就代表已經添加成功了。此時github上面的SSH key 456會變成和123同樣的綠色。(若是 網速慢,可能要稍等一會)
固然也有可能出現這樣的問題:
gonganxinxideiMac-2:.ssh gonganxinxi$ ssh -T Git@github.com The authenticity of host 'github.com (192.30.253.112)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)? yes
此時即便輸入yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts. Permission denied (publicly).
這個問題查了許久,發現是由於有時候防火牆會拒絕 SSH鏈接(Sometimes, firewalls refuse to allow SSH connections entirely. )在github的幫助中能夠看到該問題。他們也給出瞭解決方案https://help.github.com/articles/using-ssh-over-the-https-port/
經過命令:ssh -T -p 443 git@ssh.github.com使用克隆過的SSH鏈接HTTPS端口。
gonganxinxideiMac-2:.ssh gonganxinxi$ ssh -T -p 443 git@ssh.github.com The authenticity of host '[ssh.github.com]:443 ([192.30.253.123]:443)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[ssh.github.com]:443,[192.30.253.123]:443' (RSA) to the list of known hosts. Hi zhangyanxiao! You've successfully authenticated, but GitHub does not provide shell access.
看見You’ve successfully authenticated, but GitHub does not provide shell access 。這就表示已成功連上github。
成功啦,棒棒噠!!!
下面能夠在github建立倉庫,上傳項目了
一、建立github遠程倉庫
倉庫建立完成後如圖
二、建立git本地倉庫
咱們須要設置username和email,由於github每次commit都會記錄他們。
git config --global user.name "github的用戶名" git config --global user.email"註冊郵箱名"
cd到你的本地項目、根目錄下,再執行git命令
gonganxinxideiMac-2:.ssh gonganxinxi$ cd /Users/gonganxinxi/Desktop/HexTurnRGB——Demo
git本地倉庫初始化,這個時候能夠在咱們的項目文件中看到.git文件夾了
gonganxinxideiMac-2:HexTurnRGB——Demo gonganxinxi$ git init Initialized empty Git repository in /Users/gonganxinxi/Desktop/HexTurnRGB——Demo/.git/
將本地項目的全部文件添加到暫存區中
gonganxinxideiMac-2:HexTurnRGB——Demo gonganxinxi$ git add .
將暫存區的文件提交到git本地倉庫
命令:git commit -m "第一次提交" 雙引號裏面寫註釋語句
會出現不少如圖,下面還有,沒截進去。。。。
三、將git本地倉庫和Github遠程倉庫關聯
在github中打開要上傳項目的遠程倉庫,獲得網址
https://github.com/zhangyanxiao/HexTurnRGB
將本地倉庫關聯到Github上,後面的URL地址就是剛剛複製的github上倉庫的https地址
gonganxinxideiMac-2:HexTurnRGB——Demo gonganxinxi$ git remote add origin https://github.com/zhangyanxiao/HexTurnRGB
從遠程倉庫pull,獲取遠程倉庫的文件到本地倉庫(往GitHub上提交東西的時候,會由於遠程上有東西更新了可是本地倉庫沒有更新而形成提交失敗,因此咱們在push以前,都會pull一遍)
gonganxinxideiMac-2:HexTurnRGB——Demo gonganxinxi$ git pull origin master
會出現如下界面:在這裏面能夠寫pull的緣由註釋。用法可參照cocoapods導入第三方庫的使用。
最後一步,將代碼由本地倉庫上傳到Github遠程倉庫(此處,可能咱們會查看當前是否在master,使用命令:git check master)
gonganxinxideiMac-2:HexTurnRGB——Demo gonganxinxi$ git push -u origin master
哈哈,完成了呢,看圖,快👀圖👀圖
最後分享一些Github經常使用的命令: 切換分支:git checkout name 撤銷修改:git checkout -- file 刪除文件:git rm file 查看狀態:git status 添加記錄:git add file 或 git add . 添加描述:git commit -m "miao shu nei rong" 同步數據:git pull 提交數據:git push origin name 分支操做 查看分支:git branch 建立分支:git branch name 切換分支:git checkout name 建立+切換分支:git checkout -b name 合併某分支到當前分支:git merge name 刪除分支:git branch -d name 刪除遠程分支:git push origin :name