痞子衡嵌入式:第一本Git命令教程(2)- 連線(remote/clone)

痞子衡發表於2016-12-10

  今天是Git系列課程第二課,上一課我們已經學會在本地建立一個空倉庫,痞子衡今天要講的是如何將本地倉庫與遠端建立聯絡。

1.將本地倉庫掛上遠端git remote

  本地建好了倉庫,我們希望能夠掛到遠端伺服器上,方便與其他人共享。目前最流行的遠端Git伺服器當然是github,此時你需要在github上註冊賬戶並線上建立一個倉庫,此處我們輸入倉庫名為gittest
痞子衡嵌入式:第一本Git命令教程(2)- 連線(remote/clone)
  點選"Create repository"之後便彈出如下畫面,最重要的是我們可以得到一個遠端倉庫的地址:git@github.com:JayHeng/gittest.git。
痞子衡嵌入式:第一本Git命令教程(2)- 連線(remote/clone)
  有了遠端倉庫地址,我們便可以開始將本地倉庫與遠端倉庫建立聯絡:

// 與遠端建立連線之前需要保證本地倉庫為非空,即至少需要一次本地提交
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

// 本地有了提交之後,開始與遠端的地址建立聯絡
jay@pc MINGW64 /d/my_project/gittest (master)
$ git remote add origin git@github.com:JayHeng/gittest.git

// 確認本地與遠端的聯絡
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.

  很不幸,我們似乎沒有與遠端建立正確的聯絡,提示“Permission denied”,這是因為github賬號沒有設定ssh公鑰資訊所致,需要前往github網站的"account settings",依次點選"Setting -> SSH and GPG Keys"->"New SSH key",將本地的rsa key(id_rsa.pub裡的字串)填寫進去,下面是生成本地rsa key的方法:

// 建立本地rsa key(如果沒有的話,一直enter/yes;此處痞子衡已經生成過,故直接用之前的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網站設定好正確rsa key之後便可以再次嘗試與將本地與遠端進行連線:
痞子衡嵌入式:第一本Git命令教程(2)- 連線(remote/clone)

// 再試一次確認本地與遠端的聯絡
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'.

  好了,大功告成,此時我們已經成功將本地與遠端建立了聯絡,本地分支叫master,對應的遠端分支是origin。

2.克隆遠端倉庫到本地git clone

  Git是可以遠端協作的,這意味著任何人建立的共享遠端倉庫都可以被複制到任何機器上,只需要知道遠端倉庫地址即可。

// 將遠端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.

  有了遠端共享,再也不用擔心本地倉庫丟失了,想clone就clone,so easy!

相關文章