ModelArts 下 Git 问题整理:现象与原因

背景结论

这次问题的核心不是 VS Code 插件本身,而是 ModelArts 环境的持久化机制 + GitHub 网络限制 + Git 配置位置不稳定 叠加导致的。

在 ModelArts 中,通常只有:

bash 复制代码
/home/ma-user/work

更适合保存长期文件。

而下面这些位置重启后可能被清理或重建:

bash 复制代码
~/.gitconfig
~/.ssh/known_hosts
/root/.gitconfig
/root/.ssh/known_hosts
/tmp

因此,很多 git config --global ... 配置重启后会失效。


1. Git commit 失败:Please tell me who you are

报错现象

text 复制代码
*** Please tell me who you are.

fatal: no email was given and auto-detection is disabled

原因

Git 提交必须有作者信息:

bash 复制代码
user.name
user.email

但 ModelArts 重启后,原来写在:

bash 复制代码
~/.gitconfig

里的全局配置可能丢失。

所以 VS Code / Git 插件执行 commit 时找不到身份信息,就会失败。

本质原因

text 复制代码
Git 作者信息写在了非持久化位置;
ModelArts 重启后配置丢失;
commit 时无法生成 Author 信息。

2. Git pull / push 失败:github.com port 22: Connection timed out

报错现象

text 复制代码
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

原因

如果 Git remote 是 SSH 格式:

bash 复制代码
git@github.com:USER/REPO.git

默认会访问:

text 复制代码
github.com:22

但 ModelArts 环境通常无法访问 GitHub 的 SSH 22 端口。

之前通过下面命令可以绕过:

bash 复制代码
git config --global url."ssh://git@ssh.github.com:443/".insteadOf git@github.com:

但这个配置写在 ~/.gitconfig,重启后可能丢失,于是 Git 又重新走回:

text 复制代码
github.com:22

本质原因

text 复制代码
ModelArts 不能稳定访问 GitHub SSH 22 端口;
GitHub SSH 443 重写规则写在 global 配置中;
global 配置重启后丢失;
所以 Git 又回到默认 22 端口。

3. Git pull 失败:Host key verification failed

报错现象

text 复制代码
Host key verification failed.
fatal: Could not read from remote repository.

原因

SSH 第一次连接 GitHub 时,会把 GitHub 的主机指纹保存到:

bash 复制代码
~/.ssh/known_hosts

但 ModelArts 重启后,~/.ssh/known_hosts 可能被清理。

如果 VS Code / Git 插件在非交互模式下执行 git pull,它无法让你手动输入:

text 复制代码
yes

于是 host key 校验失败。

本质原因

text 复制代码
known_hosts 保存在非持久化目录;
重启后 GitHub 主机指纹丢失;
VS Code Git 插件无法交互确认 SSH 指纹;
因此 SSH 安全校验失败。

4. VS Code 日志中反复出现:not a git repository

报错现象

text 复制代码
fatal: not a git repository (or any parent up to mount point /home/ma-user)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

原因

VS Code 打开的可能是:

bash 复制代码
/home/ma-user/work

而真正的 Git 仓库在:

bash 复制代码
/home/ma-user/work/hanayo_baseline

VS Code Git 插件会扫描整个 workspace,扫描到非 Git 仓库目录时,就会打印这个日志。

这通常不是主要错误。

本质原因

text 复制代码
VS Code 打开的 workspace 范围大于实际 Git 仓库;
Git 插件扫描了不是仓库的目录;
所以出现 not a git repository。

5. VS Code 日志中出现:lost+found permission denied

报错现象

text 复制代码
Unable to read workspace folder '/home/ma-user/work/lost+found':
EACCES: permission denied

原因

lost+found 是 Linux 文件系统目录,通常需要 root 权限访问。

VS Code Git 插件扫描 /home/ma-user/work 时碰到它,就会报权限不足。

这一般不是 Git push / pull 失败的根因。

本质原因

text 复制代码
VS Code 扫描整个 /home/ma-user/work;
其中 lost+found 是系统目录;
普通用户无权限读取;
因此出现 EACCES。

6. 为什么 git config --global ... 在 ModelArts 中不可靠?

原因

--global 配置一般写入:

bash 复制代码
~/.gitconfig

例如:

bash 复制代码
/home/ma-user/.gitconfig
/root/.gitconfig

但在 ModelArts 中,~ 目录不一定是持久化目录。

如果重启后 ~/.gitconfig 被清理,那么这些配置都会消失:

bash 复制代码
user.name
user.email
url."ssh://git@ssh.github.com:443/".insteadOf
core.sshCommand

本质原因

text 复制代码
global Git 配置不在 /home/ma-user/work;
ModelArts 重启后非 work 目录可能被清理;
所以全局 Git 配置不稳定。

7. 推荐的持久化思路

不推荐

不要把关键配置只放在:

bash 复制代码
~/.gitconfig
~/.ssh/known_hosts

推荐

把配置放在 /home/ma-user/work 相关位置:

text 复制代码
Git 仓库级配置:
/home/ma-user/work/hanayo_baseline/.git/config

GitHub known_hosts:
/home/ma-user/work/git-config/known_hosts

初始化脚本:
/home/ma-user/work/init_git_modelarts.sh

8. 推荐配置方式

进入仓库:

bash 复制代码
cd /home/ma-user/work/hanayo_baseline

写入仓库级 Git 配置:

bash 复制代码
git config user.name "XXX"
git config user.email "XXX@outlook.com"
git config url."ssh://git@ssh.github.com:443/".insteadOf git@github.com:

保存 GitHub SSH host key 到持久化目录:

bash 复制代码
mkdir -p /home/ma-user/work/git-config

ssh-keyscan -p 443 ssh.github.com > /home/ma-user/work/git-config/known_hosts

chmod 600 /home/ma-user/work/git-config/known_hosts

让当前仓库使用这个持久化的 known_hosts:

bash 复制代码
git config core.sshCommand "ssh -o UserKnownHostsFile=/home/ma-user/work/git-config/known_hosts -o StrictHostKeyChecking=yes"

检查配置来源:

bash 复制代码
git config --show-origin --get-regexp 'user.name|user.email|url.*insteadOf|core.sshCommand'

9. 一句话总结

ModelArts 下这些 Git 问题的共同根因是:

text 复制代码
关键 Git 配置写在了重启后可能被清理的位置;
GitHub 默认 SSH 22 端口在 ModelArts 中不可用;
known_hosts 丢失后 VS Code 非交互式 Git 命令无法确认主机指纹。

因此推荐原则是:

text 复制代码
把 Git 配置写进 /home/ma-user/work/仓库/.git/config;
把 known_hosts 放进 /home/ma-user/work/git-config;
让 GitHub SSH 统一走 ssh.github.com:443。