Git 配置 HTTP 方式连接内网 Gitea 记录

起因

内网有台 Gitea 服务器,地址是 http://内网域名:端口号,之前配的是 SSH 方式,偶尔会遇到连接问题。想换成 HTTP 方式,一劳永逸。

同时手上积累了太多乱七八糟的 Git 配置,有 GitHub 的、Gitee 的、好几个不同的 safe.directory,还有 SourceTree 相关配置,看着心烦,一并清理掉。

此文章使用Claude Code辅助编写

配置过程

第一步:看看现状

git config --global --list

一执行吓一跳,输出一大堆,什么 GitHub 的 URL 重写、Gitee 的凭证配置、SourceTree 的 difftool/mergetool 配置、还有好几个 safe.directory 目录。得全清掉。

第二步:清理旧配置

--unset-all 逐项删除:

git config --global --unset-all user.name
git config --global --unset-all user.email
git config --global --unset-all url.https://github.com/.insteadof
git config --global --unset-all url.https://.insteadof
git config --global --unset-all credential.https://gitee.com.provider
git config --global --unset-all safe.directory
git config --global --unset-all credential.http://内网域名:端口号.provider
git config --global --unset-all url.http://内网域名:端口号/.insteadof
git config --global --unset-all difftool.sourcetree.cmd
git config --global --unset-all mergetool.sourcetree.cmd
git config --global --unset-all mergetool.sourcetree.trustexitcode
git config --global --unset-all mergetool.sourcetree.keepbackup
git config --global --unset-all credential.http://内网域名:端口号.provider

第三步:写入新配置

git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"
git config --global credential.http://内网域名:端口号.provider "generic"
git config --global url.http://内网域名:端口号/.insteadof "ssh://git@内网域名:端口号/"
git config --global credential.helper "store"
git config --global core.autocrlf "true"

第四步:弹出登录窗口并完成凭证保存

这一步会弹出用户名密码输入框。

执行以下命令:

git config --global credential.helper
git credential fill

接着在交互提示下依次输入:

protocol=http
host=内网域名:端口号

回车后,会弹出一个 Windows 凭据窗口,要求输入用户名密码

  • 用户名:填写你的 Gitea 登录用户名(注意不是邮箱)
  • 密码:填写你的 Gitea 登录密码

输入完成后回车确认。Git 会自动将凭证保存到 ~/.git-credentials 文件中,之后再访问该服务器就不需要重新输入了。

网页授权认证说明:如果你的 Gitea 开启了网页授权(OAuth)或两步验证(2FA),则这里的密码需要填写个人访问令牌(Personal Access Token),而不是登录密码。令牌可在 Gitea 网页端「设置 → 个人令牌」中生成。

第五步:测试连通性

# 测试服务器是否可达
curl -s http://内网域名:端口号/api/swagger

# 测试 Git 仓库访问(换成实际存在的仓库路径)
git ls-remote --heads http://内网域名:端口号/你的用户名/仓库名.git

几个关键配置项的含义

配置项 作用
credential.http://内网域名:端口号.provider = generic 告诉 Git 对这个服务器使用通用的凭证存储方式
url.http://内网域名:端口号/.insteadof = ssh://git@内网域名:端口号/ 把 SSH 格式的 URL 自动转成 HTTP URL
credential.helper = store 把凭证明文存到 ~/.git-credentials,省去重复输入

URL 重写这个配置特别实用。比如你项目里远程仓库地址写的是 ssh://git@内网域名:端口号/用户名/仓库名.git,Git 会在实际请求时自动把它转换成 http://内网域名:端口号/用户名/仓库名.git,不用改任何项目配置。

踩过的坑

  • 凭证里的密码如果是普通登录就填 Gitea 密码,如果开启了网页授权或两步验证,需要填个人访问令牌,不是登录密码。
  • 端口号千万不能写错。

配置完成后的样子

$ git config --global --list
core.autocrlf=true
credential.helper=store
user.name=你的用户名
user.email=你的邮箱
credential.http://内网域名:端口号.provider=generic
url.http://内网域名:端口号/.insteadof=ssh://git@内网域名:端口号/

干净多了。

总结

内网 Gitea 用 HTTP 连接比 SSH 省心,不用操心密钥问题。配置其实就那么几行,关键是搞清楚每个配置项是干嘛用的,别一股脑复制粘贴完不知道自己配了啥。


Git 配置 HTTP 方式连接内网 Gitea 记录

https://blog.ckh-cn.site/index.php/2026/04/16/208.html

作者

CKH

发布时间

2026-04-16

许可协议

CC BY 4.0

OS: Linux 115a654af43f 5.15.0-113-generic #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64
CPU Info:
Memory Info:
评论