將本地庫(kù)添加到遠(yuǎn)程庫(kù)(git)
前提條件
首先需要一個(gè)本地文件夾mkdir 文件名—>git init 本地庫(kù)初始化 —>添加文件到本地庫(kù)git add 文件名(創(chuàng)建這個(gè)文件在本地庫(kù)目錄下)—>同步到本地庫(kù)git commit -m “注釋”
1.先在GitHub上創(chuàng)建一個(gè)遠(yuǎn)程庫(kù)
①登陸GitHub,然后,在右上角找到“Create a new repo”按鈕,創(chuàng)建一個(gè)新的倉(cāng)庫(kù):

②在Repository name填入learngit,其他保持默認(rèn)設(shè)置,點(diǎn)擊“Create repository”按鈕,就成功地創(chuàng)建了一個(gè)新的Git倉(cāng)庫(kù):

2.cd到本地庫(kù)地址上,通過(guò)命令行將本地庫(kù)同步到遠(yuǎn)程庫(kù)
$ git remote add origin git@github.com:github賬戶(hù)名/learngit.git
3.把本地庫(kù)的所有內(nèi)容推送到遠(yuǎn)程庫(kù)上
git push -u origin master
注意:第一次推送master分支時(shí),需要加上-u參數(shù),可以將本地的master分支推送到遠(yuǎn)程master上,并且實(shí)現(xiàn)兩者的同步,以后就不需要這個(gè)參數(shù)了:$ git push origin master
SSH警告
當(dāng)?shù)谝淮问褂肎IT的clone貨push命令連接Github時(shí)會(huì)得到一個(gè)警告
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?
輸入yes 回車(chē)即可
Git會(huì)輸出一個(gè)警告,告訴你已經(jīng)把GitHub的Key添加到本機(jī)的一個(gè)信任列表里了:
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
git遇到的問(wèn)題之“Please make sure you have the correct access rights and the repository exists.”
這個(gè)是因?yàn)閟sh key有問(wèn)題,鏈接不上服務(wù)器
解決步驟:
1、首先我得重新在git設(shè)置一下身份的名字和郵箱(因?yàn)楫?dāng)初都忘了設(shè)置啥了,因?yàn)橛龅娇恿耍┻M(jìn)入到需要提交的文件夾底下(因?yàn)橹苯哟蜷_(kāi)git Bash,在沒(méi)有路徑的情況下,根本沒(méi)!法!改!剛使用git時(shí)遇到的坑。。。)
git config --global user.name “yourname”
git config --global user.email“your@email"
注:yourname是你要設(shè)置的名字,your@email是你要設(shè)置的郵箱。
2、刪除.ssh文件夾(直接搜索該文件夾)下的known_hosts(手動(dòng)刪除即可,不需要git)
注意:.ssh是隱藏文件需要通過(guò)命令行獲取地址后 通過(guò)前往得到
3、git輸入命令
$ ssh-keygen -t rsa -C "郵箱"(請(qǐng)?zhí)钅阍O(shè)置的郵箱地址)
接著出現(xiàn):
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):
請(qǐng)直接按下回車(chē)
然后系統(tǒng)會(huì)自動(dòng)在.ssh文件夾下生成兩個(gè)文件,id_rsa和id_rsa.pub,用記事本打開(kāi)id_rsa.pub
將全部的內(nèi)容復(fù)制
4、打開(kāi)https://github.com/,登陸你的賬戶(hù),進(jìn)入設(shè)置
進(jìn)入ssh設(shè)置


粘貼復(fù)制即可
最后在命令行中輸入ssh -T git@github.com
若顯示下面文字代表成功了
You've successfully authenticated, but GitHub does not provide shell access.
注意:有時(shí)候修改本地庫(kù)內(nèi)容后,通過(guò)git push origin master發(fā)現(xiàn)遠(yuǎn)程庫(kù)沒(méi)有改動(dòng),這是因?yàn)槟阒皇切薷牧斯ぷ鲄^(qū)的內(nèi)容,并沒(méi)有改變本地庫(kù)的內(nèi)容。解決步驟:git add 文件 存放到緩存區(qū),git commit -m “注釋”(更新到本地庫(kù)中)
問(wèn)題:push出錯(cuò)
$ git push -u origin master
To git@github.com:******/Demo.git
! [rejected]? ? ? ? master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
原因:是因?yàn)檫h(yuǎn)程repository和我本地的repository沖突導(dǎo)致的,而我在創(chuàng)建版本庫(kù)后,在github的版本庫(kù)頁(yè)面點(diǎn)擊了創(chuàng)建README.md文件的按鈕創(chuàng)建了說(shuō)明文檔,但是卻沒(méi)有pull到本地。這樣就產(chǎn)生了版本沖突的問(wèn)題。
解決辦法:
方法1.強(qiáng)制push? ? ? ? git push -u origin master -f
方法2. ①push 前將遠(yuǎn)程repository修改pull下來(lái) git pull origin master —>git push -u origin master
方法3.若不想merge(合并)遠(yuǎn)程和本地修改,可以先創(chuàng)建新的分支
git branch [分支名]
git push -u origin [分支名]
最后提醒:在遠(yuǎn)程建立庫(kù)時(shí)要注意和本地庫(kù)文件進(jìn)行統(tǒng)一,如果本地庫(kù)沒(méi)有README.md文件,創(chuàng)建時(shí)就不需要初始化文件,也可以通過(guò)以上方法解決。
從遠(yuǎn)程庫(kù)克隆島本地
現(xiàn)在,假設(shè)我們從零開(kāi)發(fā),那么最好的方式是先創(chuàng)建遠(yuǎn)程庫(kù),然后,從遠(yuǎn)程庫(kù)克隆。
首先,登陸GitHub,創(chuàng)建一個(gè)新的倉(cāng)庫(kù),名字叫g(shù)itskills:

現(xiàn)在,遠(yuǎn)程庫(kù)已經(jīng)準(zhǔn)備好了,下一步是用命令git clone克隆一個(gè)本地庫(kù):
$ git clone git@github.com:github賬戶(hù)名/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
$ cd gitskills
$ ls
README.md
git 支持多種協(xié)議,包括https,但通過(guò)ssh支持的原生git協(xié)議速度更快