GitHub已经彻底停止使用用户名和密码的方式来更新你的代码仓库。必须使用ssh key的方法来提交代码。如果继续使用https的方式提交代码,一般都会给出“Write access to repository not granted.
remote: You do not have permission to pull the repository
fatal: unable to access 'https://gitee.com/username/projectname.git/': The requested URL returned error: 403”等报错提示。这时候你就需要将连接方式由https改为SSH。
方法也比较简单,此方法适用于github和gitee
一、生成SSH
1、打开SourceTree软件,点击最顶上菜单“动作--》在终端中打开”,执行 ssh-keygen -t rsa -C "youremail@example.com,会在.ssh目录下生成id_rsa、id_rsa.pub两个文件私钥和公钥。
2、通过cat ~/.ssh/id_rsa.pub查看公钥,并将公钥整体复制,配置到GitHub和gitee后台。可以供这两个平台使用相同的公钥。
如果是GitHub,方法是“Settings --- SSH and GPG keys --- New SSH key”添加新的就可以了。名字随便写,把复制的公钥复制到key框里就可以了。
如果是Gitee,方法是“安全设置 --- ssh公钥 --- 添加公钥”就可以了。标题随便写,把复制的公钥复制到公钥框里就可以了。
二、配置本地SSH
1、执行ssh-add ~/.ssh/id_rsa将sshkey添加到sourceTree(会要求输入开机密码)
2、执行ssh-add -K ~/.ssh/id_rsa将sshkey添加到钥匙串
3、cd 到 .ssh目录下, 用touch config命令创建config文件
4、执行open config, 打开config文件,输入以下内容并保存
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
三、公钥信任
执行ssh命令链接一下git服务器,进行公钥信任。
GitHub是ssh git@github.com
gitee是ssh git@gitee.com
期间会提示“Are you sure you want to continue connecting (yes/no/[fingerprint])? ”输入yes再按回车就可以了
四、修改SourceTree的连接方式与地址
双击点击之前配置好的代码仓库,点击“设置--》远程仓库”,编辑路径,将路径地址由https改为对应的git地址,如git@github.com:****/****.git
按照上面的步骤就完成了SSH的配置。赶紧再提交一下代码试试吧!