加速github访问、git使用及其原理、错误error: remote origin already exists,error: src refspec master does not match any,fatal: unable to access 'https://github.com/xxx.git'(你的仓库地址)
访问Github
Github:https://github.com
修改hosts
1.在C:\Windows\System32\drivers\etc找到hosts
2.将hosts文件复制到其他位置(D盘等等),再复制一份备份
3.将
#github 20.205.243.166 github.com 185.199.108.153 assets-cdn.github.com
添加到hosts最后面保存(最新2023/07/18更新,更新方法可以通过网站查ip)
4.移动到C:\Windows\System32\drivers\etc,替换其中的hosts文件
(可以把地址目录复制到资源管理器,回车直接跳转)
5.管理员模式打开cmd再输入
ipconfig /flushdns
注:只是有概率加快访问速度,也可能依然打不开
配置git
git config --global user.name "Name" #设置名字 git config --global user.email "email" #设置邮箱 git config --list #查看配置结果
生成公钥
ssh-keygen -t ed25519 -C "自命名"#引号里自己写公钥的名字
输完一直回车即可
在提示的文件夹中,找到id_ed25519.pub中生成的公钥文件
可以直接把git bash上的地址复制后粘贴到资源管理器的地址栏里
(去掉id_ed25519,最后以\.ssh结尾),c盘后面加冒号:,把斜杠换成反斜杠后回车即可找到
反斜杠“\”是电脑出现了之后为了表示程序设计里的特殊含义才发明的专用标点。除了程序设计领域外,任何地方你都不应该有使用反斜杠的时候,请永远使用正斜杠“/”。
把文件内容复制进github的SSH Keys去
克隆到本地工作目录
在指定文件夹右键选“Git Bash here”
git clone 项目.git结尾的链接
Git工作原理
(1)Workspace:工作区或工作目录
在平时项目开发中保存文件的地方。
(2)Index / Stage:暂存区
用于临时存放文件的改动信息,事实上它只是一个文件。
(3)Repository:本地仓库
用于在本地存放多个版本的文件,包括提交到这里的所有版本的数据,也称为版本库。
(4)Remote:远程仓库
托管代码的服务器上保存的文件。
将工作区文件提交到远程仓库
(1)初始化工作目录:项目所在文件夹右键选“Git Bash here”,
本地操作标签
git init
(2)将文件添加到暂存区
git add <filename> #指定文件
git add . #所有文件
git status #查看当前状态
(3)提交文件到本地仓库
git commit -m 'message'
“message”记录这一版本中的更新情况,可以写修改了啥
(4)推送到远程库
git remote add origin 远程仓库链接
git push -u origin main #main是项目默认分支 git status可查
git常见错误
1.error: remote origin already exists.
如果你clone下来一个别人的仓库,在此基础上完成你的代码,推送到自己的仓库可能遇到如下问题:
error: remote origin already exists.表示远程仓库已存在。
因此你要进行以下操作:
①先输入
git remote rm origin
删除关联的origin的远程库②关联自己的仓库 git remote add origin https://gitee.com/xxxxxx.git
③最后git push origin master或main,这样就推送到自己的仓库了。
原文链接:https://blog.csdn.net/weixin_43916997/article/details/123645376
2.error: src refspec master does not match any
git push 大坑,错误error: src refspec master does not match any. error: failed to push some refs to
最后原来是github更新了,现在github的默认分支为main,但是,我一直认为是master,所以,在提交时,需要提交到main,而不是master。
使用:
git push origin
汇总一下今天一天查到其他人遇到该问题原因:- 本地git仓库目录下为空
- 本地仓库add后未commit
- git init错误
- 没有先进行git pull
3.fatal: unable to access 'https://github.com/xxxxxxxxxxxxx'(你的仓库地址)
3.1 Failed to connect to github.com port 443 after 21038 ms: Couldn't connect to server
这是由于本机系统代理端口和git端口不一致。
解决办法:①查看并打开自己本机系统代理:
端口
②修改git配置:(10809改为你电脑的端口号)
git config --global http.proxy http://127.0.0.1:10809 git config --global https.proxy http://127.0.0.1:10809
③再次push就可以成功上传。
git push
【by the way】
然后再关掉代理,从而可以打开github
git config --global --unset http.proxy
git config --global --unset https.proxy
3.2 git Recv failure: Connection was reset
①移除旧的origin
git remote remove origin
②再重新建立新的origin
git remote add origin http://github.com/×××(你的仓库地址)
③重新push
git push
3.3 ! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/xxxxxxxxxxxxx'(你的仓库地址)
因为github上的远程库与本地库版本不一致
温柔型方案
①将本地库更新到与远程库一致的版本
git pull
但要注意本地库后来做的修改可能被覆盖,最好使用不会自动合并的
git fetch
②查看更新情况再有选择合并,或者先将本地库修改过的文件备份,
git pull
后再重新修改;③再推送即可成功
git push
详见08节的3、4点
暴力型方案
强制上传:它会忽略版本不一致等问题,强制将本地库上传的远程库
但是一定要慎使用,因为-f会用本地库覆盖掉远程库
如果远程库上有重要更新,或者有其他同伴做的修改,也都会被覆盖
所以一定要在确定无严重后果的前提下使用此操作
git push -f
3.4 OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
1. 查看自己的git代理 2. 重置代理 3.4.设置代理
git config --global --get http.proxy git config --global --unset http.proxy git config --global http.proxy '127.0.0.1:8000' git config --global https.proxy '127.0.0.1:8000'
3.5 Failed to connect to 127.0.0.1 port 7890 after 2034 ms: Couldn't connect to server
使用 git 拉取、提交代码的时出现
无法连接到127.0.0.1: xxx端口: 连接被拒绝
思路:查询当前是否有代理,如果有就取消。
# 首先,查一下当前全局的 http 代理: git config --global http.proxy # 如果有代理,就取消 git config --global --unset http.proxy # 再查 https 的代理: git config --global https.proxy # 同样的,有就取消 git config --global --unset https.proxy
关于使用Git出现“git Failed to connect to 127.0.0.1 port xxxx: Connection refused”的问题解决方案
4.The current branch main has no upstream branch.
git push --set-upstream origin main
若出现Recv failure: Connection was reset可先参照前面的解决方法先做
5.error: Your local changes to the following files would be overwritten by merge
$ git pull error: Your local changes to the following files would be overwritten by merge: pwm_moter/SpeedModify.h Please commit your changes or stash them before you merge. Aborting Updating 5e8e063..0750740
法一:stash
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。
$ git stash Saved working directory and index state WIP on main: 5e8e063 a.txt $ git commit On branch main Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working tree clean $ git stash pop On branch main Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded. (use "git pull" to update your local branch) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: pwm_moter/SpeedModify.h no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (1e80137bcf54bce7be0cafc46a587c636cef23d7)
法二:强制覆盖
$ git reset --hard HEAD is now at 5e8e063 a.txt $ git pull Updating 5e8e063..0750740 Fast-forward README.md | 2 +- pwm_moter/SpeedModify.h | 56 +++++++++++++++++++++++-------------------------- pwm_moter/pwm_moter.ino | 9 ++++---- 3 files changed, 32 insertions(+), 35 deletions(-)
本地工作与远程仓库的同步
(1)从远程仓库中拉取代码
git pull
(2)本地工作及向本地仓库的提交
git add . git commit -m 'message'
(3)推送到远程仓库
git push
(4)解决冲突:拉取最新版至本地
git pull origin main
命令查看状态和文件的差异
git status git diff
忽略特定的文件
“*”通配多个字符
“?”通配单个字符
“/”表示目录
“!”不忽略匹配到的文件或目录
.gitignore文件需要进行版本管理,不可以将其指定到.gitignore(就是把自己给忽略
Git原理进阶
文件状态与命令
分支合作
- 查看分支
git branch
- 创建分支
git branch <branch-name>
- 切换分支
git checkout <branch-name>
git switch <branch-name>
- 推送至远程仓库
git push --set-upstream origin <branch-name>(用于第1次推送)
git push(日常推送当前分支)
- 合并指定分支到当前分支
git merge <branch-name>
- 删除分支
git branch -d <branch-name> #分支未提交时不能删除
git branch -D <branch-name> #强行删除(慎用!)
标签及其应用
- 切换到需要打标签的分支上
git switch master
- 打一个新标签(标签打在当前分支最新提交的commit上)
git tag <tagname>
- 查看所有标签
git tag
- 推送某个标签到远程仓库
git push origin <tagname>