问题
已将项目整个文件夹拷贝到克隆下来的文件夹中,并添加了所有文件,并修改了commit -m,使用git push -u origin main提交的时候会出现vscode请求登录github,确定之后需要等待很久,也无果
原因
由于 远程服务器无法访问 GitHub 网页认证服务,也就是 GitHub 的 OAuth 网页流程(VS Code 的 GUI 登录)在你的远程服务器上不适用或者被阻断。
解决方法
使用 SSH 密钥认证
这是最稳定、适合远程服务器的方法
步骤:
- 在远程服务器生成 SSH 密钥(如果没有的话)
bash
ssh-keygen -t ed25519 -C "[email protected]"
按提示一路回车即可,生成的密钥一般保存在 ~/.ssh/id_ed25519 和 id_ed25519.pub。
- 查看公钥内容(复制用):
bash
cat ~/.ssh/id_ed25519.pub
- 将公钥添加到 GitHub:
- 登录 GitHub;
- 进入 Settings > SSH and GPG keys;
- 点击 "New SSH key";
- 填写标题,粘贴上面复制的公钥。
- 测试 SSH 是否配置成功:
bash
ssh -T [email protected]
成功的话会看到类似:
bash
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
- 确保 Git 使用 SSH 地址作为远程仓库地址:
检查:
bash
git remote -v
如果显示的是 https://github.com/...,你需要改为 SSH 形式:
bash
git remote set-url origin [email protected]:your_username/your_repo.git
- 重新推送代码:
bash
git push -u origin main