今天是 Git 系列課程第二課,上一課我們已經(jīng)學(xué)會(huì)在本地創(chuàng)建一個(gè)空倉(cāng)庫(kù),痞子衡今天要講的是如何將本地倉(cāng)庫(kù)與遠(yuǎn)程建立聯(lián)系。
1. 將本地倉(cāng)庫(kù)掛上遠(yuǎn)程 git remote
本地建好了倉(cāng)庫(kù),我們希望能夠掛到遠(yuǎn)程服務(wù)器上,方便與其他人共享。目前最流行的遠(yuǎn)程 Git 服務(wù)器當(dāng)然是 github,此時(shí)你需要在 github 上注冊(cè)賬戶并在線創(chuàng)建一個(gè)倉(cāng)庫(kù),此處我們輸入倉(cāng)庫(kù)名為 gittest
點(diǎn)擊"Create repository"之后便彈出如下畫面,最重要的是我們可以得到一個(gè)遠(yuǎn)程倉(cāng)庫(kù)的地址:git@github.com:JayHeng/gittest.git。
有了遠(yuǎn)程倉(cāng)庫(kù)地址,我們便可以開(kāi)始將本地倉(cāng)庫(kù)與遠(yuǎn)程倉(cāng)庫(kù)建立聯(lián)系:
// 與遠(yuǎn)程建立連接之前需要保證本地倉(cāng)庫(kù)為非空,即至少需要一次本地提交
jay@pc MINGW64 /d/my_project/gittest (master)
$ echo "# gittest" >> README.md
jay@pc MINGW64 /d/my_project/gittest (master)
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
jay@pc MINGW64 /d/my_project/gittest (master)
$ git commit -m "first commit"
[master (root-commit) 5fe04f8] first commit
?1 file changed, 1 insertion(+)
?create mode 100644 README.md
// 本地有了提交之后,開(kāi)始與遠(yuǎn)程的地址建立聯(lián)系
jay@pc MINGW64 /d/my_project/gittest (master)
$ git remote add origin git@github.com:JayHeng/gittest.git
// 確認(rèn)本地與遠(yuǎn)程的聯(lián)系
jay@pc MINGW64 /d/my_project/gittest (master)
$ git push -u origin master
The authenticity of host 'github.com (xxx.xx.xxx.xxx)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,xxx.xx.xxx.xxx' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
很不幸,我們似乎沒(méi)有與遠(yuǎn)程建立正確的聯(lián)系,提示“Permission denied”,這是因?yàn)?github 賬號(hào)沒(méi)有設(shè)置 ssh 公鑰信息所致,需要前往 github 網(wǎng)站的"account settings",依次點(diǎn)擊"Setting -> SSH and GPG Keys"->"New SSH key",將本地的 rsa key(id_rsa.pub 里的字符串)填寫進(jìn)去,下面是生成本地 rsa key 的方法:
// 創(chuàng)建本地 rsa key(如果沒(méi)有的話,一直 enter/yes;此處痞子衡已經(jīng)生成過(guò),故直接用之前的 key)
jay@pc MINGW64 /d/my_project/gittest (master)
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/jay/.ssh/id_rsa):
/c/Users/jay/.ssh/id_rsa already exists.
Overwrite (y/n)? n
在 github 網(wǎng)站設(shè)置好正確 rsa key 之后便可以再次嘗試與將本地與遠(yuǎn)程進(jìn)行連接:
// 再試一次確認(rèn)本地與遠(yuǎn)程的聯(lián)系
jay@pc MINGW64 /d/my_project/gittest (master)
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address 'xxx.xx.xxx.xxx' to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:JayHeng/gittest.git
?* [new branch] ? ? ?master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
好了,大功告成,此時(shí)我們已經(jīng)成功將本地與遠(yuǎn)程建立了聯(lián)系,本地分支叫 master,對(duì)應(yīng)的遠(yuǎn)程分支是 origin。
2. 克隆遠(yuǎn)程倉(cāng)庫(kù)到本地 git clone
Git 是可以遠(yuǎn)程協(xié)作的,這意味著任何人建立的共享遠(yuǎn)程倉(cāng)庫(kù)都可以被復(fù)制到任何機(jī)器上,只需要知道遠(yuǎn)程倉(cāng)庫(kù)地址即可。
// 將遠(yuǎn)程 repo 克隆到本地
jay@pc MINGW64 /d/my_project
$ git clone git@github.com:JayHeng/gittest.git
Cloning into 'gittest'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
有了遠(yuǎn)程共享,再也不用擔(dān)心本地倉(cāng)庫(kù)丟失了,想 clone 就 clone,so easy!