本地倉庫和遠程倉庫之間的操做

1、建立遠程倉庫並和本地做關聯

在本地打開git bash,而後輸入命令ssh-keygen -t rsa:git

$ ssh-keygen -t rsa -C "akipa11@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Jin/.ssh/id_rsa):
Created directory '/c/Users/Jin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Jin/.ssh/id_rsa.
Your public key has been saved in /c/Users/Jin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:54SycobqhLRV/ElJ2eulF40hv1wANdsjE40KFPlwlNY akipa11@163.com
The key's randomart image is:
+---[RSA 2048]----+
|      .+*o=+o    |
|    . .=.* E=.   |
|     o o* =+=o   |
|    . o .= =oo.  |
| . .  .oS * +    |
|..o  . o * +     |
|... o +   o      |
| . . +           |
| .o              |
+----[SHA256]-----+

此時會在本地目錄生成兩個文件,id_rsa(私鑰)和id_rsa.pub(公鑰)github

$ cd ~/.ssh

Jin@Jin-PC MINGW64 ~/.ssh
$ ls
id_rsa  id_rsa.pub

咱們把公鑰文件id_rsa.pub的內容複製,而後打開你的Github網站,在右上角點擊頭像,而後選擇「Settings」,再選擇「SSH and GPG keys」,把剛纔複製的公鑰內容粘貼到「Key」裏面,這樣咱們就能夠進行遠程倉庫操做了。bash

在Github上新建一個名爲learn_git的倉庫:dom

此時咱們就能夠從這個倉庫克隆出新的倉庫,也能夠把一個已有的本地倉庫與之關聯,而後,把本地倉庫的內容推送到GitHub倉庫。ssh

根據Github的提示,咱們在本地的Git bash輸入如下命令:ide

git remote add origin https://github.com/zhangjin0110/learn_git.git網站

而後就就能夠把本地庫的全部內容推送到遠程庫上了:3d

$ git push -u origin master
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 884 bytes | 126.00 KiB/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To https://github.com/zhangjin0110/learn_git.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

把本地庫的內容推送到遠程,用git push命令,其實是把當前分支master推送到遠程。code

因爲遠程庫是空的,咱們第一次推送master分支時,加上了-u參數,Git不但會把本地的master分支內容推送的遠程新的master分支,還會把本地的master分支和遠程的master分支關聯起來,在之後的推送或者拉取時就能夠簡化命令。blog

推送成功後,就能夠在Github上看到跟本地倉庫同樣的內容了。

只要本地的倉庫做了修改,均可以用命令git push origan master推送到Github的遠程倉庫上去。

2、從遠程倉庫克隆到本地

在Github上新建一個名爲use_git的倉庫:

而後在本地的Git bash中輸入如下命令:

$ git clone git@github.com:zhangjin0110/use_git.git
Cloning into 'use_git'...
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

再查看本地是否已經克隆成功:

$ ls
id_rsa  id_rsa.pub  known_hosts  use_git/

Jin@Jin-PC MINGW64 ~/.ssh
$ cd use_git/

Jin@Jin-PC MINGW64 ~/.ssh/use_git (master)
$ ls
README.md

Jin@Jin-PC MINGW64 ~/.ssh/use_git (master)
$ cat README.md
# use_git

能夠看到,咱們已經成功把Github上的use_git倉庫克隆到本地了。

相關文章
相關標籤/搜索