千寻简Git连接GitHub

🦋千寻简Git连接GitHub

📔 笔记介绍

大家好,这里是千寻简笔记,我是作者星辰,笔记内容整理并发布,内容有误请指出,笔记源码已开源,前往Gitee搜索《chihiro-notes》感谢您的观看

作者各大平台直链: GitHub | Gitee | CSDN

🅰️解决方案

1️⃣生成/添加SSH公钥

在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsaid_rsa.pub这两个文件,如果有的话,直接跳过此如下命令,如果没有的话,打开命令行,输入如下命令:

ssh-keygen -t ed25519 -C "xxxxx@xxxxx.com"  
# Generating public/private ed25519 key pair...

注意:这里的 xxxxx@xxxxx.com 只是生成的 sshkey 的名称,并不约束或要求具体命名为某个邮箱。

现网的大部分教程均讲解的使用邮箱生成,其一开始的初衷仅仅是为了便于辨识所以使用了邮箱。

按照提示完成三次回车,即可生成 ssh key。
SSH生成

通过查看 ~/.ssh/id_ed25519.pub 文件内容,获取到你的 public key。Windows在C:\Users\当前用户\.ssh\ 文件夹里。

cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....

image-20220628164526152

输入图片说明

2️⃣复制公钥到GitHub

打开GitHub:https://github.com/settings/keys

点击:new SSH Key

image-20220710183334034

3️⃣添加SSH Key

填上Title(随意写),在Key文本框里粘贴 id_rsa.pub文件里的全部内容,点击Add SSH key即可。

image-20220710183820219

4️⃣本地验证是否成功

验证是否成功,在git bash里输入下面的命令

$ ssh -T git@github.com

如果初次设置的话,会出现如下界面,输入yes 同意即可

image-20220710184314498

5️⃣设置名称和邮箱

设置usernameemail,因为GitHub每次commit都会记录他们。

$ git config --global user.name  "name"//你的GitHub登陆名
$ git config --global user.email "123@126.com"//你的GitHub注册邮箱
# 踩了一个坑,这里的邮箱一定要登录的邮箱

6️⃣连接仓库

git工具使用以下命令,看是否有没有远程仓库源。

git remote      //--git查看远程仓库信息

image-20220710184723948

出现以上错误就是改文件夹没有 git init 他不是一个仓库文件夹.

git init		//--初始化本地仓库

image-20220710184850720

这个时候本地的文件夹就多了一个.git文件。

这个时候我们在来输入测试一下, 没有任何显示就是没有仓库信息。

git remote      //--git查看远程仓库信息

image-20220710195112355

我们去复制一下仓库信息:https://github.com/MrChihiro/chihiro-notes

image-20220710195415291

添加仓库信息:

git remote add origin git@github.com:MrChihiro/chihiro-notes.git

image-20220710195700890

这样我们就已经连接到了,可以看看,输入以下命令

git remote -v

image-20220710195717818

7️⃣拉取和更新

$ git pull origin main

image-20220710195851801

合并完后将代码push到git上

image-20220628184725557

这个时候,我们会发现这里报了一个错误错误:src refspec main 不匹配任何

错误产生的原因:Github 工程默认名为了 main

由于受到"Black Lives Matter"运动的影响,GitHub 从今年 101 日起,在该平台上创建的所有新的源代码仓库将默认被命名为 “main”,而不是原先的"master"

image-20220222212805549

push 报错

image-20220222212657946

解决方案1:

官方给了提示 git branch -M main。但在执行时报错:

error: refname refs/heads/master not found
fatal: Branch rename failed

原因在于 你本地暂没发现 master 分支。

git branch -M main

是一个改名操作,所以本地要保证先有 master 分支。那就在本地先执行完基本的操作:初始化、add 、commit 之后,再 执行更名操作。

image-20220628171337990

这里就成功的把本地的master更改为main。

image-20220628171752902

更改完成后我们去push,成功提交到Gitee中。

$ git push -u origin main

image-20220628184931031

8️⃣ 常用的命令

参考文献:Git操作流程(非常详细)

查看远程仓库信息

#查看远程仓库信息
$ git remote
origin


$ git remote -v
origin  git@gitee.com:opxc/chihiro-notes.git (fetch)
origin  git@gitee.com:opxc/chihiro-notes.git (push)

初始化仓库

//初始化仓库
$ git init
Reinitialized existing Git repository in D:/GitSQL/gitee-chihiro-notes/.git/

查看分支名称

$ git branch
* main

同步一下仓库

git fetch

拉取代码与线下的git代码合并

git pull origin main

更新分支代码并提交

git add .

添加注释

git commit -m “添加注释”

合并完后将代码push到git上

git push -u origin main

报错:

chihiro@LAPTOP-3M9S1EO9 MINGW64 /d/GitSQL/GiteeChihiroNotes (main)
$ git push origin main
To gitee.com:opxc/chihiro-notes.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'gitee.com:opxc/chihiro-notes.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

报错2:

chihiro@LAPTOP-3M9S1EO9 MINGW64 /d/GitSQL/GitHubChihiroNotes (main)
$ git push -u origin main
ssh: Could not resolve hostname github.com: 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.
ssh:无法解析主机名 github.com:名称或服务未知

致命:无法从远程存储库中读取。

请确保您拥有正确的访问权限

并且存储库存在。

解决方案:https://blog.csdn.net/qq_32879029/article/details/104090548

邮箱跟登录邮箱不一致,导致没有上传权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千寻简

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值