使用git上传代码到非本人账号的的github仓库

**背景:**本地git配置信息为本人的信息,但因为需要需要上传代码到公共github账号


整体思路

1:创建新的SSH KEY,并将生成的新的id_rsa文件中的公钥添加到github对应的sshkey,并更改本地host文件。参考博客:https://www.jianshu.com/p/12badb7e6c10 https://blog.csdn.net/komojay/article/details/80480098

2:使用git基本操作上传

3:踩坑报错解决。

①本地git init 完毕后是master,远程仓库是main,error: failed to push some refs to ‘github-teacher:NetworkCommunication/CachOf.git’
②:host文件配置错误导致:
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

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

kex_exchange_identification: banner line contains invalid characters
banner exchange: Connection to 151.101.1.194 port 443: invalid format
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
③repository已经存在项目且不是你本人提交
error: failed to push some refs to ‘github-teacher:NetworkCommunication/CachOf.git’
解决参考:https://blog.csdn.net/kiddd_fu/article/details/78247290


具体解决过程

参考https://blog.csdn.net/komojay/article/details/80480098

cd ~/.ssh
ssh-keygen -t rsa -C "XXXXXXXX"

然后会提示

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

这里我输入id_rsa_teacher。
创建完成如下:
在这里插入图片描述
id_rsa_teacher.pub是公钥,id_rsa_teacher是私钥,打开id_rsa_teacher.pub公钥,把里面的内容拷贝到github账户中的ssh key中。
更改config文件,如下,new_github是我新增加的文件。
在这里插入图片描述
如上步骤完成后第一步就完成了。

第二步:
在一个空文件夹下使用命令如下

git init
git commit -m "first commit"
git branch -M main
git remote add origin xxx //xxx为你要上传的的git仓库地址
git push -u origin main

第三步:
第一个错误使用命令

git branch -m master main

第二个错误正确配置config即可解决

第三个错误使用命令

 git pull origin main --allow-unrelated-histories

Note:

//github-teacher替换仓库地址:github.com
//因为github-teacher为我们新配置的github host相关的信息
git remote add origin git@github-teacher:NetworkCommunication/test.git
  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值