Git学习笔记——问题记录
问题一
从远程仓库拉去时出现如下报错:
kyle@kyle:/media/kyle/Software/Git$ git clone https://github.com/GDUT-Kyle/vscode_git.git
Cloning into 'vscode_git'...
fatal: unable to access 'https://github.com/GDUT-Kyle/vscode_git.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
解决方法:
经过仔细排查,发现还是由于代理设置有错,为 http 错误配置了 https 的代理,导致出错。
如果没有配置代理,可使用以下命令 取消代理 :
git config --global --unset http.proxy
git config --global --unset https.proxy
如果需要使用 代理 , http 协议通过以下命令配置,使用 7890 端口为例:
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy https://127.0.0.1:7890
socket 协议通过以下命令配置,使用 7890 端口为例:
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
也可以这样子 仅代理 GitHub :
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
#取消代理
git config --global --unset http.https://github.com.proxy
问题二
kyle@kyle:/media/kyle/Software/Git$ git clone https://github.com/GDUT-Kyle/vscode_git.git
Cloning into 'vscode_git'...
Username for 'https://github.com': GDUT-Kyle
Password for 'https://GDUT-Kyle@github.com':
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/GDUT-Kyle/vscode_git.git/'
从 2021 年 8 月 13 日开始,我们将在对 Git 操作进行身份验证时不再接受帐户密码,并将要求使用基于令牌(token)的身份验证,例如个人访问令牌(针对开发人员)或 OAuth 或 GitHub 应用程序安装令牌(针对集成商) GitHub.com 上所有经过身份验证的 Git 操作。 您也可以继续在您喜欢的地方使用 SSH 密钥。
解决方法
- 如何生成自己的
token
1、在个人设置页面,找到Setting
2、 选择开发者设置Developer setting
3、 选择个人访问令牌Personal access tokens
,然后选中生成令牌Generate new token
4、 设置token的有效期,访问权限等
选择要授予此令牌token的范围或权限。
(1) 要使用token从命令行访问仓库,请选择repo。
(2) 要使用token从命令行删除仓库,请选择delete_repo
(3) 其他根据需要进行勾选
5、 生成令牌Generate token
注意:
记得把你的token
保存下来,因为你再次刷新网页的时候,你已经没有办法看到它了,所以我还没有彻底搞清楚这个token
的使用,后续还会继续探索!
6、之后用自己生成的token
登录,把上面生成的token
粘贴到输入密码的位置,然后成功push
代码!
也可以把token
直接添加远程仓库链接中,这样就可以避免同一个仓库每次提交代码都要输入token
了:
git remote set-url origin https://<your_token>@github.com/<USERNAME>/<REPO>.git
<your_token>:换成你自己得到的token
<USERNAME>:是你自己github的用户名
<REPO>:是你的仓库名称
例如:
git remote set-url origin https://ghp_SAV2psyGbfshjashasauisuiuG3gbVy9@github.com/GDUT-Kyle/vscode_git.git/
注意:要在本地仓库目录下才能执行该指令
问题三
kyle@kyle:~/Sleipnir$ git pull
fatal: refusing to merge unrelated histories
原因是两个分支是两个不同的版本,具有不同的提交历史
解决方法
git pull origin <当前分支,如master> --allow-unrelated-histories
git自动输入密码
在项目目录下输入:
git config credential.helper store
之后请求输入过密码后,账户密码就会自动保存,以后就不用输入密码了。
密码明文记录在
$ file ~/.git-credentials
$ cat ~/.git-credentials
git pull异常
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
首先查看一下本地仓库是否已经与远程仓库链接:
git remote -v
如果没有的话则使用git remote add origin </url>
。如果有的话,则要与远程仓库进行分支合并:
git pull <remote> <branch>
# 注: master是远程仓库那里的分支,如:
git pull https://github.com/GDUT-Kyle/VINS-MONO_NOTED.git master
git历史树整理
情况:本地仓库版本低于远程仓库,例如:
- 第一天,大车和中型车都保持同版本代码。
- 第二天,大车更新了代码并推到远程仓库。
- 第三天,中型车调试代码前没有执行
git pull
,直接修改代码后就commit
,意味着本地仓库更新了版本,此时打算push
到远程仓库的时候发现报错,提示我们需要先pull
一次,再push
,我们如是执行,这个过程相当于合并分支,最后到远程仓库查看发现显示是merge
,这样不是很适合线性开发流程。
这种情况应该怎么办?
在中型车回溯到commit前的模样:
git reset --soft <commit_id>
基于修改记录(使用soft的回退会保留修改记录,使用hard的话就不会),修改完之后再执行commit
+push
。至此,远程仓库和本地仓库的历史树已被整理。
git push/pull 失败
$ git pull
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
然后,我尝试在/etc/hosts文件中删除github相关的IP:
2.205.243.166 github.com
再次测试,仍有报错:
$ git push
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.
试一下ping github.com
,如果ping不通,说明DNS错误。解决方法:
sudo vim /etc/resolv.conf,
添加: nameserver 8.8.8.8 或者 nameserver 8.8.4.4
再次ping github.com
,ping通的话应该可以获取对应IP
$ ping github.com
PING github.com (20.205.243.166) 56(84) bytes of data.
接着:
sudo gedit /etc/hosts
添加:
20.205.243.166 github.com
问题即可解决,可以正常git的远程仓库操作。