国内怎么愉快使用Github
如何使用Git
首先安装git
使用git打开命令窗口
配置user.name、user.email。(显示commit的用户名和邮箱)
1
2git 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 |
|
global配置
1 |
|
system配置
1 |
|
1 |
|
- 本地生成ssh密钥,中间可以修改文件名,默认就是一路回车
1 |
|
- 修改.ssh下的config文件
1 |
|
1 |
|
- 将公钥(后缀为pub)里的内容复制到github里,一键直达
不出意外能够正常使用,出意外了也很正常。
如何代理git
以上就是配置git的过程,如果你不加hosts还是无法使用,加hosts也是一会一阻断,clone、pull、push统统失败。开代理可以,但是要提前配置好才能使用,要不然挂全局也是没有用的。
对于pull和clone我们还可以使用之前的方式,但是push不行。
所以我们使用代理
- 一个代理
- 代理软件的端口号(http和socks)(我使用的是clash,默认端口号为7890)
在Github上主要用到的是https和ssh协议,所以我们要对这两种协议流量进行代理,推荐对ssh协议进行代理。
http代理(不推荐)
使用http代理
1 |
|
使用socks5(h)代理
git 底层使用 libcurl 发送 http 请求,而 libcurl 的代理使用 socks5://时会在本地解析 DNS ,应该改成 socks5h://
1 |
|
查看代理
1 |
|
取消代理
1 |
|
以上配置是针对http协议的,这时在clone和pull时就会使用代理,然而push使用http时,每次都要输入密码,很不方便,所以也要对ssh进行代理。
ssh代理(强烈推荐)
1 |
|
替换connect.exe的路径(与git在同一路径下)和端口号
以下经过我本人实验正确。
ProxyCommand如果在最上面,那么Host下的ProxyCommand将不起作用,所以要分别代理的话使用第二种。
ProxyCommand如果在最下面将不起作用。
1. 全部代理
1 |
|
2. 单独代理(推荐)
1 |
|
1 |
|
配置http代理用来pull和clone,配置ssh代理用来push
多个github账号
1 |
|
1 |
|
1 |
|
Bitwarden ssh agent对应多个GitHub
- Bitwarden创建密钥 -> 导入公钥到GitHub -> 选择验证方式为Authentication Key
- 设置代理,如果你的网络很好就不需要设置IdentityFile 可以指定私钥或公钥:
1
2# notepad %HOMEPATH%\.ssh\config
# vim ~/.ssh/config
指定私钥:免密登录
指定公钥:优先轮询对应项目
文件权限设置: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
23Host 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 %p1
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 |
|
- 使用命令行或者直接修改global config,请将下面添加到对应位置
1
2
3
4
5
6
7
8
9
10
11
12
13git 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 |
|
hexo
如果第二个github用来作为hexo,那么修改在hexo目录下的_config.yaml
1 |
|
然后执行 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 |
|
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