大家好,我是全栈小5,欢迎来到《小5讲堂》。
这是《源代码管理工具》系列文章,每篇文章将以博主理解的角度展开讲解。
温馨提示:博主能力有限,理解水平有限,若有不对之处望指正!

目录
- 前言
- 错误提示
- 解决方案
- 为什么会冲突
-
- [1. 代理配置冲突](#1. 代理配置冲突)
- [2. 认证信息冲突](#2. 认证信息冲突)
-
- [SSH 密钥冲突:](#SSH 密钥冲突:)
- 账号认证冲突:
- [3. 解决方案](#3. 解决方案)
-
- 方案1:为不同平台设置不同的代理规则
- 方案2:使用代理软件的规则模式
- 方案3:分开配置不同的远程仓库
- [方案4:使用 SSH 替代 HTTPS](#方案4:使用 SSH 替代 HTTPS)
- [4. 凭据管理冲突解决](#4. 凭据管理冲突解决)
- [5. 推荐的最佳实践](#5. 推荐的最佳实践)
- [6. 验证配置是否正确](#6. 验证配置是否正确)
- 文章推荐
前言
最近在用github拉取代码,不知道什么情况下,导致两者出现冲突。
使用完毕github后,隔天再次推送gitee上面代码就提示无法连接到服务器了,
并且提示本地ip地址127.0.0.1,以及端口号7898
错误提示
html
git.exe clone --progress -v -- "https://gitee.com/xxx/xxx.git" "D:\github\xxx"
Cloning into 'D:\github\xxx'...
fatal: unable to access 'https://gitee.com/xxx/xxx.git/':
Failed to connect to 127.0.0.1 port 7898 after 2116 ms: Couldn't connect to server
git 未能顺利结束 (退出码 128) (2250 ms @ 2025/9/24 14:47:30)
解决方案
这个问题是因为 Git 试图通过本地 7898 端口的代理服务器连接 Gitee,但代理服务没有运行。以下是几种解决方案:
方案1:临时关闭 Git 的代理设置(推荐先尝试)
bash
# 取消全局代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy
# 或者只为 Gitee 域名取消代理【博主是通过执行下面方式就可以了】
git config --global http.https://gitee.com.proxy ""
git config --global https.https://gitee.com.proxy ""
方案2:检查并启动代理服务
如果您确实需要使用代理,请启动您的代理软件:
- Clash/V2Ray:打开代理软件,确保它正在运行
- 检查代理端口:确认代理软件确实使用 7898 端口
方案3:直接使用命令行取消代理后克隆
bash
# 1. 先取消代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy
# 2. 重新克隆项目
git clone https://gitee.com/xiao555/centerinfo.git "D:\github\centerinfo"
方案4:检查环境变量
有时候代理设置可能来自环境变量:
bash
# 检查环境变量
echo $HTTP_PROXY
echo $HTTPS_PROXY
echo $http_proxy
echo $https_proxy
# 如果设置了,可以临时取消
set HTTP_PROXY=
set HTTPS_PROXY=
针对 Windows 系统的具体操作
方法1:使用 Git Bash 或命令提示符
bash
# 打开命令提示符或 Git Bash,依次执行:
git config --global --unset http.proxy
git config --global --unset https.proxy
git clone https://gitee.com/xiao555/centerinfo.git "D:\github\centerinfo"
方法2:检查全局 Git 配置
bash
# 查看当前 Git 配置
git config --global --list
# 如果看到 proxy 设置,就取消它
验证解决方案
执行完上述命令后,重新运行克隆命令:
bash
git clone https://gitee.com/xiao555/centerinfo.git "D:\github\centerinfo"
如果您确实需要代理
如果您的网络环境必须使用代理,请:
- 确保代理软件正常运行
- 确认代理端口确实是 7898
- 或者将 Git 配置为使用正确的代理地址
为什么会冲突
GitHub 和 Gitee 本身不会冲突,但在使用过程中可能会出现以下几种"冲突"情况:
1. 代理配置冲突
问题原因:
- GitHub:国外网站,通常需要代理才能快速访问
- Gitee:国内网站,直连速度更快,走代理反而慢或无法连接
典型症状:
bash
# 为 GitHub 设置了代理,但 Gitee 也走代理导致失败
git config --global http.proxy http://127.0.0.1:7898
git config --global https.proxy http://127.0.0.1:7898
2. 认证信息冲突
SSH 密钥冲突:
bash
# 同一个 SSH 密钥可能在不同平台权限不同
# GitHub 和 Gitee 可能使用相同的 SSH 密钥,但配置不同
账号认证冲突:
- 缓存了错误的凭据
- 两个平台使用不同的用户名/邮箱
3. 解决方案
方案1:为不同平台设置不同的代理规则
bash
# 只为 GitHub 设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7898
git config --global https.https://github.com.proxy http://127.0.0.1:7898
# Gitee 直连(取消代理)
git config --global http.https://gitee.com.proxy ""
git config --global https.https://gitee.com.proxy ""
方案2:使用代理软件的规则模式
在代理软件(Clash/V2Ray)中设置:
- GitHub 走代理
- Gitee 直连(DIRECT)
方案3:分开配置不同的远程仓库
bash
# 为特定仓库设置单独配置
cd /path/to/gitee-project
git config http.proxy "" # Gitee 项目直连
cd /path/to/github-project
git config http.proxy http://127.0.0.1:7898 # GitHub 项目走代理
方案4:使用 SSH 替代 HTTPS
bash
# 使用 SSH 连接可以避免 HTTP/HTTPS 代理问题
git clone git@gitee.com:xiao555/centerinfo.git
git clone git@github.com:username/repo.git
4. 凭据管理冲突解决
检查当前配置:
bash
# 查看所有 Git 配置
git config --global --list
# 查看特定配置
git config --global http.proxy
git config --global https.proxy
清理缓存凭据:
bash
# Windows 凭据管理器清理
# 或使用 Git 命令清理
git credential-manager reject https://gitee.com
git credential-manager reject https://github.com
5. 推荐的最佳实践
配置示例:
bash
# 取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 按需为国外网站设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7898
git config --global http.https://gitlab.com.proxy http://127.0.0.1:7898
# 国内网站直连
git config --global http.https://gitee.com.proxy ""
6. 验证配置是否正确
bash
# 测试 Gitee 连接(应该直连)
git ls-remote https://gitee.com/xiao555/centerinfo.git
# 测试 GitHub 连接(应该走代理)
git ls-remote https://github.com/someuser/somerepo.git
文章推荐
【代码管理】在本地使用github和gitee之后,可能存在冲突,导致再次提交代码时提示Couldn't connect to server
【代码管理】使用github在线源代码管理工具时,遇到443提示服务器无法连接到
【Git】查看凭据管理器的账号信息,并删除账号,解决首次认证登录失败后无法重新登录的问题
总结:温故而知新,不同阶段重温知识点,会有不一样的认识和理解,博主将巩固一遍知识点,并以实践方式和大家分享,若能有所帮助和收获,这将是博主最大的创作动力和荣幸。也期待认识更多优秀新老博主。