# Git Push 到 Hugging Face 问题修复指南 ## 问题描述 ``` Connection closed by 198.18.0.7 port 22 fatal: Could not read from remote repository. ``` ## 快速解决方案(推荐) ### 方案 1: 切换到 HTTPS(最简单,推荐) 1. **查看当前远程仓库 URL:** ```bash git remote -v ``` 2. **将 SSH URL 切换为 HTTPS URL:** ```bash # 格式:git remote set-url origin https://huggingface.co/spaces/USERNAME/SPACE_NAME # 例如: git remote set-url origin https://huggingface.co/spaces/your-username/your-space-name ``` 3. **使用 Hugging Face token 进行认证:** - 访问 https://huggingface.co/settings/tokens - 创建一个新的 token(需要有 write 权限) - push 时使用 token 作为密码: ```bash git push # Username: 你的 Hugging Face 用户名 # Password: 你的 token(不是密码) ``` ### 方案 2: 修复 SSH 配置 如果必须使用 SSH,按以下步骤操作: 1. **检查 SSH key 是否存在:** ```bash ls -la ~/.ssh/ ``` 2. **如果没有 SSH key,生成一个:** ```bash ssh-keygen -t ed25519 -C "your_email@example.com" # 按 Enter 使用默认路径 ``` 3. **查看公钥并添加到 Hugging Face:** ```bash cat ~/.ssh/id_ed25519.pub ``` - 复制输出的内容 - 访问 https://huggingface.co/settings/keys - 点击 "New SSH key",粘贴公钥并保存 4. **创建或编辑 SSH 配置文件:** ```bash nano ~/.ssh/config ``` 添加以下内容: ``` Host hf.co HostName hf.co User git Port 22 IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes ``` 5. **设置正确的文件权限:** ```bash chmod 600 ~/.ssh/config chmod 600 ~/.ssh/id_ed25519 chmod 644 ~/.ssh/id_ed25519.pub ``` 6. **测试 SSH 连接:** ```bash ssh -T git@hf.co ``` 应该看到类似 "You've successfully authenticated" 的消息 ### 方案 3: 使用 SSH over HTTPS 端口(如果端口 22 被防火墙阻止) 如果端口 22 被阻止,可以尝试使用端口 443: 1. **编辑 SSH 配置:** ```bash nano ~/.ssh/config ``` 添加或修改为: ``` Host hf.co HostName hf.co User git Port 443 IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes ``` 2. **测试连接:** ```bash ssh -T git@hf.co ``` ## 诊断步骤 运行诊断脚本: ```bash chmod +x fix_git_push.sh ./fix_git_push.sh ``` ## 常见问题 ### Q: 如何找到我的 Hugging Face 空间名称? A: 查看当前远程 URL: ```bash git remote -v ``` 或者访问 https://huggingface.co/spaces 查看你的空间列表 ### Q: 如何生成新的 SSH key? A: ```bash ssh-keygen -t ed25519 -C "your_email@example.com" ``` ### Q: 如何查看已添加的 SSH key? A: ```bash cat ~/.ssh/id_ed25519.pub ``` ### Q: HTTPS 方式需要每次都输入 token 吗? A: 可以配置 Git credential helper 来保存凭据: ```bash git config --global credential.helper store ``` ## 推荐做法 对于大多数用户,**使用 HTTPS + token** 是最简单可靠的方式,因为: - 不需要配置 SSH - 不受防火墙端口限制 - 更容易调试 - Hugging Face 官方推荐