国内怎么愉快使用Github

如何使用Git

  1. 首先安装git

  2. 使用git打开命令窗口

  3. 配置user.name、user.email。(显示commit的用户名和邮箱)

    1
    2
    git config --global user.name '用户名'
    git config --global user.email '邮箱'

    Git的配置一共有三个级别:system(系统级)、global(用户级)和local(版本库)。system的配置整个系统只有一个,global的配置每个账户只有一个,local的配置取决于Git版本库数量,在版本库才能看到。

    这三个级别是逐层覆盖的,首先去查找system配置,其次查找global配置,最后查找local配置。逐层查找的过程中若查到配置值,则会覆盖上一层的配置。假如三个级别都配置了用户信息,则最后生效的配置是local(版本库)级的。

即 local > global > system

local配置

1
2
git config --local user.name '用户名'
git config --local user.email '邮箱'

global配置

1
2
git config --global user.name '用户名'
git config --global user.email '邮箱'

system配置

1
2
git config --system user.name '用户名'
git config --system user.email '邮箱'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 取消全局 用户名/邮箱 配置
git config –global –unset user.name
git config –global –unset user.email

# 单独设置每个repo 用户名/邮箱
git config user.name '用户名'
git config user.email '邮箱'
# 原来git clone
git clone git@github.com:用户名/仓库名.git
# 现在git clone
git clone git@github.com:用户名/仓库名.git # one
git clone git@two.github.com:用户名/仓库名.git # two
# 或者进入.git修改config成自己的项目名

# 重建origin ,关联自己账号
git remote rm origin
git remote add origin git@one.github.com:用户名/仓库名.git
git push origin master
  1. 本地生成ssh密钥,中间可以修改文件名,默认就是一路回车
1
ssh-keygen -t rsa -C '邮箱'
  1. 修改.ssh下的config文件
1
2
notepad %HOMEPATH%\.ssh\config
# 粘贴并修改以下内容到config,修改username为电脑用户名
1
2
3
4
5
6
7
Host github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名
IdentityFile "C:\Users\Administrator\.ssh\id_rsa"
TCPKeepAlive yes
  1. 将公钥(后缀为pub)里的内容复制到github里,一键直达

不出意外能够正常使用,出意外了也很正常。

如何代理git

以上就是配置git的过程,如果你不加hosts还是无法使用,加hosts也是一会一阻断,clone、pull、push统统失败。开代理可以,但是要提前配置好才能使用,要不然挂全局也是没有用的。

对于pull和clone我们还可以使用之前的方式,但是push不行。

所以我们使用代理

  • 一个代理
  • 代理软件的端口号(http和socks)(我使用的是clash,默认端口号为7890)

在Github上主要用到的是httpsssh协议,所以我们要对这两种协议流量进行代理,推荐对ssh协议进行代理。

http代理(不推荐)

使用http代理

1
2
3
4
5
# 全局代理设置(推荐)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 只对GitHub代理(不推荐)
git config --global http.https://github.com.proxy http://127.0.0.1:7890

使用socks5(h)代理

git 底层使用 libcurl 发送 http 请求,而 libcurl 的代理使用 socks5://时会在本地解析 DNS ,应该改成 socks5h://

1
2
3
4
5
6
7
8
9
10
11
12
13
# socks5
# 全局代理(推荐)
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890
# 只对GitHub代理(不推荐)
git config --global http.https://github.com.proxy socks5://127.0.0.1:7890

# socks5h
# 全局代理(推荐)
git config --global http.proxy socks5h://127.0.0.1:7890
git config --global https.proxy socks5h://127.0.0.1:7890
# 只对GitHub代理(不推荐)
git config --global http.https://github.com.proxy socks5h://127.0.0.1:7890

查看代理

1
2
git config --global --get http.proxy
git config --global --get https.proxy

取消代理

1
2
3
git config --global --unset http.proxy 
git config --global --unset https.proxy
git config --global --unset http.https://github.com.proxy

以上配置是针对http协议的,这时在clone和pull时就会使用代理,然而push使用http时,每次都要输入密码,很不方便,所以也要对ssh进行代理。

ssh代理(强烈推荐)

