git设置代理&多账号

git代理设置

  • 不建议设置全局代理, 多环境下可能混乱
  • 注意使用协议,能使用ssh协议尽量使用ssh协议方式
  • 具体的代理ip和端口视情况调整,以下用端口1080示例

HTTP协议代理设置

1、使用命令直接设置代理

--global 表示全局,不需要可以不加

git config --global https.proxy ***

例子:

# socks
git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'
# http
git config --global http.proxy http://127.0.0.1:1080 
git config --global https.proxy https://127.0.0.1:1080

# 只对github.com使用代理,其他仓库不走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消github代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

2、直接修改~/.gitconfig文件

[http]
proxy = socks5://127.0.0.1:1080
[https]
proxy = socks5://127.0.0.1:1080

3、取消代理

git config --global --unset http.proxy
git config --global --unset https.proxy

SSH协议代理设置

修改ssh配置文件~/.ssh/config

没有的话新建一个文件
Windows ssh配置文件路径:C:\Users\你的用户名\.ssh\config
Linux ssh配置文件路径:/home/你的用户名/.ssh/config

ProxyCommand connect -S 代理地址:端口 %h %p

例子

# 全局
# ProxyCommand connect -S 127.0.0.1:1080 %h %p
# 只为特定域名设定
Host github.com gitlab.com
ProxyCommand connect -S 127.0.0.1:1080 %h %p

-S 代表走socks代理。( -H 实现http和https的仓库的克隆)
多个地址设置:Host后面使用空格隔开,而不是,

多账号设置

生成公私钥,并在对应账号配置公钥

// 生成两个邮箱对应的ssh公私钥
ssh-keygen -t ed25519 -C "1@email"
ssh-keygen -t ed25519 -C "2@email"

配置例子:

# Host:仓库网站的别名,随意取
# HostName:仓库网站的域名(PS:IP 地址应该也可以)
# User:仓库网站上的用户名
# IdentityFile:私钥的绝对路径
Host aa.github.com 
Hostname ssh.github.com
Port 22
User aa
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_aa
ProxyCommand connect -S 127.0.0.1:1080 %h %p

Host bb.github.com 
Hostname ssh.github.com
Port 22
User bb
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_bb
ProxyCommand connect -S 127.0.0.1:1080 %h %p

测试:

$ ssh -vT git@仓库网站的别名
$ ssh -vT git@github.com 
$ ssh -vT git@aa.github.com 
$ ssh -vT git@bb.github.com 
clone代码
git@仓库网站的别名:项目地址.git

使用多账号时,删除全局的配置,然后再项目下单独配置

删除全局的配置
git config --global --unset user.name
git config --global --unset user.email
git config --global --unset user.password 
项目下配置
git config  user.name "name1"
git config  user.email "1@email"

之后再添加远程仓库的时候,就不能直接使用http的方式了,只能使用ssh方式.

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Git 拉取代码时不弹出账号密码弹框,可以考虑使用 SSH 认证方式。以下是具体步骤: 1. 生成 SSH 公钥和私钥 在命令行中输入以下命令: ```bash ssh-keygen -t rsa -C "your_email@example.com" ``` 其中,`your_email@example.com` 是你的邮箱地址。执行完该命令后,系统会提示你输入文件名和密码等信息。如果不需要设置密码,可以直接按回车键。 2. 将 SSH 公钥添加到 Git 账户 将 SSH 公钥添加到 Git 账户中,以便 Git 能够识别你的身份。登录 Git 账户,进入个人设置页面,找到“SSH 公钥”选项,点击“添加 SSH 公钥”,将刚才生成的公钥复制粘贴进去即可。 3. 将仓库地址改为 SSH 协议 进入要拉取代码的仓库页面,在“Clone or download”按钮下方,点击“Use SSH”按钮,将仓库地址从 https 协议改为 SSH 协议。复制该地址,然后在命令行中输入以下命令: ```bash git clone git@github.com:user/repo.git ``` 其中,`user` 是你在 Git 上的用户名,`repo` 是要拉取的仓库名称。 4. 配置 SSH 代理 如果你在使用 Git 时遇到了“Host key verification failed”或“Permission denied (publickey)”等错误,可能需要配置 SSH 代理。可以在 `~/.ssh/config` 文件中添加以下内容: ```bash Host github.com User git Hostname github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes ``` 其中,`IdentityFile` 指定了你的 SSH 私钥文件路径,`IdentitiesOnly` 表示只使用指定的私钥文件进行身份验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值