Git 爬坑 - 建立远程仓库

Git 爬坑开篇-建立远程仓库: 建立远程仓库的核心在于建立本地与 git 服务器的 ssh 连接,我们通过 git init 初始化本地仓库后,需要将本地生成的 rsa 秘钥粘贴到 github 账户底下来确保一致性,稍有不慎,Permission denied 的悲剧将会重演。

1. 建立远程 git 仓库

假设我们需要将本地的代码库 MoConfig 建立为一个远程仓库。
首先,我们在 github 网站上创建同名的 MoConfig 仓库,(这一点很重要,不然执行 push 命令时会提示“仓库不存在”的报错。)并将本地代码提交到远端。

然后,我们需要将本地的已有的代码库转换为 git 仓库 init,与远程仓库建立连接 remote add,提交到本地 addcommit

>> git init
>> git remote add origin [email protected]:{$user_name}/MoConfig.git 
>> git add .
>> git commit -m "initialize the repository"
>> git push -u origin master

正常情况下,你的本地代码库会多出一个 .git/ 文件夹,如下所示。

2. 与远程仓库建立连接失败:Permission denied

然而非常不幸地,你在创建仓库的过程中出现了 Permission dened 报错,这表明你与远程仓库的连接出现了问题。
出现这个错误的原因在于本地机器没有与 git 服务器建立 ssh 连接互信,解决办法就是在本地生成 rsa 密钥,并将此密钥复制到 github 账户下。 解决方案见 STEP-1 和 STEP-2。

Warning: Permanently added the RSA host key for IP address '13.250.177.XXX' to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
STEP-1: 生成本地 rsa 密钥,该密钥会默认保存在 /d/Users/LEE/.ssh/ 下。
>> ssh-keygen -t rsa -C "{regester_email}"

以下是命令的输出,首先设置密钥的保存地址。

Generating public/private rsa key pair.
Enter file in which to save the key (/d/Users/LEE/.ssh/id_rsa):

如果你本身有 rsa 密钥,系统会询问你是否覆盖原密钥。

/d/Users/LEE/.ssh/id_rsa already exists.
Overwrite (y/n)? 

设置访问密钥的密码,直接回车表示不设置。

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

看到如下输出表示最终密钥生成完毕,此时会有一封来自 github 的神秘邮件寄到你的注册邮箱。

Your identification has been saved in /d/Users/LEE/.ssh/id_rsa.
Your public key has been saved in /d/Users/LEE/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mr+HhpTDqdoEERASioEZBoc/6jDu39ZVahPhgZwKKj0 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|@Oo   . o        |
|Oo o   + o       |
|ooo . . . o      |
|..E. .   o .     |
|...o . oS +      |
|+  .  *o =       |
|+.  .o+oo..      |
| o oo..oo .      |
|..ooo. .oo       |
+----[SHA256]-----+
STEP-2: 将本地 rsa 密钥粘贴到 github 账户下。

我们在相关地址下找到 id_rsa.pub 文件,并将其中的内容粘贴到 github 账户下

// id_rsa.pub 的部分内容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAA
BAQCrshsFvxn/Og5uwQ7HaoD6ph4
...
[email protected]

最后,我们输入命令,测试一下是否连接成功。

>> ssh -T [email protected]

以下为正确的输出结果,表示本机与 git 服务器连接成功。

Hi Gu-Youngfeng! You've successfully authenticated, 
but GitHub does not provide shell access.
发布了14 篇原创文章 · 获赞 29 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/chikily_yongfeng/article/details/99697868