美文网首页
本地仓库与GitHub关联

本地仓库与GitHub关联

作者: 肥树仙僧 | 来源:发表于2018-08-25 00:29 被阅读0次
  • 初始化本地仓库
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017
$ git init
Initialized empty Git repository in E:/code/vs2017/.git/

初始化后目录里会出一个.git的目录,可以见下图。

  • 新建一个read.md文档


    新建read.md文档
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ ls
digui/  hano/  read.md
  • 添加到暂存区
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git add read.md
  • 提交,但是提示说不知道我是谁,需要输入邮箱和名字
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git commit -m "first commit"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'TANGFAN@DESKTOP-9CIUUKE.(none)')
  • 根据提示输入邮箱和名字
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git config --global user.email "youremail@163.com"

TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git config --global user.name "feishu"
  • 再次提交
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git commit -m "first commit"
[master (root-commit) a14c662] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 read.md
  • 添加远程仓库
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git remote add vs2017 https:github.com/GitHubAccount/vs2017.git
  • 提示失败,因为还没有和GitHub建立联系
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git push -u vs2017 master
ssh: Could not resolve hostname https: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • 和GitHub关联,既然要关联。那么GitHub就得信任,此时非对称密钥该上场了。
  • 新建一个RSA密钥对
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ ssh-keygen -t rsa -C "youremail@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/TANGFAN/.ssh/id_rsa):
/c/Users/TANGFAN/.ssh/id_rsa already exists.
Overwrite (y/n)?
******** 这里一路enter下去就好 *********

小提示
密钥对文件存放在默认位置:/c/Users/TANGFAN/.ssh/id_rsa。自己找去吧
但是如果你是在没有用git初始化目录中生成密钥对,结果会多一些信息,但是一路enter就好

TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017
$ ssh-keygen -t rsa -C "youremail@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/TANGFAN/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/TANGFAN/.ssh/id_rsa.
Your public key has been saved in /c/Users/TANGFAN/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:QtWATf7fx1Bmdw+7Gb4l6JYZje3mzMWHTvtcC6E1MsE youremail@163.com
The key's randomart image is:
+---[RSA 2048]----+
|       ++o       |
|      .o. o      |
|      . .  E  . *|
|     .   .  .  *+|
|      . S .o B+ .|
|       .   .B+=O |
|           .o*O O|
|           .+=+O+|
|           ..oBoo|
+----[SHA256]-----+
  • 在GitHub上设置刚刚是生成的公钥

/c/Users/TANGFAN/.ssh/id_rsa用Notepad++打开公钥文件,全选并拷贝

公钥文件内容
进入GitHub账户,进行如下操作
打开设置
新建密钥
设置名称和粘贴公钥内容
添加密钥后,显示如上图所示
  • 测试是否设置成功
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017
$ ssh -T git@github.com
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)?
Host key verification failed.
********** 竟然失败,简直不能忍*******************
  • 冷静点,为什么失败。

在网上搜了一圈,说是没有写“yes”
机灵的我再折回来看看错误信息。还好是这样
再看第一次操作提示信息,Are you sure you want to continue connecting (yes/no)?问我是不是要继续连接?而我还傻头傻脑的继续按enter。哎,这货不地道哇,你就不能强制我们输入回答吗。

  • 意识到错误,我们再来一次。一定要回答yes,一定要回答yes,一定要回答yes
TANGFAN@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017
$ ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' 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,13.229.188.59' (RSA) to the list of known hosts.
Hi GitHubAccount! You've successfully authenticated, but GitHub does not provide shell access.
  • 既然已经成功了,那么同步提交到GitHub吧。顺便看下我们添加的远程仓库
TANGYUE@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git remote -v
vs2017  https://github.com/MrFeiShu/vs2017.git (fetch)
vs2017  https://github.com/MrFeiShu/vs2017.git (push)

TANGYUE@DESKTOP-9CIUUKE MINGW64 /e/code/vs2017 (master)
$ git push -u vs2017 master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 224 bytes | 224.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/MrFeiShu/vs2017.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'vs2017'.

注意,第一次push时,会提示登录GitHub账号


登录GitHub账号
  • 再看看GitHub上的结果
    read.md文档已经同步成功
    哎,终于提交上去了。回答yes那个问题一直让我怀疑人生。
    要细心哇

相关文章

网友评论

      本文标题:本地仓库与GitHub关联

      本文链接:https://www.haomeiwen.com/subject/gwhwiftx.html