Github备忘录——Git讲解
现在很多编译软件对提交代码操作很友好,傻瓜式操作即可,但也会遇到一些问题,本章也会对此进行记录以及解决。除此之外,还会对Git指令具体含义以及“分支branch”等进行介绍(基于Linux的操作)
一、常见一些问题
Q1:
fatal: unable to access 'https://github.com/xxx/xxx.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
fatal: unable to access 'https://github.com/xxx/xxx.git/': Failed to connect to github.com port 443: Connection timed out
这个是很常见的问题,因为Github服务器在国外,平时登录github都费劲,更不用说我要上传代码到服务器端,这个解决方法:
- 上传代码时不用
HTTPS
,改用SSH
,具体则是在commit之后输入指令git remote add origin git@github.com:username/project.git
即可。注意点:
①origin后面的这一串改为自己项目在github上的ssh地址;
②origin就是你提交的分支名称,可以随意命名。
Q2:
- 首次提交代码时可能会遇到
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of
这个是因为代理,需要将
13.250.177.223 github.com
加到hosts文件里,其中hosts文件的位置:/etc/hosts
,所以这个解决方法很简单:
- 先打开hosts文件:
sudo gedit /etc/hosts
;- 在文件末尾加上
13.250.177.223 github.com
即可。
Q3:
you are about to commit crlf line separators to the git repository...
解决方法:
- 如果是用的vscode、idea、pycharm等进行提交,那只需将右下角为CRLF的文件换为LF就行了。
- 如果是指令端:
git config --global core.autocrlf input
或者git config --global core.autocrlf true
(具体选哪个看下面原因部分)
原因:
CRLF
是carriage return line feed的缩写。意思是回车换行,而LF
是line feed的缩写,意思是换行。简单来说就是不同操作系统的换行符不一样导致的错误,需要转换下格式:
- CRLF->Windows Style
- LF->Unix Style
- CR->Mac Style
所以如果代码是在windows上运行,输入
git config --global core.autocrlf true
,在unix/mac上,则输入git config --global core.autocrlf input
二、介绍分支branch
git branch # 查看本地分支
git branch -r # 查看远端分支
git branch [name] # 新建一个名为name的分支
git branch -d [name] # 删除名为name的分支
git checkout [name] # 切换到名为name分支上
git checkout -b [name] # 新建一个name的分支并切换到分支上
git merge [name] # 合并指定的分支到当前分支