目录
针对错误类型一:ssh: connect to host github.com port 22: Operation timed out
针对错误类型二:The authenticity of host 'github.com (13.229.188.59)' can't be established.
方式一:远程仓库创建项目-关联远程项目到本地(git pull)
第一步:配置ssh key
.ssh目录是针对每一个用户而言的,作为当前用户身份的识别。在当前用户家目录下查看是否有 .ssh目录(命令 cd ~/.ssh)。如果有,查看在该目录下有没有.id_rsa和.id_rsa.pub文件(命令 ls -al)。如果有执行下一步,如果没有执行操作生成key(推荐使用第二种方式生成key)。
方式1:通过默认路径方式生成
下列命令中邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,这个时候我们也可以在git bash中看到 SSH秘钥的路径。
$ ssh-keygen -t rsa -C xxx@email.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):
Created directory '/Users/xxx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0sqs/QBbhQ/9X2Lyru xxx@email.com
The key's randomart image is:
+---[RSA 2048]----+
| ..jy+=+ |
| .w++*m.+ |
| +.==q + . |
| . =m/+ty + . |
| . b.vx. . |
| + . |
| . . |
| |
| |
+----[SHA256]-----+
方式2:多人共用一台虚拟机,防覆盖-通过制定目录生成
在多人共用一台虚拟机的情况下,为了避免多个人的公钥密钥被覆盖,在生成key时可以指定生成key的文件目录(按照下方命令执行操作)
1. cd ~/.ssh
2. 生成专属ssh密钥:ssh-keygen -t rsa -f id_rsa.lmy
3. ~/.ssh/config中配置密钥地址:vim config
Host gihub.com
User XXX
IdentityFile /home/work/.ssh/id_rsa.lmy
4. chmod 600 config
5.将公钥配置到icode上:cat /home/work/.ssh/id_rsa.lmy.pub
第二步: 登陆GitHub配置公钥
登陆 https://github.com ->settings->ssh and gpg keys
add key :Key文本框里粘贴id_rsa.pub文件的内容
第三步: 测试ssh key是否成功
$ ssh -T git@github.com
错误类型一:ssh: connect to host github.com port 22: Operation timed out
错误类型二:The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nX1RMNTCspRoTxY8.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
报错解决方式
针对错误类型一:ssh: connect to host github.com port 22: Operation timed out
针对错误类型二:The authenticity of host 'github.com (13.229.188.59)' can't be established.
在.ssh目录下创建config文件,文件中内容配置如下:
以上解决方案引自:github连接报"ssh: connect to host github.com port 22: Connection timed out"错误 - 星朝 - 博客园
还有一种解决方案,推荐使用第一种:https://www.cnblogs.com/simonbaker/p/7191260.html
MacBook-Pro:.ssh xxx$ ssh -T git@github.com
ssh: connect to host github.com port 22: Operation timed out
MacBook-Pro:.ssh xxxx$ ls -al
total 16
drwx------ 4 xxx staff 128 4 23 15:38 .
drwxr-xr-x+ 18 xxx staff 576 4 23 15:38 ..
-rw------- 1 xxx staff 1823 4 23 15:38 id_rsa
-rw-r--r-- 1 xxx staff 397 4 23 15:38 id_rsa.pub
MacBook-Pro:.ssh xxx$ vim config
MacBook-Pro:.ssh xxx$ ls -al
total 24
drwx------ 5 xxx staff 160 4 23 15:48 .
drwxr-xr-x+ 18 xxx staff 576 4 23 15:48 ..
-rw-r--r-- 1 xxx staff 132 4 23 15:48 config
-rw------- 1 xxx staff 1823 4 23 15:38 id_rsa
-rw-r--r-- 1 xxx staff 397 4 23 15:38 id_rsa.pub
MacBook-Pro:.ssh xxx$ ssh -T git@github.com
The authenticity of host '[ssh.github.com]:443 ([192.30.253.122]:443)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspGl7E1IGOC.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[ssh.github.com]:443,[192.30.253.122]:443' (RSA) to the list of known hosts.
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.
成功了!!!
第四步:安装git命令行操作工具,否则会报错如下:
点击“安装”,安装上了git工具,从这一步起可以开始相关git操作了。项目协同走起来
第五步:git开始操作项目
方式一:远程仓库创建项目-关联远程项目到本地(git pull)
方式二:本地创建项目-关联本地项目到远程
常见操作指令集
#仓库
git remote -v #查看远程连接
git remote remove origin #取消与远程仓库的连接
git init #初始化仓库
git remote add origin https://github.com/XXX.git #连接新仓库
#分支
git branch -r #查看远端全部分支
git branch -l. #查看本地全部分支
git checkout -b #创建分支
git checkout #切换分支
#代码提交and合并
git status #检查状态
git checkout -- 文件名 #丢弃在工作区的改动,修改被彻底丢弃
git add . #提交到 本地暂存区 git add <文件>... 更新要提交的内容
git restore --staged <文件>... #取消暂存区到 本地
git restore <文件>... #丢弃工作区的改动
git commit -m "说明" #提交说明,提交代码到 本地仓库
git push --set-upstream origin 本地分支名 #为推送当前分支并建立与远程上游的跟踪
git merge 分支名称 #本地合并其他分支代码
git push #推送到远程
git push -u br #推送到远程分支
git push origin HEAD:refs/for/(分支) #推送到远程分支 https://cloud.baidu-int.com/icloud/iCode/Git-client-message/push_error/#no_diff
为推送当前分支并建立与远程上游的跟踪,使用: git push --set-upstream origin [本地分支名称]
最后,总结一句常见的操作步骤,git都会在命令行有对应的提示,根据提示操作即可解决大部分问题!
问题:明明本地有改动,为什么git diff 查询出来的数据为空?
git diff 对比的是本地和暂存区的对比,如果本地改动已经通过git add 提交至暂存区,则git diff将查无结果。
问题:如何进行代码回滚和找回?(此处主要说代码已经提交如何进行回滚)
第六步:解决冲突相关操作
1.通过暂存解决临时代码的存储
git stash
git stash save [名称]
git stash pop
git stash list
git stash apply [名称]
#其他命令详见:https://www.cnblogs.com/tocy/p/git-stash-reference.html
其他stash使用推荐博客:git-stash用法小结 - Tocy - 博客园
2.通过get reset --hard进行版本回退
第七步:idea工具之git快捷操作
1.idea 指定合并固定文件
只合并指定分支的指定文件,其他文件不进行提交和合并。git merge会把某一分支的全部文件进行合并,但有时因特殊场景需要,只需要对指定的某个文件进行合并,其他并不需要合并,操作方式见下:
idea git合并某个分支下的指定文件_旅人mfy的博客-CSDN博客_为什么idea没有get from branch