Python之建立遠端倉庫
現在的情景是,你已經在本地建立了一個Git倉庫後,又想在GitHub建立一個Git倉庫,並且讓這兩個倉庫進行遠端同步,這樣,GitHub上的倉庫既可以作為備份,又可以讓其他人通過該倉庫來協作,真是一舉多得。
第一種情況:將本地倉庫的內容上傳給遠端倉庫:
第一步,登陸GitHub,然後,在右上角找到“New repository”按鈕,建立一個新的倉庫:
建立好的倉庫
目前,在GitHub上的這個oldboy_website倉庫還是空的,GitHub告訴我們,可以從這個倉庫克隆出新的倉庫,也可以把一個已有的本地倉庫與之關聯,然後,把本地倉庫的內容推送到GitHub倉庫。
第二步,我們根據GitHub的提示,在本地已有的git_trainning倉庫(即當前git_trainning目錄)下執行命令:
$ git remote add origin [email protected]:triaquae/oldboy_website.git #新增遠端倉庫
有時會出現錯誤提示:fatal: remote origin already exists.
解決辦法如下:
1、先輸入$ git remote rm origin
2、再輸入$ git remote add origin
來自 <https://blog.csdn.net/dengjianqiang2011/article/details/9260435>
$ git push -u origin master #推到遠端
The authenticity of host 'github.com (192.30.253.113)' 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.113' (RSA) to the list of known hosts.
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (20/20), 1.76 KiB | 0 bytes/s, done.
Total 20 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To [email protected]:triaquae/oldboy_website.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
請千萬注意,把上面的triaquae替換成你自己的GitHub賬戶名,否則,你在本地關聯的就是我的遠端庫,關聯沒有問題,但是你以後推送是推不上去的,因為你的SSH Key公鑰不在我的賬戶列表中。
新增後,遠端庫的名字就是origin,這是Git預設的叫法,也可以改成別的,但是origin這個名字一看就知道是遠端庫。
把本地庫的內容推送到遠端,用git push命令,實際上是把當前分支master推送到遠端。
此時重新整理遠端倉庫頁面, 就看到了你剛從本地推上來的程式碼了
從現在起,只要本地作了提交,就可以通過命令:
|
$ git push origin master |
what ? 不信?那幫你試一下吧
建立一個index.html檔案,同時上傳到遠端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ vim index.html $ git add . $ git commit -m "add home page"
[master 8675486] add home page 1 file changed, 6 insertions(+) create mode 100644 index.html
$ git push origin master #推到遠端
Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 362 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:triaquae/oldboy_website.git 03df00a..8675486 master -> master
|
然後重新整理下遠端倉庫頁面,就看到你的新建立的檔案了
第二種情況:從遠端倉庫克隆:
我們講了先有本地庫,後有遠端庫的時候,如何關聯遠端庫。
現在,假設我們從零開發,那麼最好的方式是先建立遠端庫,然後,從遠端庫克隆。
第一步,登陸GitHub,建立一個新的倉庫,名字叫gitskills:
我們勾選Initialize this repository with a README,這樣GitHub會自動為我們建立一個README.md檔案。建立完畢
後,可以看到README.md檔案:
第二步,遠端庫已經準備好了,下一步是用命令git clone克隆一個本地庫:
在本地找一個你想存放這個遠端倉庫的目錄,然後在本地命令列用git clone 命令來克隆這個遠端庫
1 2 3 4 5 6 7 8 9 10 11 |
$ git clone [email protected]:triaquae/gitskills.git Cloning into 'gitskills'... Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts. remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Checking connectivity... done.
$ cd gitskills/ #進入剛clone下來的目錄 $ ls README.md |
克隆下來之後,你就可以在這個目錄下建立自己的工作內容了,建立的檔案提交之後同樣使用命令$ git push origin master #推到遠端。
如果有多個人協作開發,那麼每個人各自從遠端克隆一份就可以了。
你也許還注意到,GitHub給出的地址不止一個,還可以用https://github.com/triaquae/gitskills.git 這樣的地址。實際上,Git支援多種協議,預設的git://使用ssh,但也可以使用https等其他協議。
使用https除了速度慢以外,還有個最大的麻煩是每次推送都必須輸入口令,但是在某些只開放http埠的公司內部就無法使用ssh協議而只能用https。
現在的情景是,你已經在本地建立了一個Git倉庫後,又想在GitHub建立一個Git倉庫,並且讓這兩個倉庫進行遠端同步,這樣,GitHub上的倉庫既可以作為備份,又可以讓其他人通過該倉庫來協作,真是一舉多得。
第一種情況:將本地倉庫的內容上傳給遠端倉庫:
第一步,登陸GitHub,然後,在右上角找到“New repository”按鈕,建立一個新的倉庫:
建立好的倉庫
目前,在GitHub上的這個oldboy_website倉庫還是空的,GitHub告訴我們,可以從這個倉庫克隆出新的倉庫,也可以把一個已有的本地倉庫與之關聯,然後,把本地倉庫的內容推送到GitHub倉庫。
第二步,我們根據GitHub的提示,在本地已有的git_trainning倉庫(即當前git_trainning目錄)下執行命令:
$ git remote add origin [email protected]:triaquae/oldboy_website.git #新增遠端倉庫
有時會出現錯誤提示:fatal: remote origin already exists.
解決辦法如下:
1、先輸入$ git remote rm origin
2、再輸入$ git remote add origin [email protected]:djqiang/gitdemo.git 就不會報錯了!
來自 <https://blog.csdn.net/dengjianqiang2011/article/details/9260435>
$ git push -u origin master #推到遠端
The authenticity of host 'github.com (192.30.253.113)' 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.113' (RSA) to the list of known hosts.
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (20/20), 1.76 KiB | 0 bytes/s, done.
Total 20 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To [email protected]:triaquae/oldboy_website.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
請千萬注意,把上面的triaquae替換成你自己的GitHub賬戶名,否則,你在本地關聯的就是我的遠端庫,關聯沒有問題,但是你以後推送是推不上去的,因為你的SSH Key公鑰不在我的賬戶列表中。
新增後,遠端庫的名字就是origin,這是Git預設的叫法,也可以改成別的,但是origin這個名字一看就知道是遠端庫。
把本地庫的內容推送到遠端,用git push命令,實際上是把當前分支master推送到遠端。
此時重新整理遠端倉庫頁面, 就看到了你剛從本地推上來的程式碼了
從現在起,只要本地作了提交,就可以通過命令:
|
$ git push origin master |
what ? 不信?那幫你試一下吧
建立一個index.html檔案,同時上傳到遠端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ vim index.html $ git add . $ git commit -m "add home page"
[master 8675486] add home page 1 file changed, 6 insertions(+) create mode 100644 index.html
$ git push origin master #推到遠端
Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 362 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:triaquae/oldboy_website.git 03df00a..8675486 master -> master
|
然後重新整理下遠端倉庫頁面,就看到你的新建立的檔案了
第二種情況:從遠端倉庫克隆:
我們講了先有本地庫,後有遠端庫的時候,如何關聯遠端庫。
現在,假設我們從零開發,那麼最好的方式是先建立遠端庫,然後,從遠端庫克隆。
第一步,登陸GitHub,建立一個新的倉庫,名字叫gitskills:
我們勾選Initialize this repository with a README,這樣GitHub會自動為我們建立一個README.md檔案。建立完畢
後,可以看到README.md檔案:
第二步,遠端庫已經準備好了,下一步是用命令git clone克隆一個本地庫:
在本地找一個你想存放這個遠端倉庫的目錄,然後在本地命令列用git clone 命令來克隆這個遠端庫
1 2 3 4 5 6 7 8 9 10 11 |
$ git clone [email protected]:triaquae/gitskills.git Cloning into 'gitskills'... Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts. remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. Checking connectivity... done.
$ cd gitskills/ #進入剛clone下來的目錄 $ ls README.md |
克隆下來之後,你就可以在這個目錄下建立自己的工作內容了,建立的檔案提交之後同樣使用命令$ git push origin master #推到遠端。
如果有多個人協作開發,那麼每個人各自從遠端克隆一份就可以了。
你也許還注意到,GitHub給出的地址不止一個,還可以用https://github.com/triaquae/gitskills.git 這樣的地址。實際上,Git支援多種協議,預設的git://使用ssh,但也可以使用https等其他協議。
使用https除了速度慢以外,還有個最大的麻煩是每次推送都必須輸入口令,但是在某些只開放http埠的公司內部就無法使用ssh協議而只能用https。