106 lines
2.2 KiB
Markdown
106 lines
2.2 KiB
Markdown
# 解决 Git 推送认证问题
|
||
|
||
## 问题现象
|
||
|
||
```
|
||
remote: Unauthorized
|
||
fatal: Authentication failed for 'http://101.43.95.130:8080/test-project/'
|
||
```
|
||
|
||
## 解决方案
|
||
|
||
### 方案一:配置 Git 凭证(推荐)
|
||
|
||
在您的本地电脑上执行以下命令:
|
||
|
||
```bash
|
||
# 配置 Git 使用您的用户名
|
||
git config --global credential.helper store
|
||
git config --global credential.http://101.43.95.130:8080.username renjianbo
|
||
|
||
# 或者直接配置项目
|
||
cd /d/ttt/test-project
|
||
git config credential.http://101.43.95.130:8080.username renjianbo
|
||
```
|
||
|
||
然后重新推送:
|
||
|
||
```bash
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
当提示输入密码时,直接按回车(留空)即可。
|
||
|
||
### 方案二:在 URL 中包含用户名
|
||
|
||
修改远程仓库 URL:
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 查看当前远程 URL
|
||
git remote -v
|
||
|
||
# 修改为包含用户名的 URL
|
||
git remote set-url origin http://renjianbo@101.43.95.130:8080/test-project
|
||
|
||
# 重新推送
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
### 方案三:使用 SSH(最可靠)
|
||
|
||
如果 HTTP 认证一直有问题,建议使用 SSH:
|
||
|
||
#### 1. 生成 SSH 密钥(如果还没有)
|
||
|
||
```bash
|
||
# 在您的本地电脑上执行
|
||
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
|
||
```
|
||
|
||
#### 2. 将公钥添加到 Gerrit
|
||
|
||
1. 访问:`http://101.43.95.130:8080/#/settings/ssh-keys`
|
||
2. 复制您的公钥内容(通常在 `~/.ssh/id_rsa.pub`)
|
||
3. 粘贴到 Gerrit 的 SSH Keys 页面
|
||
4. 点击 "Add" 保存
|
||
|
||
#### 3. 使用 SSH 克隆和推送
|
||
|
||
```bash
|
||
# 删除当前的 HTTP 远程仓库
|
||
cd /d/ttt/test-project
|
||
git remote remove origin
|
||
|
||
# 添加 SSH 远程仓库
|
||
git remote add origin ssh://renjianbo@101.43.95.130:29418/test-project
|
||
|
||
# 推送代码
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
## 快速修复(最简单)
|
||
|
||
在您的本地电脑上执行:
|
||
|
||
```bash
|
||
cd /d/ttt/test-project
|
||
|
||
# 方法 1:修改远程 URL 包含用户名
|
||
git remote set-url origin http://renjianbo@101.43.95.130:8080/test-project
|
||
|
||
# 方法 2:配置凭证
|
||
git config credential.http://101.43.95.130:8080.username renjianbo
|
||
|
||
# 重新推送
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
当提示输入密码时,直接按回车(留空)即可。
|
||
|
||
## 如果还是不行
|
||
|
||
请告诉我具体的错误信息,我会继续帮您排查。
|
||
|