1
2
3
4
5
# Linux/MacOS下
vi ~/.ssh/config

# Windows下
notepad %HOMEPATH%\.ssh\config

替换connect.exe的路径(与git在同一路径下)和端口号

以下经过我本人实验正确。

ProxyCommand如果在最上面,那么Host下的ProxyCommand将不起作用,所以要分别代理的话使用第二种。

ProxyCommand如果在最下面将不起作用。

1. 全部代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Windows用户,注意替换你的端口号和connect.exe的路径
# ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

# MacOS/Linux用户用下方这条命令,注意替换你的端口号
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

# 下面是多个github账号的配置
Host github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名
IdentityFile "C:\Users\Administrator\.ssh\one"
TCPKeepAlive yes

Host two.github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名
IdentityFile "C:\Users\Administrator\.ssh\two"
TCPKeepAlive yes

2. 单独代理(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Host github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名
IdentityFile "C:\Users\Administrator\.ssh\one"
TCPKeepAlive yes
# MacOS/Linux使用👇
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
# Windows使用👇
# ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host two.github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名
IdentityFile "C:\Users\Administrator\.ssh\two"
TCPKeepAlive yes
# MacOS/Linux使用👇
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
# Windows使用👇
# ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p
1
2
3
# 测试是否成功
ssh -T git@github.com
ssh -vT git@github.com # 加v会显示debug信息

配置http代理用来pull和clone,配置ssh代理用来push

多个github账号

1
2
3
4
5
6
# 进入%HOMEPATH%\.ssh\下生成密钥
ssh-keygen -t rsa -C '邮箱'

# pub粘贴进对应的github账号里
# vim ~/.ssh/config
# notepad %HOMEPATH%\.ssh\config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Host github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名,Windows下使用\ ,linux使用/ .
IdentityFile "C:\Users\Administrator\.ssh\one"
TCPKeepAlive yes
# MacOS/Linux使用👇
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
# Windows使用👇
# ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host two.github.com
User git
Port 443
HostName ssh.github.com
# 注意修改绝对路径,或者修改username为电脑用户名,Windows下使用\ ,linux使用/ .
IdentityFile "C:\Users\Administrator\.ssh\two"
TCPKeepAlive yes
# MacOS/Linux使用👇
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
# Windows使用👇
# ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# test
ssh -T git@github.com # 对应第一个github账号
ssh -T git@two.github.com # 对应第二个github账号
# 出现Hi,xxx则配置成功

# 原来git clone
git clone git@github.com:用户名/仓库名.git

# 现在git clone
git clone git@github.com:用户名/仓库名.git # one
git clone git@two.github.com:用户名/仓库名.git # two
# 或者进入.git/修改config成自己的项目名

# 重建origin ,关联自己账号
git remote rm origin
git remote add origin git@two.github.com:用户名/仓库名.git
git push origin master # master/main

Bitwarden ssh agent对应多个GitHub

  1. Bitwarden创建密钥 -> 导入公钥到GitHub -> 选择验证方式为Authentication Key
  2. 设置代理,如果你的网络很好就不需要设置
    1
    2
    # notepad %HOMEPATH%\.ssh\config
    # vim ~/.ssh/config
    IdentityFile 可以指定私钥或公钥:
    指定私钥:免密登录
    指定公钥:优先轮询对应项目
    文件权限设置:chmod 600 ~/.ssh/id_rsa_github(Macos/Linux下)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Host github.com
    User git
    Port 443
    HostName ssh.github.com
    # 注意修改绝对路径,或者修改username为电脑用户名,
    IdentityFile "C:\Users\Administrator\.ssh\pub"
    TCPKeepAlive yes
    # MacOS/Linux使用👇
    # ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
    # Windows使用👇
    # ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

    Host github2.com
    User git
    Port 443
    HostName ssh.github.com
    # 注意修改绝对路径,或者修改username为电脑用户名
    IdentityFile "C:\Users\Administrator\.ssh\pub2"
    TCPKeepAlive yes
    # MacOS/Linux使用👇
    # ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
    # Windows使用👇
    # ProxyCommand "C:\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 原来git clone
    git clone git@github.com:用户名/仓库名.git
    # 现在git clone
    git clone git@github.com:用户名/仓库名.git # one
    git clone git@github2.com:用户名/仓库名.git # two
    # 或者进入.git修改config成自己的项目名

    # 重建origin ,关联自己账号
    git remote rm origin
    git remote add origin git@github2.com:用户名/仓库名.git
    git push origin master # master/main

PS. 以下三种配置都能够ssh连接到GitHub,官方推荐优先顺序是:profile1 > profile2 >> profile3。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Host profile1
User git
Port 22
HostName github.com

Host profile2
User git
Port 443
HostName ssh.github.com

Host profile3
User git
Port 22
HostName ssh.github.com
  1. 使用命令行或者直接修改global config,请将下面添加到对应位置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    git config --global gpg.format ssh
    git config --global commit.gpgsign true
    git config --global user.signingkey ~/.ssh/id_ed25519.pub
    # 或者 git config --global user.signingkey ED25519 xxx
    git config --global user.email "email"
    git config --global user.name 'usename'

    # 使用下面检查输出结果是否符合预期
    git config --global gpg.format
    git config --global commit.gpgsign
    git config --global user.signingkey
    git config --global user.email
    git config --global user.name
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # notepad %HOMEPATH%\.gitconfig
    # vim ~/.gitconfig
    [user]
    email = <Email>
    name = <Username>
    signingkey = <Publickey>
    [http]
    proxy = http://127.0.0.1:7890
    [https]
    proxy = http://127.0.0.1:7890
    [gpg]
    format = ssh
    [commit]
    gpgsign = true
    [core]
    sshCommand = C:/Windows/System32/OpenSSH/ssh.exe
    [gpg "ssh"]
    program = C:/Windows/System32/OpenSSH/ssh-keygen.exe
    首先,在需要添加代理的地方请在对应位置添加代理
    其次,如果需要用到Bitwarden的ssh agent,必须设置:signingkey = <Publickey>
    最后,要想第一个轮询,IdentityFile要填写对应GitHub的公钥。
1
2
3
4
5
设置GitHub verified signature:
a. 同时导入公钥到[GitHub](https://github.com/settings/keys) -> 选择验证方式为 `Signing Key`
b. 邮箱设置要与GitHub一致,如果设置了 `Keep my email addresses private` ,那么邮箱就设置为`@users.noreply.github.com`,否则就设置为自己邮箱
c. 上面的 `.gitconfig` 都要设置完成
d. 使用 `git commit --allow-empty -m "Test SSH signing"` 测试即可

hexo

如果第二个github用来作为hexo,那么修改在hexo目录下的_config.yaml

1
2
3
4
5
6
deploy:
type: 'git'
#repository: git@github.com:用户名/仓库名.git
# 改为
repository: git@two.github.com:用户名/仓库名.git
branch: main

然后执行 hexo g -d

常见情况

a. 使用GitHub actions更新代码,使用GITHUB_TOKEN一直遇到 ! [remote rejected] main -> main (refusing to allow a GitHub App to create or update workflow .github/workflows/docker-build.yml without workflows permission).
原因是上游更新了workflows文件,但是加上permissions: write-all也不行.
解决方案是使用PAT提交,别忘记给足够的权限。
在仓库:https://github.com/<repository>/settings/secrets/actions里面新建Repository secrets,起名比如WORKFLOW_TOKEN,内容为填PAT获取的token,

1
2
git remote set-url origin "https://${{ github.actor }}:${{ secrets.WORKFLOW_TOKEN }}@github.com/${{ github.repository }}"
git push origin main --force

b. commit时不签名
git commit -m "" --no-gpg-sign

c. 创建空白分支
git checkout –orphan

d. 清理未提交的残余文件
git reset –hard
git clean -fd

e. 创建一个空的 commit
git commit –allow-empty -m “Initial commit”

参考与引用:

设置代理解决github被墙
Git的ssh配置
ssh agent
SSH 代理
https://github.com/orgs/community/discussions/35410#discussioncomment-7645702


国内怎么愉快使用Github
https://shyi.org/posts/14780/
作者
Shyi
发布于
2022年7月30日
更新于
2025年6月17日
许可协议