关联github远程仓库步骤

概述

当分别在远程和本地创建好git之后, 如何将他们两个之间进行关联, 从而实现随时随地的pull,push等操作。

远程创建git后

仓库地址生成,且会有两种不同的展示方式:

https和ssh区别

【使用区别】

  • clone项目:
    使用ssh方式时,需要配置ssh key,即要将生成的SSH密钥对的公钥上传至服务器;
    使用http方式时,没有要求,可以直接克隆下来。
  • push项目:
    使用ssh方式时,不需要验证用户名和密码,之前配置过ssh key,(如果你没设置密码)直接push即可;
    使用http方式时,需要验证用户名和密码。

【结论】
HTTPS利于匿名访问,适合开源项目,可以方便被别人克隆和读取(但没有push权限);
SSH不利于匿名访问,比较适合内部项目,只要配置了SSH公钥极可自由实现clone和push操作。
所以我们采用ssh的方式配置远程仓库。

页面上会显示出我们可以进行的操作:

  • …or create a new repository on the command line
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin 仓库地址
git push -u origin master
  • …or push an existing repository from the command line
git remote add origin https://github.com/账号名/仓库名.git
git branch -M master
git push -u origin master
  • …or import code from another repository

git push -u origin master 报错

Warning: Permanently added the RSA host key for IP address '20.205.243.166' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

人话: 没有权限访问远程仓库!

添加公钥

  • 打开cmd或powershell

  • 输入 ssh-keygen -t rsa -C "你的邮箱" -- rsa 秘钥类型

  • 按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_rsa.pub 文件内容,获取到你的 public key(文件存放路径C:\Users\你的计算机用户名\.ssh文件夹中)

  • 在powershell中输入命令cat ~/.ssh/id_rsa.pub可以打印出公钥

  • 将该公钥添加到github网站->my settings->SSH Keys中

删除本地关联的远程仓库配置

git remote rm origin

查看主机是否与github网站之间的ssh通信是否连接成功

ssh -T git@github.com

设置上游要跟踪的分支,与此同时会自动执行一次git push命令

git push --set-upstream origin master

总结

本地github仓库代码关联远程github仓库需要注意的是要配置好ssh公钥,然后就可以实现自由的pull,push操作了。

posted @ 2022-11-08 17:47  青青U衿  阅读(383)  评论(0编辑  收藏  举报