Github鉴权配置

3,860 阅读3分钟

全局单账号

适用于只有一个账号,只需要提交一个平台。例如,使用A账号,提交到github的A仓库。如果需要使用A账号提交github的A账号对应的仓库,使用B账号提交github的B账号对应的仓库,则参考后文提供的多账号方式

git clone 鉴权失败解决方法

image.png

  • 个人中心设置

image.png

  • 开发者设置 image.png

  • 生成token

image.png

  • token权限范围的配置 image.png

  • 生成和复制 image.png

  • 使用token作为密码

image.png

git push 每次都需要输入账号密码

通过如下配置就不需要每次都输入账号、密码了。

git config --global credential.helper store
git config --global user.email 用户邮箱
git config --global user.password token
git config --global user.name 用户名

实际上git config --global credential.helper store命令生成的是.git-credentials文件,账号密码等配置信息就会存储在这个文件内。

image.png

git push 报错 SSL_ERROR_SYSCALL

fatal: 无法访问 'github.com/xxx/xxx.git… SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

参考so 设置即可

git config --global --add remote.origin.proxy ""

image.png

多账号

清除

查看之前的配置

git config --list

移除全局账号邮件密码

git config --global --unset user.name
git config --global --unset user.email
git config --global --unset user.password

生成ssh秘钥

生成两个账号的ssh key

ssh-keygen -t rsa -C 第一个email
ssh-keygen -t rsa -C 第二个email

image.png

添加私钥

要把生成的密钥使用 ssh-add 指令添加到 ssh-agent 的身份验证代理中:

ssh-add id_rsa_a
ssh-add id_rsa_b

如果不使用 ssh-add 指令添加新的密钥到 ssh agent 中的话,系统会仍然使用 id_rsa 作为默认的 SSH Key。

为远程服务器配置密钥

新增配置:

touch ~/.ssh/config

为不同的 Git 代码托管平台服务器配置不同的 SSH 密钥。

Host github
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github

Host company-git
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_company

config 配置文件中的各项配置意思为:

  • Host:指定连接到的主机名,可以随意指定,相当于实际连接目标主机的别名;
  • User:指定使用的用户名,通常为 git,也可以不指定;
  • Hostname:指定连接到的主机的实际域名或IP地址。如果是向 Github 推送代码,则为 github.com,如果是向公司的 Git 代码托管平台推送代码,则填写公司主机的地址
  • Port:SSH 服务的端口号,默认为 22,可以不写
  • PreferredAuthentications:指定优先使用的身份验证方法,指定为publickey,即使用公钥进行身份认证。
  • IdentityFile:指定要使用的私钥文件路径,即指向你创建的私钥,我们这里分别为不通的 Git 代码托管平台指定了不同的私钥

添加公钥

image.png 将id_rsa_[xxx].pub里面的公钥贴到github中

image.png 测试

ssh -T git@github

image.png

本地仓库使用ssh方式clone和提交

image.png

切换另外一个账号

ssh -T git@github_x

image.png

特别注意:这个时候需要修改@的host为~/.ssh/config中配置的host

image.png

github action依赖

在部署github pages时我们配置了工作流yml

- name: Deploy to GitHub Pages
      uses: peaceiris/actions-gh-pages@v3
      with:
        deploy_key: ${{ secrets.OPENSSH_PRIVATE_KEY }}
        publish_dir: ./_build/html

这里得OPENSSH_PRIVATE_KEY如果不配置的话会报错:

Setup auth token

Error: Action failed with "not found deploy key or tokens"

image.png

在github上将私钥配置上去就可以了:Setting -> Security -> Secrets and Variables -> Actions 新增仓库私钥

image.png

这个时候回到github pages就能看到部署成功的链接了,相当不错。

image.png

总结下部署pages就是:

  1. 编写.github/workflows/gh-pages.yml,监听main分支变化
  2. 配置pages的私钥,新建个gh-pages的分支
  3. 提交部署,生成页面

npm 安装electron问题

npm config edit 

registry=https://registry.npmmirror.com 
electron_mirror=https://cdn.npmmirror.com/binaries/electron/ 
electron_builder_binaries_mirror=https://npmmirror.com/mirrors/electron-builder-binaries/

然后就可以安装了

sudo npm install electron@v29.1.4 -g