20180617_将Git的本地版本库,添加到github远程仓库

将Git的本地版本库,添加到github远程仓库

感谢廖雪峰老师提供的git教程:

传送门:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

首先登陆github。(没有github账号的童鞋,去注册一个账号,这里不多说了。)

找到 New repository,创建一个新的存储库。

 

在Repository name填入learngit,其他保持默认设置,点击“Create repository”按钮,就成功地创建了一个新的Git仓库:

 

创建好了之后,会看到新建的版本库learngit,现在里面是空的。

 

GitHub告诉我们,可以从这个仓库克隆出新的仓库,也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。

关键的命令:

git remote add origin git@github.com:mfcfine/learn.git

git push -u origin master

请千万注意,把上面的mfcfine替换成你自己的GitHub账户名,否则,你在本地关联的就是我的远程库,关联没有问题,但是你以后推送是推不上去的,因为你的SSH Key公钥不在我的账户列表中。

添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库。

我们先记住这两个命令,先不着急使用它们。

因为我现在使用的电脑,现在是第一次访问这个仓库。

现在要生成我自己电脑的SSH key。

参考这篇博客:

https://blog.igevin.info/posts/generate-ssh-key-for-git/

先查看我的电脑有没有SSH KEY。

$ ls -al ~/.ssh

如果有文件id_rsa.pub 或 id_dsa.pub,则直接进入步骤3将SSH key添加到GitHub中,否则进入第二步生成SSH key

我这里是没有的。

sample result:

total 13

drwxr-xr-x 1 mfcfi 197609   0 6月  17 15:31 ./

drwxr-xr-x 1 mfcfi 197609   0 6月  17 15:29 ../

-rw-r--r-- 1 mfcfi 197609 406 6月  17 15:31 known_hosts

生成新的ssh key

命令:

$ ssh-keygen -t rsa -C "mfcfine@163.com"

Sample result:

Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/mfcfi/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /c/Users/mfcfi/.ssh/id_rsa.

Your public key has been saved in /c/Users/mfcfi/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:KBC3SE7AGMAFsSfgrTSeDM04PeYNYk03Ua4az5MEyho mfcfine@163.com

The key's randomart image is:

+---[RSA 2048]----+

|X=Bo.+o.         |

|+@=+..o          |

|=B@=.  .         |

|B=B=. ..         |

|EB..oo. S        |

|..  *..          |

|.  . =           |

|      .          |

|                 |

+----[SHA256]-----+

这里有三处位置,会让你输入key。默认回车就好了,别给自己找麻烦。

Enter file in which to save the key (/c/Users/mfcfi/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

默认会在相应路径下(/your_home_path)生成id_rsaid_rsa.pub两个文件

将新生成的key添加到ssh-agent:

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa

再将ssh key添加到GitHub中

自己喜欢的文本编辑器打开id_rsa.pub文件(.pub为公钥文件,id_rsa为私钥文件),里面的信息即为SSH key,将这些信息复制到GitHub的Add SSH key页面即可。

打开https://github.com/settings/profile,在SSH key那栏选择New SSH Key,并将复制内容拷贝到key,选择保存。为ssh keys的title取一个标题。

 

完成后如图:

 

测试是否连接成功。

命令:

$ ssh -T git@github.com

Sample result:

Hi mfcfine! You've successfully authenticated, but GitHub does not provide shell access.

将本地git版本库的文件推送到github上。

进入git bash,切换目录到learngit。

命令:

$ cd /d/learngit

查看状态:

$ git status

Sample result:

On branch master

nothing to commit, working tree clean

查看文件:

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ dir

readme.txt  test.txt

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ cat readme.txt

kkkkGitRkkk hsdfk gg.

dfak fkkd.

dfd.

hello world.

new update 2.

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ cat test.txt

new file.

添加到远程仓库

$ git remote add origin git@github.com:mfcfine/learngit.git

提示:fatal: remote origin already exists.

因为我以前添加过,所以显示远程仓库origin已存在。

删除origin仓库

$ git remote rm origin

再次添加,并提交到远程仓库。也就是github上。

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ git remote add origin git@github.com:mfcfine/learngit.git

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ git push -u origin master

Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.

Counting objects: 15, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (9/9), done.

Writing objects: 100% (15/15), 1.17 KiB | 62.00 KiB/s, done.

Total 15 (delta 1), reused 0 (delta 0)

remote: Resolving deltas: 100% (1/1), done.

To github.com:mfcfine/learngit.git

 * [new branch]      master -> master

Branch 'master' set up to track remote branch 'master' from 'origin'.

mfcfi@DESKTOP-047TA98 MINGW64 /d/learngit (master)

$ exit

提示:

SSH警告

当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告:

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)?

这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

这个警告只会出现一次,后面的操作就不会有任何警告了。

如果你实在担心有人冒充GitHub服务器,输入yes前可以对照GitHub的RSA Key的指纹信息是否与SSH连接给出的一致。

查看github上的learngit仓库。

 

这样就完成了。

小结

要关联一个远程库,使用命令git remote add origin..

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

原文地址:https://www.cnblogs.com/mfcfine/p/9193518.html