Mac无法通过SSH/HTTPS克隆GitLab仓库问题求助
无法克隆GitLab仓库:SSH超时+HTTPS认证失败
之前推送、拉取GitLab代码时出现问题,我删除了Mac上的本地Git仓库,但现在无法重新克隆项目。尝试SSH和HTTPS两种方式均失败,已删除原有SSH密钥并生成新密钥关联至GitLab,甚至重装Git也没能解决问题。
操作日志与错误信息
marcus@Marcuss-MacBook-Pro-2 cd .git marcus@Marcuss-MacBook-Pro-2 .git % git clone git@gitlab.stud.idi.ntnu.no:marcuslj/rentadog-git Cloning into 'rentadog'... ssh: connect to host gitlab.stud.idi.ntnu.no port 22: Operation timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. marcus@Marcuss-MacBook-Pro-2 .git git clone https://gitlab.stud.idi.ntnu.no/marcuslj/rentadog.git Cloning into 'rentadog'... Username for 'https://gitlab.stud.idi.ntnu.no: marcuslj Password for 'https://marcuslj@gitlab.stud.idi.ntnu.no remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password. See https://gitlab.stud.idi.ntnu.no/help/topics/git/troubleshooting.git#error-on-git-fetch-http-basic-access-denied fatal: Authentication failed for 'https://gitlab.stud.idi.ntnu.no/marcuslj/rentadog.git/' marcus@Marcuss-MacBook-Pro-2 git %
排查与解决步骤
针对SSH超时问题
- 验证网络连通性:执行
ping gitlab.stud.idi.ntnu.no查看是否能正常解析并连通,用traceroute gitlab.stud.idi.ntnu.no跟踪路由节点,排查是否是学校网络限制、本地防火墙或代理导致端口22被阻断。 - 尝试切换SSH端口:部分GitLab实例支持通过443端口提供SSH服务,改用以下命令克隆:
git clone ssh://git@gitlab.stud.idi.ntnu.no:443/marcuslj/rentadog-git - 配置SSH客户端参数:在
~/.ssh/config文件中添加以下配置(替换为你的新密钥文件名),优化连接稳定性:
配置完成后执行Host gitlab.stud.idi.ntnu.no HostName gitlab.stud.idi.ntnu.no User git Port 22 IdentityFile ~/.ssh/your_new_key TCPKeepAlive yes ConnectTimeout 30ssh -T git@gitlab.stud.idi.ntnu.no测试SSH认证是否成功。
针对HTTPS认证失败问题
- 确认账号2FA状态:如果你的GitLab账号开启了两步验证,必须使用个人访问令牌(PAT)代替密码,不能直接输入账号密码。
- 生成并使用PAT:在GitLab账号设置中生成PAT,勾选
read_repository等仓库相关权限,然后用以下命令克隆(替换为你的PAT):git clone https://marcuslj:your_personal_access_token@gitlab.stud.idi.ntnu.no/marcuslj/rentadog.git - 清除旧凭据:打开Mac的「钥匙串访问」,搜索
gitlab.stud.idi.ntnu.no,删除相关的密码条目,避免系统自动填充错误的旧密码。
内容的提问来源于stack exchange,提问作者Marcuslj
相关产品推荐
相关产品推荐

